getting ld to ignore undefined references when making a DLL - gcc

I'm trying to get gcc's ld to ignore unresolved references when putting together a shared library from a bunch of object files compiled with -fpic flag.
I tried a bunch of options so far such as (replaced the long file names of the many object files with a few small ones for brevity):
ld --allow-shlib-undefined --unresolved-symbols=ignore-all -shared 1.o 2.o -o lib0.so
ld -G 1.o 2.o -o lib0.so
(I've red somewhere that -G will allow unresolved references, but had no luck with it.)
Running it though gcc (with -Wl,--unresolved-symbols=ignore-all) results in fewer unresolved references as it links by default with
-lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt
but it still complains about not having -lopengl32 and -lgdi32.
Compiling the lib without -fpic, stashing all the object files in a .a with ar and static linking to it while having the exe program link to -lopengl32 and -lgdi32 results in working completely fine.
The actual error messages (replaced the long file and function names for brevity):
[file].o: In function `[function]': [file].cpp:19: undefined reference to `memcpy'
[file].o: In function `[function]': [file].cpp:26: undefined reference to `memcpy'
[file].o:[file].cpp:(.xdata+0x4c): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.xdata+0x74): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.rdata$[file]]+0x20):undefined reference to `__cxa_pure_virtual'
[file].o:[file].cpp:(.rdata$[file]]+0x28): undefined reference to `__cxa_pure_virtual'
How can I get ld to ignore undefined references (at least from libopengl32 and libgdi32 if not libstdc++ etc too) and let the program thats going to use it link to them?

Actually ld is not part of gcc. It is part of binutils.
Try to link with gcc, or in case of C++ with g++ instead of ld.
Also, on Windows you can't build shared libraries (.dll) or executables (.exe) with unresolved symbols. Part of "DLL hell" is that you must make sure all symbols resolve.
Normally it's a matter of finding the library providing those symbols and adding the required -l flags.
However errors like undefined reference to 'memcpy' indicate it's not finding stuff in the standard library. So either you're using ld that did not come with MinGW or there is a flag like -nostdlib present telling the compiler not to look for the standard library.

Related

What default libraries does -nolibc remove?

I am working on a project which eventually requires me to link my code against my customized version of libc, libstdc++ and libgcc. Now I am learning what libraries are linked by default.
I tried to compile my project code with -nolibc and manually provide the needed libraries in the argument list.
g++ -fuse-ld=gold -O2 -o main -static -nolibc main.cpp -lgcc -lm -lc
I got the following error, showing that I missed some libraries.
/usr/lib/gcc/x86_64-linux-gnu/9/libgcc_eh.a(unwind-dw2-fde-dip.o):function _Unwind_Find_FDE: error: undefined reference to 'dl_iterate_phdr'
Which library is missing here?

linker --as-needed flag not pruning libraries

I'm running into an issue after upgrading gcc from 4.1.1 to 4.7.2. The problem is that the ld --as-needed flag is not pruning libraries that are not required if enough libraries with inter dependencies are listed.
For example, if I build a simple program that doesn't need any special libraries, but includes them on the build line, as such
gcc -m32 test.c -Wl,--as-needed -L/usr/local/lib -lrt -lprojcommon -lproj -lrte -o test
then it builds fine and the --as-needed flag does it's job pruning out all of the listed libs that are not needed.
ldd test
linux-gate.so.1 => (0x00bfc000)
libc.so.6 => /lib/libc.so.6 (0x001ac000)
/lib/ld-linux.so.2 (0x0018a000
However, if I add one more library (in this case crypto), then the build fails with undefined reference errors.
gcc -m32 test.c -Wl,--as-needed -L/usr/local/lib -lcrypto -lrt -lprojcommon -lproj -lrte -o test
/usr/local/lib/librte.so: error: undefined reference to 'tla_decap_data'
/usr/local/lib/librte.so: error: undefined reference to 'do_db'
collect2: error: ld returned 1 exit status
This exact same build worked with 4.1.1, but started failing with 4.7.2.
This is part of a general build infra and libraries included on the build line are generic and expected to be pruned via --as-needed. I could fix this with --allow-shlib-undefined, but I'd prefer to find real unresolved symbols at build time. If I do set --allow-shlib-undefined, then I end up with the same set of required libs as the build that worked.
Any insight would be appreciated.

why "undefined reference to `boost::system::generic_category" even if I do link against boost_system

I would understand this error message if I had not put the -lboost_system flag, but it is really here:
g++ -o build/myproject build/main/main.o -L/usr/local/boost/boost_1_52_0/boost/libs -L/usr/lib -Lbuild -L. -lboost_system -lboost_thread -lpthread -lboost_regex -lpq -lmylibrary
build/libmylibrary.a(library.o): In function `__static_initialization_and_destruction_0(int, int)':
library.cpp:(.text+0x25f): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x269): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x273): undefined reference to `boost::system::system_category()'
Do you have any idea what should I investigate to solve the problem ? (I use gcc 4.6.3)
The order at which you link your libraries matters, in your case you have library.cpp that apparently uses the boost_system library
library.cpp:(.text+0x25f): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x269): undefined reference to `boost::system::generic_category()'
library.cpp:(.text+0x273): undefined reference to `boost::system::system_category()'
To solve this you should move the boost_system library to the end of your link line
g++ -o build/myproject build/main/main.o -L/usr/local/boost/boost_1_52_0/boost/libs -L/usr/lib -Lbuild -L. -lboost_thread -lpthread -lboost_regex -lpq -lmylibrary **-lboost_system**
Alternatively, build libmylibrary.so as a shared library and link to the boost_system library directly.

