【问题标题】:Cant link boost library with g++ Windows无法将 boost 库与 g++ Windows 链接
【发布时间】: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 并构建了我需要的库。它有效!

标签: c++ windows gcc boost g++


【解决方案1】:

Visual Studio 用于 boost 的二进制文件与 MinGW 不兼容。您将需要使用您的工具链生成的 boost 二进制文件。虽然许多c 库可以使用由其他c 编译器生成的二进制文件,但c++ 库通常不兼容。

以下问题解释了c++中ABI不兼容的困难: Easy way to guarantee binary compatibility for C++ library, C linkage?

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多