【发布时间】:2020-06-22 10:15:10
【问题描述】:
我正在尝试构建以下代码:
“Source.cpp”的内容
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
path myPath("foo");
if (exists(myPath)) ...
}
我用来编译它的命令是:
g++ -Wall -I D:\boost_1_72_0 Source.cpp -o test -L D:\boost_1_72_0\stage\lib -lboost_filesystem-vc142-mt-gd-x32-1_72
但我得到默认未解决的符号错误:
C:\Users\User\AppData\Local\Temp\ccskBqAh.o:Source.cpp:(.text$_ZN5boost10filesystem11path_traits7convertEPKcS3_RNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEE[__ZN5boost10filesystem11path_traits7convertEPKcS3_RNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEE]+0x7): undefined reference to `boost::filesystem::path::codecvt()'
... etc
不过用Visual Studio编译没问题。
附: “-L”和“-I”后面的空格是允许的。而且我试图链接不同的库。对于共享,我还使用了#define BOOST_ALL_DYN_LINK 以防万一。
共享:
boost_filesystem-vc142-mt-gd-x32-1_72
boost_filesystem-vc142-mt-gd-x64-1_72
boost_filesystem-vc142-mt-x32-1_72
boost_filesystem-vc142-mt-x64-1_72
静态:
libboost_filesystem-vc142-mt-gd-x32-1_72
libboost_filesystem-vc142-mt-gd-x64-1_72
libboost_filesystem-vc142-mt-sgd-x32-1_72
libboost_filesystem-vc142-mt-sgd-x64-1_72
libboost_filesystem-vc142-mt-s-x32-1_72
libboost_filesystem-vc142-mt-s-x64-1_72
libboost_filesystem-vc142-mt-x64-1_72
【问题讨论】:
-
vc142中的boost_filesystem-vc142-mt-gd-x32不是意味着这个库是为 MSVC 构建的吗?如果是这样,它与 MinGW 不兼容。你需要一个为 MinGW 构建的版本。 -
您需要使用您正在使用的编译器构建 boost。 Visual Studio 二进制文件没有用。
-
我建议安装 MSYS2。作为奖励,您将获得预构建的 Boost 和新版本的 GCC。
-
这些是 Visual Studio 2019 库(对大多数用户免费!)。您将需要获取或构建自己的,从源代码构建 Boost 并不难,有 step by step instructions on the site,尽管 Boost 似乎并未正式支持 MSYS。
-
谢谢,我选择了 toolset=gcc 并构建了我需要的库。它有效!