开发者

Problem with libraries and Mingw-gcc to cross-compile Win32 code on Linux

I am attempting to cross-compile some C/C++ programs I've written on Linux for Windows. I've had a lot of experience using both GCC and MSVC.

Normally when compiling a program using gcc, one would use the -l linker argument to specify a library to link against. It seems that this does not work properly when using the MingW-gcc linker.

GCC version: i586-mingw32msvc-gcc (GCC) 4.4.4

When linking a program that uses (for example, GTK+ or libpng, libz, etc.), using something like:

i586-mingw32msvc-gcc -mwindows -L/opt/xcompile-win32/lib -lmylib -lmylib2 myprog.o -o myprog.exe

Gives numerous errors about undefined references to the library functions. However, if I specify the libraries as additional objects like this:

i586-mingw32msvc-gcc -mwindows -L/opt/xcompile-w开发者_高级运维in32/lib /opt/xcompile-win32/lib/libmylib.a /opt/xcompile-win32/lib/libmylib2.a myprog.o -o myprog.exe

Everything works fine and the resulting program works great! My question is: Is there a way I can get the normal -l library arguments to work correctly? This method seems a bit cumbersome! I can't seem to find anything online which addresses this issue. Thanks!

Edit:

To clarify: the order of the library arguments on the command-line makes no difference. Also, the program compiles just fine on Linux (gcc) using only the -l arguments. Actual command below:

i586-mingw32msvc-gcc  -mwindows -o win32/vmclient.exe win32/gtk_test.o \
-L/opt/xcompile-win32/lib -latk-1.0 -lpangoft2-1.0 -lpangocairo-1.0 \
-lgdk_pixbuf-2.0 -lm -lcairo -lpng12 -lpango-1.0 -lfreetype -lfontconfig \
-lgmodule-2.0 -lgthread-2.0 -lglib-2.0

win32/gtk_test.o:gtk_test.c:(.text+0x37): undefined reference to `_gtk_init_abi_check'

(and a bunch of similar errors). The program compiles fine for Win32 (using i586-mingw32msvc-gcc) when directly referencing the .a library files in the exact same order.


The problem here is in the order of the command line arguments. If library linked with the -l the linker will only take the object modules needed to satisfy any unresolved referece seen so far. From info ld, option -l:

If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

But when you write the full path of the library it is linked wholly.

So to compile your program simply write:

i586-mingw32msvc-gcc -mwindows myprog.o -o myprog.exe \
    -L/opt/xcompile-win32/lib -lmylib -lmylib2 

With the libraries at the end.

BTW, if I'm not mistaken this behavior is no different in the native linux compiler.

Edit:

Replying to your edit, you forgot to add a few libraries to your compiler command. You are missing -lgtk-win32-2.0 -lgdk-win32-2.0.

Personally I find it much more convenient to use the pkg-config tool. When cross-compiling it is just as easy as exporting PKG_CONFIG_LIBDIR=/op/xcompile-win32/pkgconfig.

In linux it works fine maybe because the missing libraries are brought in automatically by NEEDED records in the other libraries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