C程序中对'printf'的未定义引用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C程序中对'printf'的未定义引用相关的知识,希望对你有一定的参考价值。
我试图理解一些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
", &a);
printf(" sizof(a) = %d
", sizeof(a));
printf("__alignof__ = %d
", __alignof__(a));
printf(" p = %p
", p);
printf(" b = %p
", &b);
printf(" sizof(b) = %d
", sizeof(b));
printf("__alignof__ = %d
", __alignof__(b));
p = p + (BYTES / sizeof(int));
printf(" p = %p
", p);
printf(" c = %p
", &c);
printf(" sizof(c) = %d
", sizeof(c));
printf("__alignof__ = %d
", __alignof__(c));
p = p + sizeof(b) / sizeof(int) ;
printf(" p = %p
", p);
long unsigned int alignment;
for(;(alignment=( (long unsigned int) p) & (BYTES << 1 )- 1 , printf("alignment = %p
",alignment), alignment != 0 ) ; p++);
printf(" d = %p
", &d);
printf(" sizof(d) = %d
", sizeof(d));
printf("__alignof__ = %d
", __alignof__(d));
printf(" p = %p
", p);
}
include/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 = .;
}
Problem
每当我尝试链接时,它都会因未定义的引用而失败
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
没有联系。我在glibc-static
stackoverflow问题之后安装了this并尝试使用-lc
切换ld
dudarev@Test-Sandbox section $ ld -lc -T ld.lds align.o imposible.o -o a.out
ld: cannot find -lc
我错过了什么人?
提前致谢
关于这个问题:
imposible.c:6:9:警告:从指针转换为不同大小的整数[-Wpointer-to-int-cast] return(int)&base;
这是因为'base'是类型:long int
,imposible()
函数的返回类型是: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
", (void*)&a);
printf(" sizof(a) = %lu
", sizeof(a));
printf("__alignof__ = %lu
", __alignof__(a));
printf(" p = %p
", p);
printf(" b = %p
", &b);
printf(" sizof(b) = %lu
", sizeof(b));
printf("__alignof__ = %lu
", __alignof__(b));
p = p + (BYTES / sizeof(int));
printf(" p = %p
", p);
printf(" c = %p
", &c);
printf(" sizof(c) = %lu
", sizeof(c));
printf("__alignof__ = %lu
", __alignof__(c));
p = p + sizeof(b) / sizeof(int) ;
printf(" p = %p
", p);
long unsigned int alignment;
for( ;
(alignment = (long unsigned int) p) &
((BYTES << 1 )- 1) && alignment != 0 ;
p++)
{
printf("alignment = %p
", (void*)alignment);
}
printf(" d = %p
", &d);
printf(" sizof(d) = %lu
", sizeof(d));
printf("__alignof__ = %lu
", __alignof__(d));
printf(" p = %p
", 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()
以上是关于C程序中对'printf'的未定义引用的主要内容,如果未能解决你的问题,请参考以下文章
部署到Heroku:NoMethodError:nil:NilClass的未定义方法'+'
C语言中一个简单的球3个数最大数的程序中,最后一步:printf(''max=%d\n'',max);怎么理解
安装的未定义方法`devise'(调用'Install.connection'建立连接):Class
Scipoptsuite-6.0.0中的GCG编译错误:reader_zpl.c :(。text + 0x319):对`zpl_read'的未定义引用