我花了一整天的时间尝试将 data_time
库链接到我的 C++ cmake
项目。
所以我正在使用 cmake 3.4
和 boost 1.61.0
。
我有一个类,其中是一个正在使用本地时间的函数:
void TestClass::getTime() {
this->endDate = boost::posix_time::second_clock::local_time();
}
然后我想通过返回字符串的函数返回endDate
的值:
string testCalss::Info() {
return to_simple_string(this->beginningDate);
}
我需要将endDate
变量的类型转换为string
,因为函数是string类型。
我在程序链接时收到错误消息:
In function boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::simple_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
我读到 data_time 不仅是 header 库,我应该构建它并将其添加到我的项目中。
我已经尝试过这个命令 gcc myapp.cpp -omyapp -lboost_date_time
,但它不起作用,因为我在 cmake
中使用 g++
项目,我还没有找到 g++ 的任何东西。
我也试过这个:
c++ -I path/to/boost_1_61_0 example.cpp -o example \
~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a
它来自官方 boost 文档的示例如何链接库。
在 cmake
项目中,我有几个 cpp
文件。我应该使用我的转换命令在哪里的文件运行此命令?
有没有更简单的方法来修复这个错误?
最佳答案
我找到了解决问题的方法。我选择的方式很简单。首先,我使用了 locate */boost/date_time*
命令来查找我的 boost
和 date_time
库的安装位置。我不知道我使用的是什么版本的 boost 库,所以我再次使用 locate
命令找到 boost/version.hpp
文件。之后,我在 CmakeLists.txt
中添加了几行:
find_package( Boost 1.60.0 COMPONENTS date_time)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries( Program ${Boost_DATE_TIME_LIBRARY} )
endif()
就是这样。
关于c++ - Cmake 对 `boost::gregorian::greg_month::as_short_string() const' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126624/