基于Boost库的x86_64架构编译链接错误求助
问题描述
我编写了一个使用Boost库实现当前线程暂停功能的简单程序,代码如下:
#include <iostream> #include <boost/thread.hpp> #include <boost/chrono.hpp> int main(int argc, char* argv[]) { boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); return 0; }
我的Makefile已配置Boost头文件和库的路径:
OS = $(shell uname) CXX = clang++ CPPFLAGS += -Wall CPPFLAGS += -Wextra CXXFLAGS += -std=c++11 CXXFLAGS += -fno-exceptions ifeq ($(OS), Darwin) INC += -I/Users/bp/programming/boost_1_67_0 endif INC += # Sources SRCS = $(shell find . -name '*.cpp') # Objects # OBJS = $(patsubst %.cpp,%.o,$(SRCS)) # # Dependencies # DEPS = $(patsubst %.cpp,%.ddd,$(SRCS)) ifeq ($(OS), Darwin) LDLIBS = -lboost_thread -lpthread LDFLAGS += -L/Users/bp/programming/boost_1_67_0/stage/lib endif LDLIBS += LDFLAGS += STRIP = strip -s TARGET = test_prog all: $(TARGET) $(TARGET): $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC) $(SRCS) -o $(TARGET) $(STRIP) $(TARGET) clean: rm -rf $(TARGET)
执行make后得到如下输出:
2 warnings generated. Undefined symbols for architecture x86_64: "boost::throw_exception(std::exception const&)", referenced from: boost::mutex::mutex() in main-a1c10d.o boost::unique_lock<boost::mutex>::lock() in main-a1c10d.o boost::mutex::lock() in main-a1c10d.o boost::condition_variable::condition_variable() in main-a1c10d.o boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::real_platform_timepoint const&) in main-a1c10d.o boost::unique_lock<boost::mutex>::unlock() in main-a1c10d.o "boost::chrono::steady_clock::now()", referenced from: bool boost::condition_variable::wait_for<long long, boost::ratio<1l, 1000l>, bool (*)()>(boost::unique_lock<boost::mutex>&, boost::chrono::duration<long long, boost::ratio<1l, 1000l> > const&, bool (*)()) in main-a1c10d.o bool boost::condition_variable::wait_until<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> >, bool (*)()>(boost::unique_lock<boost::mutex>&, boost::chrono::time_point<boost::chrono::steady_clock, boost::chrono::duration<long long, boost::ratio<1l, 1000000000l> > > const&, bool (*)()) in main-a1c10d.o "boost::system::generic_category()", referenced from: boost::system::error_category::std_category::equivalent(int, std::__1::error_condition const&) const in main-a1c10d.o boost::system::error_category::std_category::equivalent(std::__1::error_code const&, int) const in main-a1c10d.o boost::thread_exception::thread_exception(int, char const*) in main-a1c10d.o boost::condition_error::condition_error(int, char const*) in main-a1c10d.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [test_prog] Error 1
我无法理解该错误信息,请问该错误是否隐含输出中未明确说明的问题?
错误分析与解决
这些错误本质是链接器无法找到Boost库的某些依赖符号,输出里已经明确列出了缺失的符号,但背后有两个隐含的核心问题需要解决:
1. 缺失必要的Boost库依赖
你的Makefile里只链接了-lboost_thread和-lpthread,但从错误信息看,程序还依赖另外两个关键Boost模块:
boost::chrono:对应boost::chrono::steady_clock::now()这类符号,需要链接-lboost_chronoboost::system:对应boost::system::generic_category()这类符号,需要链接-lboost_system
2. -fno-exceptions与Boost的兼容性冲突
你在CXXFLAGS里添加了-fno-exceptions禁用C++异常,但Boost的很多组件(比如mutex、condition_variable)内部会调用boost::throw_exception抛出异常。当禁用异常时,必须额外定义宏BOOST_NO_EXCEPTIONS,让Boost适配无异常环境,避免生成依赖异常处理的代码。
修改后的关键配置
针对Darwin系统,更新Makefile的相关部分:
CXXFLAGS += -std=c++11 CXXFLAGS += -fno-exceptions CXXFLAGS += -DBOOST_NO_EXCEPTIONS # 添加宏适配无异常环境 ifeq ($(OS), Darwin) LDLIBS = -lboost_thread -lboost_chrono -lboost_system -lpthread # 补充缺失的库 LDFLAGS += -L/Users/bp/programming/boost_1_67_0/stage/lib endif
另外,建议检查你编译Boost库时是否启用了thread、chrono、system模块,并且确保Boost的编译选项和你的项目一致(比如同样用clang++编译、目标架构为x86_64)。如果是预编译的Boost库,要确认版本和架构匹配;如果是自行编译的,确保编译时没有遗漏这些模块。
内容的提问来源于stack exchange,提问作者xBACP
相关产品推荐
相关产品推荐

