C程序中对'printf'的未定义引用

时间:2017-06-23 09:25:07

标签: c gcc linker ld

我想了解一些gcc功能,例如__attribute__,更准确地说,是如何使用__attribute__((__section__("foo")))在特定内存位置分配数据/代码。

我的设置

align.c

#include <stdio.h>
//Align some ints
#define BYTES 16
#define __weird_thing __attribute__((__section__("weird.data")))
int __weird_thing *bootstrap;
int a __attribute__((aligned(BYTES))) = 1;
int b __attribute__((aligned(BYTES))) = 2;
int c = 3;
int d __attribute__((aligned(BYTES))) = 4;


int main( int argc, char *argv[])
{
        extern a, b, c, d;
        int *p = &a;
        printf(" a = %p \n", &a);
        printf(" sizof(a) = %d\n", sizeof(a));
        printf("__alignof__ = %d\n", __alignof__(a));
        printf(" p = %p \n\n", p);

        printf(" b = %p \n", &b);
        printf(" sizof(b) = %d\n", sizeof(b));
        printf("__alignof__ = %d\n", __alignof__(b));
        p = p + (BYTES / sizeof(int));
        printf(" p = %p \n\n", p);

        printf(" c = %p \n", &c);
        printf(" sizof(c) = %d\n", sizeof(c));
        printf("__alignof__ = %d\n", __alignof__(c));
        p = p + sizeof(b) / sizeof(int) ;
        printf(" p = %p \n\n", p);

        long unsigned int alignment;
        for(;(alignment=( (long unsigned int) p) & (BYTES << 1 )- 1 ,    printf("alignment = %p\n",alignment), alignment  != 0 ) ; p++);
        printf(" d = %p \n", &d);
        printf(" sizof(d) = %d\n", sizeof(d));
        printf("__alignof__ = %d\n", __alignof__(d));
        printf(" p = %p \n\n", p);
}

包括/ imposible.h

int imposible(void);

imposible.c

#include<imposible.h>
extern unsigned long int base;
int imposible(void)
{
        base++;
        return (int) &base;
}

ld.lds

SECTIONS
{
        . = 0x10000;
        weird.data : { *(weird.data) }
        base = .;
}

问题

每当我尝试链接时,它都会因未定义的引用而失败

dudarev@Test-Sandbox section $ gcc -c align.c -o align.o
dudarev@Test-Sandbox section $ gcc -c -I include/ imposible.c -o imposible.o
imposible.c: In function ‘imposible’:
imposible.c:6:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  return (int) &base;
     ^

dudarev@Test-Sandbox section $ ld -T ld.lds align.o imposible.o -o a.out
align.o: In function `main':
align.c:(.text+0x27): undefined reference to `printf'
align.c:(.text+0x3b): undefined reference to `printf'
align.c:(.text+0x4f): undefined reference to `printf'
align.c:(.text+0x65): undefined reference to `printf'
align.c:(.text+0x79): undefined reference to `printf'
align.o:align.c:(.text+0x8d): more undefined references to `printf' follow

我没看到它找不到printf,这意味着stdio没有链接。 我在this stackoverflow问题后安装了glibc-static,并尝试-lc切换为ld,最后进入

dudarev@Test-Sandbox section $ ld -lc -T ld.lds align.o imposible.o -o a.out
ld: cannot find -lc

我错过了什么人?

提前致谢

1 个答案:

答案 0 :(得分:0)

关于这个问题:

imposible.c:6:9:警告:从指针转换为不同大小的整数[-Wpointer-to-int-cast]   return(int)&amp; base;

这是因为&#39; base&#39;是类型:long intimposible()函数的返回类型是:int

文件align.c导致编译器输出大量警告,所有这些都应该被纠正。

文件:ld.lds缺少几个必要的陈述。

ld.lds缺少的陈述是无法找到printf()函数的原因

这是align.c文件

的更正版本
#include <stdio.h>

//Align some ints
#define BYTES 16

#define weird_thing __attribute__((__section__("weird.data")))
int weird_thing *bootstrap;

int a __attribute__((aligned(BYTES))) = 1;
int b __attribute__((aligned(BYTES))) = 2;
int c = 3;
int d __attribute__((aligned(BYTES))) = 4;


int main( void )
{
        extern int a;
        extern int b;
        extern int c;
        extern int d;
        int *p = &a;
        printf(" a = %p \n", (void*)&a);
        printf(" sizof(a) = %lu\n", sizeof(a));
        printf("__alignof__ = %lu\n", __alignof__(a));
        printf(" p = %p \n\n", p);

        printf(" b = %p \n", &b);
        printf(" sizof(b) = %lu\n", sizeof(b));
        printf("__alignof__ = %lu\n", __alignof__(b));
        p = p + (BYTES / sizeof(int));
        printf(" p = %p \n\n", p);

        printf(" c = %p \n", &c);
        printf(" sizof(c) = %lu\n", sizeof(c));
        printf("__alignof__ = %lu\n", __alignof__(c));
        p = p + sizeof(b) / sizeof(int) ;
        printf(" p = %p \n\n", p);

        long unsigned int alignment;
        for( ;
             (alignment = (long unsigned int) p) &
             ((BYTES << 1 )- 1) && alignment != 0 ;
             p++)
        {
             printf("alignment = %p\n", (void*)alignment);
        }

        printf(" d = %p \n", &d);
        printf(" sizof(d) = %lu\n", sizeof(d));
        printf("__alignof__ = %lu\n", __alignof__(d));
        printf(" p = %p \n\n", p);
}

仅编译/链接align.c文件然后运行产生以下内容:(在我的Linux计算机上,没有ld.lds文件)

a = 0x602070 
 sizof(a) = 4
__alignof__ = 16
 p = 0x602070 

 b = 0x602080 
 sizof(b) = 4
__alignof__ = 16
 p = 0x602080 

 c = 0x602084 
 sizof(c) = 4
__alignof__ = 4
 p = 0x602084 

alignment = 0x602084
alignment = 0x602088
alignment = 0x60208c
alignment = 0x602090
alignment = 0x602094
alignment = 0x602098
alignment = 0x60209c
 d = 0x602090 
 sizof(d) = 4
__alignof__ = 16
 p = 0x6020a0 

注意:align.c文件具有main()函数,该函数不会调用任何其他发布的函数,例如imposible()