首页 > 解决方案 > 编译包含boost库和google测试的项目时出现问题

问题描述

我有一个使用 boost::gregorian::date 的项目。我想使用 gtest 库添加一些单元测试。到目前为止,我刚刚将 gtest 库添加到我的项目中,但我还没有添加任何测试。当我尝试编译我的项目时,我遇到了一些链接器错误。当我在 Makefile (当然,在其他文件中)与 boost 相关的行中评论时,我没有错误。当我在与 gtest 相关的 Makefile 行中评论时,我只收到警告:

warning: /usr/lib/x86_64-linux-gnu/libboost_date_time.so: not a directory

但该程序按我的意愿工作。

当我尝试使用 boost 和 gtest 库编译代码时,出现错误:

cc1plus: error: /usr/lib/x86_64-linux-gnu/libboost_date_time.so: not a directory [-Werror]
cc1plus: all warnings being treated as errors
lib/googletest-master/googletest/CMakeFiles/gtest.dir/build.make:62: recipe for target 'lib/googletest-master/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o' failed
make[3]: *** [lib/googletest-master/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o] Error 1
CMakeFiles/Makefile2:282: recipe for target 'lib/googletest-master/googletest/CMakeFiles/gtest.dir/all' failed
make[2]: *** [lib/googletest-master/googletest/CMakeFiles/gtest.dir/all] Error 2
CMakeFiles/Makefile2:85: recipe for target 'CMakeFiles/planner.dir/rule' failed
make[1]: *** [CMakeFiles/planner.dir/rule] Error 2
Makefile:164: recipe for target 'planner' failed
make: *** [planner] Error 2

这是我的Makefile:

cmake_minimum_required(VERSION 3.10)
project(planner)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-std=c++14 -Wall -Wextra -pedantic")

add_executable(planner [some files])

#boost
find_package(Boost)

if (NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find boost!")
endif ()

if (Boost_FOUND)
    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")
    set(BOOST_INCLUDE_DIRS "/usr/include")
    set(BOOST_LIBRARY_DIRS "/usr/lib")
    find_package(Boost REQUIRED date_time)
    include_directories(${Boost_INCLUDE_DIRS})
    include_directories(${Boost_LIBRARIES})
    target_link_libraries(planner ${Boost_LIBRARIES})
endif ()
#end boost

#gtest
add_subdirectory(lib/googletest-master)
include_directories(lib/googletest-master/googletest/include)
include_directories(lib/googletest-master/googlemock/include)
target_link_libraries(planner gtest gtest_main)
#end gtest


#cppconn
target_link_libraries(planner mysqlcppconn)

我正在使用 CLion 2019.1.4。你能帮我解决这个问题吗?非常感谢您的任何帮助。

标签: c++boostcmakegoogletest

解决方案


推荐阅读