Building a 32-bit app in 64-bit Ubuntu

After hours of googling, I decide to give up and ask you experts. I am trying to build a 32-bit application (xgap if anyone interested) in my 64 Ubuntu 11.10. I added the CFLAGS=-m32 and the LDFLAGS=-L/usr/lib32 in the makefile. The objects are built into 32 bit fine. The last step is to link all the objects and libraries for X windows into this executable---xgap. Somehow it keeps giving me this error:
gcc -o xgap xcmds.o utils.o gapgraph.o gaptext.o pty.o popdial.o xgap.o selfile.o -L/usr/lib32 -lXaw -lXmu -lXt -lXext -lX11 -lSM -lICE
/usr/bin/ld: skipping incompatible /usr/lib32/libXmu.so when searching for -lXmu
...
/usr/bin/ld: i386 architecture of input file `xcmds.o' is incompatible with i386:x86-64 output
...
I have installed ia32-libs and mutilib support. I think I just need to force the linker to generate a i386 output. I tried to put two ld flags in my gcc command as shown above: -melf_i386 and -oformat elf32-i386. But what happens is that gcc doesn't search for the 32 bit library in /usr/lib32 anymore. I wonder if I need to put those flags in some fixed order?
Thanks for any idea and help!
EDIT: when I add the -m32 flag in my last gcc command (the linking stage I believe), even if I have the -L/usr/lib32 flag in place, gcc doesn't search in /usr/lib32 anymore ( really weird...) and generates the following error:
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../libXaw.so when searching for -lXaw
/usr/bin/ld: skipping incompatible /usr/lib/libXaw.so when searching for -lXaw
/usr/bin/ld: cannot find -lXaw
collect2: ld returned 1 exit status
Any one has any idea why this happens? I am using the auto tool to configure and make. I am really good at modifying those script files.
You need to use link with -m32 as well.
gcc -m32 -o xgap xcmds.o utils.o gapgraph.o gaptext.o pty.o popdial.o xgap.o selfile.o -L/usr/lib32 -lXaw -lXmu -lXt -lXext -lX11 -lSM -lICE
All things considered, I think you should be able to drop the -L/usr/lib32 when using -m32.
I solved the problem. I think that gcc was expecting a static library archive. I used the getlibs script from http://ubuntuforums.org/showthread.php?t=474790 to download all the .a archives needed for linking. Then gcc worked. I think gcc did search in /usr/lib32 directory but didn't find the .a archives so went on to search in the standard directory which is /usr/lib, where it finds the incompatible *.so files.
But then the question is: the *.so files in /usr/lib32/ from package ia32-libs doesn't really have the libraries needed for linking? What are those files in /usr/lib32/ used for?

Linking the static version of a library instead of the dynamic one

I am trying to use libjpeg in my application. Making the project yields a libjpeg.a in .libs folder. What I would like to do is to use this file in the linkage stage. I have tried the following: I copied the libjpeg.a into the folder where my C code resides. Trying to link with
gcc libjpeg.a mycode.c -o executable_name
fails. If I do gcc -ljpeg mycode.c, the compilation is successful when I change my header to point to instead of "libjpeg.h", but this obviously links to the system-wide dynamic version of the library.
Trying to link with relative or absolute path also fails:
gcc ./libjpeg.a mycode.c -o executable_name
I have tried the static option as well:
gcc -static libjpeg.a mycode.c -o executable_name
The linker error is the following:
Linking...
gcc -std=c99 -Wall -Wextra -g -pedantic ./libjpeg.a ./libjpeg.a -lm obj/read_jpeg.o obj/utils.o -o test_jpeg
obj/read_jpeg.o: In function `read_JPEG_file':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:37: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:45: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:46: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:47: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:48: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:62: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:74: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:75: undefined reference to `jpeg_destroy_decompress'
obj/read_jpeg.o: In function `read_JPEG_file_props':
/home/ustun/Downloads/jpeg_test/read_jpeg.c:93: undefined reference to `jpeg_std_error'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:100: undefined reference to `jpeg_CreateDecompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:101: undefined reference to `jpeg_stdio_src'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:102: undefined reference to `jpeg_read_header'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:103: undefined reference to `jpeg_start_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:113: undefined reference to `jpeg_read_scanlines'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:116: undefined reference to `jpeg_finish_decompress'
/home/ustun/Downloads/jpeg_test/read_jpeg.c:117: undefined reference to `jpeg_destroy_decompress'
collect2: ld returned 1 exit status
make: *** [test_jpeg] Error 1
You can download a simple project with a Makefile here.
You'd have to give the full path to libjpeg.a, If you have libjpeg.a in a .libs folder relative to where you compile:
gcc mycode.c -o executable_name .libs/libjpeg.a
If your special libjpeg.a is elsewhere, give a full path to it.
If this fails, you have to tell us what happens. (the details are important, so please copy paste the exact errors and the exact command line that is run).
You need to use -static:
gcc -static -o exec_name mycode.c -ljpeg
No need to copy the archive (.a). You could have found out by reading man ld.
This may help you if you have the same problem as mine. In my system, I have
libjpeg.so.62 -> libjpeg.so.62.0.0
libjpeg.so.62.0.0
After I added symbolic link:
sudo ln -s libjpeg.so.62 libjpeg.so
My problem got solved.

Resources