Ale*_*xxx 6 linker gcc shared-libraries static-libraries
我试图链接静态libc.a和动态库.so失败.
我已经尝试过以下方法:
首先,我测试所有动态:
它正在工作(编译和执行)
其次我测试我想要的东西(动态lib和静态libc):
它正在编译,但在执行时,它是段错误的!一个strace显示它正在尝试访问libc.so !!!
最后,我尝试编译一个简单的程序,没有引用动态库
怎么做?
谢谢
你的第二个测试(你要做的那个)在i686-linux上为我工作:
$ cat libtest.c
#include <stdio.h>
void foo() { printf("%d\n", 42); }
$ cat main.c
#include <stdio.h>
extern void foo();
int main() { puts("The answer is:"); foo(); }
$ export LD_LIBRARY_PATH=$PWD
$ gcc -shared libtest.c -o libtest.so && gcc -c main.c -o main.o && gcc main.o -o test -L. -ltest && ./test
The answer is:
42
$ gcc -shared libtest.c -o libtest.so && gcc -c main.c -o main.o && gcc main.o -o test libtest.so /usr/lib/libc.a && ./test
The answer is:
42
Run Code Online (Sandbox Code Playgroud)
但是,您必须意识到您构建的共享库取决于共享的libc.因此,它试图在运行时打开它是很自然的.
$ ldd ./libtest.so
linux-gate.so.1 => (0xb80c7000)
libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7f4f000)
/lib/ld-linux.so.2 (0xb80c8000)
Run Code Online (Sandbox Code Playgroud)
实现目标的一种方法是使用:-static-libgcc -Wl,-Bstatic -lc.
| 归档时间: |
|
| 查看次数: |
27163 次 |
| 最近记录: |