首页 > 解决方案 > 关于 Boost 和架构的编译错误

问题描述

我有一个简单的程序,使用 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

我不明白make在尖叫什么。此错误是否实际上暗示了输出中未描述的其他内容?

标签: c++macosc++11boostclang++

解决方案


推荐阅读