在 Linux 上交叉编译 Win32 代码的库和 Mingw-gcc 的问题
我正在尝试交叉编译我在 Linux 上为 Windows 编写的一些 C/C++ 程序。我在使用 GCC 和 MSVC 方面拥有丰富的经验。
通常,当使用 gcc 编译程序时,会使用 -l 链接器参数来指定要链接的库。使用 MingW-gcc 链接器时,这似乎无法正常工作。
海湾合作委员会版本: i586-mingw32msvc-gcc (GCC) 4.4.4
当链接使用(例如 GTK+ 或 libpng、libz 等)的程序时,使用如下内容:
i586-mingw32msvc-gcc -mwindows -L/opt/xcompile-win32/lib -lmylib -lmylib2 myprog.o -o myprog.exe
给出大量有关库函数未定义引用的错误。但是,如果我将库指定为附加对象,如下
所示: i586-mingw32msvc-gcc -mwindows -L/opt/xcompile-win32/lib /opt/xcompile-win32/lib/libmylib.a /opt/xcompile- win32/lib/libmylib2.a myprog.o -o myprog.exe
一切正常,生成的程序运行得很好!我的问题是:有没有办法让正常的 -l 库参数正常工作?这个方法好像有点麻烦!我似乎无法在网上找到任何解决此问题的内容。谢谢!
编辑:
澄清一下:命令行上库参数的顺序没有区别。此外,该程序仅使用 -l 参数就可以在 Linux (gcc) 上正常编译。实际命令如下:(
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'
以及一堆类似的错误)。当以完全相同的顺序直接引用 .a 库文件时,该程序可以很好地针对 Win32(使用 i586-mingw32msvc-gcc)进行编译。
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-win32/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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题在于命令行参数的顺序。如果库与
-l
链接,链接器将仅获取满足迄今为止看到的任何未解析引用所需的对象模块。来自info ld
,选项-l
:但是,当您编写库的完整路径时,它是完全链接的。
因此,要编译您的程序,只需编写:
将库放在末尾。
顺便说一句,如果我没记错的话,这种行为在本机 Linux 编译器中没有什么不同。
编辑:
回复您的编辑时,您忘记向编译器命令添加一些库。您缺少
-lgtk-win32-2.0 -lgdk-win32-2.0
。就我个人而言,我发现使用
pkg-config
工具更方便。交叉编译时就像导出 PKG_CONFIG_LIBDIR=/op/xcompile-win32/pkgconfig 一样简单。在 Linux 中,它工作得很好,可能是因为缺少的库是由其他库中的 NEEDED 记录自动引入的。
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. Frominfo ld
, option-l
:But when you write the full path of the library it is linked wholly.
So to compile your program simply write:
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 exportingPKG_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.