我试图在不依赖libc (或任何其他)的情况下生成一个可执行文件。首先,我这样做了:
// test.c
void _start()
{
// write(1, "hello!\n", 7);
asm ("int $0x80"::"a"(4), "b"(1), "c"("hello!\n"), "d"(7));
// exit(0);
asm ("int $0x80"::"a"(1), "b"(0));
}用gcc -m32 -nostdlib test.c -o test编译
hello到目前一切尚好。后来,我尝试使用一些更“高级”的C,比如long long。在32位平台上(我的情况),这需要libgcc。
// test.c
void _start()
{
volatile long long int a = 10;
volatile long long int b = 5;
volatile int c = a/b; // Implemented as a call to '__divdi3'
}这将导致undefined reference to '__divdi3'编译失败。似乎是正确的,因为我实际上并没有让它链接。但是添加标志-static-libgcc并不能解决这个问题!为什么?
请注意,我不能动态链接到libgcc。下列情况必须成立:
$ ldd test
not a dynamic executable我正在编译64位Ubuntu 14.04与gcc 4.8.2 (没什么稀奇)。
发布于 2014-05-01 15:11:01
最终我自己找到了解决方案。看来gcc找不到图书馆,也没有抱怨过。我运行了以下命令:
$ locate libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/32/libgcc.a
/usr/lib/gcc/x86_64-linux-gnu/4.8/x32/libgcc.a然后,我没有将-static-libgcc交给编译器,而是将标志更改为:
gcc -m32 -nostdlib test.c -o test -L/usr/lib/gcc/x86_64-linux-gnu/4.8/32 -lgcc它编译和运行都很好!
-L是多余的。下列措施也有效:
gcc -m32 -nostdlib test.c -o test -lgcchttps://stackoverflow.com/questions/23410221
复制相似问题