MacOS下链接预编译QuantLib二进制文件时出现Boost符号未定义问题
Let's break down what's causing those undefined symbols errors and how to fix them:
Why This Happens
The missing symbols like boost::assertion_failed belong to Boost's assertion module. Your current compile command specifies the include and library paths for Boost and QuantLib, but it doesn't actually tell the linker to link against the necessary Boost libraries—so the linker can't find those symbols.
Step-by-Step Solutions
1. Use pkg-config for Automatic Dependency Handling
Homebrew-installed QuantLib and Boost should come with .pc files that let pkg-config automatically resolve all required compile and link flags. This is the most reliable way to avoid missing libraries:
clang++ -std=c++11 ql_ex1.cpp -o build/ql_ex1 $(pkg-config --cflags --libs quantlib)
This command will pull in all the correct include paths, library paths, and the exact list of Boost libraries that QuantLib depends on (including boost_assert, boost_system, boost_thread, etc.).
2. Manually Link Required Boost Libraries
If you prefer to stick with manual paths, you need to add the specific Boost libraries that QuantLib uses. At minimum, you'll need to link against libboost_assert plus other common QuantLib dependencies. Update your compile command like this:
clang++ -std=c++11 ql_ex1.cpp -o build/ql_ex1 \ -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 \ -lQuantLib -lboost_assert -lboost_system -lboost_thread -lboost_date_time
The -l flag tells the linker to link against the specified library (e.g., -lboost_assert maps to libboost_assert.dylib or libboost_assert.a).
3. Verify Boost Library Architecture
Make sure your installed Boost libraries are built for arm64 (M1's architecture). Run this command to check:
lipo -info /opt/homebrew/Cellar/boost/1.76.0/lib/libboost_assert.dylib
You should see output like Non-fat file: ... is architecture: arm64. If it shows x86_64, you might have a conflicting installation—reinstall Boost via Homebrew to get the arm64 version.
Why Your Previous Fixes Didn't Work
- Using x86 emulation (Rosetta) won't help because the issue is missing linked libraries, not architecture mismatch (unless your Boost/QuantLib were installed for x86, but Homebrew on M1 defaults to arm64).
- The
assertion_failedsymbols are inlibboost_assert.aorlibboost_assert.dylib—you might have missed it because it's a small, focused library compared to larger Boost components.
内容的提问来源于stack exchange,提问作者Philip Maybank

