Recently, my company wants to upgrade the compiler from gcc-3.4 to gcc-4.5. However, our client machine may not have updated libstdc++.so , so we want to statically link our binary.
Our program needs to tune malloc()/free() for very high performance requirements.
I modified the makefile, added -static when linking, and received the following error message:
/usr/lib64/libc.a(malloc.o)(.text+0x18c0): In function `free': : multiple definition of `free' ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o)(.text+0x3430): first defined here /usr/bin/ld: Warning: size of symbol `free' changed from 271 in ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o) to 255 in /usr/lib64/libc.a(malloc.o) /usr/lib64/libc.a(malloc.o)(.text+0x3970): In function `malloc': : multiple definition of `malloc' ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o)(.text+0x29c0): first defined here /usr/bin/ld: Warning: size of symbol `malloc' changed from 281 in ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o) to 461 in /usr/lib64/libc.a(malloc.o) /usr/lib64/libc.a(malloc.o)(.text+0x4050): In function `realloc': : multiple definition of `realloc' ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o)(.text+0x3e80): first defined here /usr/bin/ld: Warning: size of symbol `realloc' changed from 335 in ../../ic/src/memmgr/libmemmgr_mt_thread.a(memmgr_mt_thread.o) to 927 in /usr/lib64/libc.a(malloc.o)
Well, that’s reasonable, since libc.a already has malloc()/free() .
But what bothers me is that there are no errors with dynamic linking. I searched and found this question: How to override malloc () on Linux for use in C ++ new . The response says that the linker considers the library file (.a) and the object file (.o) differently. Now I know the cause of the error with static binding, but not with dynamics.
However, I tried the solution described in this answer, directly replaced the library file with the object file, but there is no difference. I still got a multiple definition binding error. I also tried -static-libgcc (because I don’t know what to do, I just tried everything I saw on the gcc man page), but that doesn’t help either.
I do not need to use static binding. I just want to solve the problem with the version of libstdc++.so Any suggestion would be appreciated.
Thanks in advance.
Edit: Sorry, I did not explain. Using #define malloc ... might not help. Since our program is C ++. The idiom #define can only use the malloc()/free() function. But our program uses new/delete to allocate / free memory. Thanks anyway: D