在 MacOS 上链接到预编译的 QuantLib 二进制文件时未定义的 Boost 符号

Undefined Boost symbol when linking to pre-compiled QuantLib binaries on MacOS

我正在尝试编译一些依赖于 QuantLib 的 C++,而后者又依赖于 Boost。我正在研究 MacBook Pro M1(ARM 架构)。我使用此页面上的说明安装了 Boost 和 QuantLib:https://www.quantlib.org/install/macosx.shtml。然后我尝试编译以下源代码:

#include <iostream>
#include <ql/quantlib.hpp>
 
int main(int, char*[])
{
    QuantLib::Option::Type OptionType(QuantLib::Option::Call);
    std::cout << "Option Type = " << OptionType << std::endl;
    
    return 0;
}

使用以下命令:

clang++ -std=c++11 ql_ex1.cpp -o build/ql_ex1 $(INC) $(LIB) \
    -I/opt/homebrew/Cellar/quantlib/1.23/include -I/opt/homebrew/Cellar/boost/1.76.0/include \
    -L/opt/homebrew/Cellar/boost/1.76.0/lib -L/opt/homebrew/Cellar/quantlib/1.23/lib

这给了我以下错误信息:

Undefined symbols for architecture arm64:
  "boost::assertion_failed(char const*, char const*, char const*, long)", referenced from:
      long double boost::math::detail::sinpx<long double>(long double) in ql_ex1-044d3e.o
  "boost::assertion_failed_msg(char const*, char const*, char const*, char const*, long)", referenced from:
      boost::array<long double, 171ul>::operator[](unsigned long) const in ql_ex1-044d3e.o
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1

到目前为止,我已经尝试了以下方法,

谁能指出这个符号的定义位置(如果有的话)?了解我是否只是缺少正确的编译器选项或者是否有更基本的问题需要解决会很有用。

我认为您的命令行中缺少 -lQuantLib?您是在告诉编译器在哪里查找库(-L 开关`),而不是要 link.

的库

此外,请检查 homebrew 是否也在您的计算机上安装了 quantlib-config 脚本。如果是这样,它应该提供您需要的开关;尝试 运行

quantlib-config --cflags

应该输出指定 QuantLib headers 位置的 -I 标志(可能还有您手动传递的 -std=c++11 标志),以及

quantlib-config --libs

应该输出相应的 -L 开关以及您错过的 -lQuantLib

如果它们有效,您可以使用命令

clang++ ql_ex1.cpp -o build/ql_ex1 $(INC) $(LIB) \
-I/opt/homebrew/Cellar/boost/1.76.0/include \
`quantlib-config --cflags` `quantlib-config --libs`

然后让脚本为您填写信息。