如何解决gcc linux中的多个定义错误?

我在尝试静态链接libDuma时遇到以下错误,请问如何让g ++从libDuma使用malloc?

sunny@sunny-laptop:~/CodeTest$ g++ ./testDuma.cpp -g  -o testDuma -static -lduma -pthread
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `free':
(.text+0x4b00): multiple definition of `free'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x25f0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `malloc':
(.text+0x4bc0): multiple definition of `malloc'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x2730): first defined here
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `realloc':
(.text+0x5950): multiple definition of `realloc'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x23d0): first defined here
collect2: ld returned 1 exit status

不要强制使用完全静态的链接(不要使用-static标志) - 在任何现代UNIX系统上这样做都是一个非常糟糕的主意(TM)。

相反,只需静态链接libduma。 这些命令都可以工作:

g++ ./testDuma.cpp -g -pthread -o testDuma /path/to/libduma.a
g++ ./testDuma.cpp -g -pthread -o testDuma -Wl,-Bstatic -lduma -Wl,-Bdynamic

添加-nodefaultlibs标志以不链接到libc。 或者,在编译之后删除-lduma并动态链接它:

LD_PRELOAD=/usr/lib/libduma.so ./testDuma
链接地址: http://www.djcxy.com/p/52859.html

上一篇: How to solve Multiple definition errors in gcc linux?

下一篇: Why am I getting a 404 requested resource not available error from Tomcat 7