简体   繁体   English

在Windows和Linux上的Mingw中使用boost / asio进行故障排除

[英]Trouble building using boost/asio in mingw, both on windows and on linux

What I'm trying to do 我想做什么

I'm trying to build a simple communication between an ARM development board and a PC, using boost/asio. 我正在尝试使用boost / asio在ARM开发板和PC之间建立简单的通信。 I want to build the PC side as a 32 bit DLL, as the tool that will use it is 32 bit only. 我想将PC端构建为32位DLL,因为使用它的工具仅是32位。
For now I'm trying to get the build to work, so my files don't actually contain much code, but import the relevant libraries. 现在,我正在尝试使构建正常工作,因此我的文件实际上并不包含太多代码,而是导入相关的库。 Ultimately I want to build on linux, so I compiled boost from source using mingw-w64 on there. 最终,我想在Linux上构建,因此我在那里使用mingw-w64从源代码编译了boost。 However, for now I'm trying to get it to build on windows, using msys2 and mingw32. 但是,现在我正在尝试使用msys2和mingw32在Windows上构建它。 I installed the mingw32/mingw-w64-i686-boost 1.64.0-3 package. 我安装了mingw32/mingw-w64-i686-boost 1.64.0-3软件包。

For now I haven't actually included any code that uses boost, I merely imported it with #include <boost/asio.hpp> in pc/McBSP.cpp. 到目前为止,我实际上还没有包含任何使用boost的代码,我只是在pc / McBSP.cpp中使用#include <boost/asio.hpp>导入了它。

The Problem 问题

When I'm trying to compile it I get undefined reference to `boost::system::generic_category()' errors, even though I have included -lboost_system in my options. 当我尝试编译它时,即使我在选项中包含了-lboost_system ,也得到了undefined reference to `boost::system::generic_category()'错误的undefined reference to `boost::system::generic_category()'

I'm building in the mingw32 shell, Full output of an attempted build: 我在mingw32 shell中构建,尝试构建的完整输出:

$ make pc32
g++ -m32 -DBUILDING_DLL -I board -I common -I pc -c -o common/Addresses-w64-i686.o common/Addresses.cpp
g++ -m32 -DBUILDING_DLL -I board -I common -I pc -c -o common/Audio_Preset-w64-i686.o common/Audio_Preset.cpp
SHA=fe69ff6
echo -n "static const char *GIT_COMMIT_TAG = \"fe69ff6\";" > common/version.h
g++ -m32 -DBUILDING_DLL -I board -I common -I pc -static -lwsock32 -lws2_32 -L/mingw32/lib -lboost_system-mt -c -o pc/McBSP_PC-w64-i686.o pc/McBSP_PC.cpp
g++ -m32 -DBUILDING_DLL -I board -I common -I pc -static -lwsock32 -lws2_32 -L/mingw32/lib -lboost_system-mt common/Addresses-w64-i686.o common/Audio_Preset-w64-i686.o \
    pc/McBSP_PC-w64-i686.o -o McBSP_PC-w64-i686.dll
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text+0xc4): undefined reference to `boost::system::generic_category()'
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text+0xce): undefined reference to `boost::system::generic_category()'
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text+0xd8): undefined reference to `boost::system::system_category()'
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text$_ZN5boost4asio5error19get_system_categoryEv[__ZN5boost4asio5error19get_system_categoryEv]+0x7): undefined reference to `boost::system::system_category()'
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh[__ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh]+0x6a): undefined reference to `_imp__WSAStartup@8'
pc/McBSP_PC-w64-i686.o:McBSP_PC.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE[__ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE]+0x30): undefined reference to `_imp__WSACleanup@0'
C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/../../../../i686-w64-mingw32/lib/../lib\libmingw32.a(lib32_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:124: McBSP_PC-w64-i686.dll] Error 1

All questions I found on this suggest including -lboost or -lboost_system, which I already have. 我对此提出的所有问题都建议包括-lboost或-lboost_system,它们已经存在。 LD finds boost_system(-mt) just fine, but somehow it seems like it doesn't satisfy the depencendy? LD认为boost_system(-mt)很好,但是似乎不满足于依赖吗?

Update 更新资料

I got it to compile with the following changes: 我将其编译为以下更改:

  • I put the libraries at the END of the call, after the object files. 我将库放在调用的末尾,在目标文件之后。
  • I changed -static to -shared. 我将-static更改为-shared。

But I am unsure if this DLL now requires boost on the target system? 但是我不确定此DLL现在是否需要在目标系统上增强功能? I am under the impression that -shared means the boost lib is now not included in the dll. 我的印象是-shared意味着boost lib现在不包含在dll中。

I got it to work. 我知道了。 Here is how: 方法如下:

Make it build 建立它

I needed to move the linker flags after the object files. 我所需要的目标文件 ,移动连接标志。 That was all it took to get rid of the boost::system errors. 这就是摆脱boost::system错误所需要的。 That introduced a new error, ld could not find WinMain. 这就引入了一个新错误,ld找不到WinMain。 That was fixed by changing -static to -shared , as I wanted to build a shared library which has no main function. 通过将-static更改为-shared ,因为我想构建一个没有主要功能的共享库。

Making it static 使其静态

Now the problem arose, that PCs I wanted to run this on needed to have the libraries I used, so I needed to tell the linker to link those statically. 现在出现了问题,我想要在上面运行的PC需要拥有我使用的库,因此我需要告诉链接器静态链接这些库。 I achieved that with these linker flags: 我通过这些链接器标志实现了这一点:

-static-libstdc++ -static-libgcc -Wl,-Bstatic,--whole-archive -lpthread \
    -Wl,--no-whole-archive -L/mingw32/lib -Wl,-Bstatic \
    -lboost_system-mt -lws2_32 -lwsock32

Now it could build the DLL on windows. 现在它可以在Windows上构建DLL。 Porting this build system to my linux environment was now achieved by exchanging g++ for i686-w64-mingw32-g++ and changing -L/mingw32/lib to the path where I installed the cross-compiled boost lib to. 现在,通过将g++ i686-w64-mingw32-g++i686-w64-mingw32-g++并将-L/mingw32/lib更改为我将交叉编译的boost lib安装到的路径,可以将该构建系统移植到我的linux环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM