Cmake未定义对`boost :: gregorian :: greg_month :: as_short_string()const'的引用

时间:2017-11-05 21:04:26

标签: c++ boost

我花了一整天时间尝试将data_time库链接到我的c ++ cmake项目。

所以我使用cmake 3.4boost 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,因为函数是字符串类型。

我在程序链接时收到错误消息:

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不仅是仅限标题的库,我应该构建并将其添加到我的项目中。

我已经尝试过这个命令gcc myapp.cpp -omyapp -lboost_date_time,但它不起作用,因为我在g++项目中使用cmake而我没有找到g ++的任何内容。

我也试过这个:

c++ -I path/to/boost_1_61_0 example.cpp -o example \
   ~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a

它的例子来自官方提升文档如何链接库。 在cmake项目中,我只有cpp个文件。我应该使用file where where conversion命令运行此命令吗?

有没有更简单的方法来解决此错误?

1 个答案:

答案 0 :(得分:0)

我发现了如何解决这个问题。我选择的方式很简单。首先,我使用locate */boost/date_time*命令查找我的boostdate_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()

多数民众赞成。