3
votes

I am trying to build an image processing program written in C++ that depends on the following libraries using MinGW + MSYS (with GCC4.8.1) that I downloaded from www.mingw.org/ on a Windows 8 64bit computer

LibJPEG

BLAS and LAPACK

Armadillo

OpenMP

I have successfully compiled all the source code files (with -fopenmp flag of course), then I linked with the following statement:

g++ -o ./build/rspfitter {a list of .o files} -L{paths to libraries} -ljpeg -lopenblas -lgomp -lpthread

The executable was correctly produced. However, it asks for the following dlls:

libgomp-1.dll

libpthread-2.dll

pthreadGC2.dll

I think it might a better idea to link libgomp and libpthread statically, so that I can minimize the number of dlls I need to deploy my program with (the above three dlls are not the only ones the program depends on). So I tried to link only libgomp and libpthread statically with the following command:

g++ -o ./build/rspfitter {a list of .o files} -L{paths to libraries} -ljpeg -lopenblas -Wl,-static -lgomp -lpthread

But this time it fails with the following error message:

d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0xbfe): undefined reference to `_imp__pthread_attr_init'

d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0xc13): undefined reference to `_imp__pthread_attr_setdetachstate'

d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o):(.text.startup+0x3c): undefined reference to `_imp__pthread_attr_setstacksize'

d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:

d:/mingw/bin/../lib/gcc/mingw32/4.8.1\libgomp.a(env.o): bad reloc address 0x0 in section `.ctors'

d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:

final link failed: Invalid operation

Then I tried to execute the exact same compiling and linking commands using the MinGW + GCC 4.8.1 environment that was installed together with CodeLite. It fails again with different error messages:

./tmp/hshfitcmdline.o:hshfitcmdline.cpp:(.text.unlikely+0x105): undefined reference to `_Unwind_Resume'

./tmp/hshfitcmdline.o:hshfitcmdline.cpp:(.text$_ZN9NormalMapD1Ev[__ZN9NormalMapD1Ev]+0xb4): undefined reference to `_Unwind_Resume'

d:/mingw-4.8.1/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:

./tmp/hshfitcmdline.o: bad reloc address 0xb4 in section `.text$_ZN9NormalMapD1Ev[__ZN9NormalMapD1Ev]'

d:/mingw-4.8.1/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:

final link failed: Invalid operation collect2.exe: error: ld returned

1 exit status make: *** [build/rspfitter] Error 1

I confirmed that the file "libgomp.a"/"libgomp.dll.a" was present on the [MinGW dir]/lib/gcc/mingw32/4.8.1/ for both installations of MinGW. However, they are of different sizes! In installation downloaded from MinGW.org, 'libgomp.a' is of 86kb, and "libgomp.dll.a" is of 87kb; In the CodeLite installation, however, the sizes are 74kB and 148Kb respectively.

Now I am wondering:

  1. What could be the cause of the error messages given by the two MinGW systems? Could it be that the statically libraries that I downloaded from MinGW are corrputed? But dynamically linking was perfectly fine on both systems.

  2. How on earth can I correctly link libgomp statically?

Thanks

1

1 Answers

2
votes

To link libgomp statically you can do

ln -s `g++ --print-file-name=libgomp.a` && \
g++ foo.o -static-libgcc -static-libstdc++ -L. -o foo -fopenmp -ljpeg -lopenblas

However your executable will still depend on a pthread dll. The reason you are getting the error is that libc is still linking dynamically. To fix this you have to link libc statically as well

ln -s `g++ --print-file-name=libpthread.a` && \
ln -s `g++ --print-file-name=libc.a` && \
g++ foo.o -static-libgcc -static-libstdc++ -L. -o foo -fopenmp -ljpeg -lopenblas

However, if openblas or jpeg libraries depend on libc then there will likely still be undefined references.