CMake 如何正确包含我自己的 headers?

CMake How to properly include my own headers?

我正在使用 ros,现在我想编写自己的单元测试。但我正在努力。也许你对我有什么建议?或者可能有解决方案?

my structure:
-catkin_ws
--build
--install
--src
----project1
----CMakeLists.txt
----package.xml
------include
--------prime_tables.h
--------sample1.h
------src
--------sample1.cpp
------tests
--------sample1_unittest.cpp

我的cmakelist.txt:

cmake_minimum_required(VERSION 2.8.3)
project(project1)

find_package(catkin REQUIRED COMPONENTS roscpp rostest rosunit)
find_package(sample1)



catkin_package(
   INCLUDE_DIRS include
   LIBRARIES ${PROJECT_NAME}
   CATKIN_DEPENDS roscpp rostest
   DEPENDS  )
include_directories(include ${catkin_INCLUDE_DIRS})


if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(project1 tests/sample1_unittest.cpp)

  add_executable(add_test tests/sample1_unittest.cpp)
  add_library(add include/sample1.h src/sample1.cpp)
  target_link_libraries(add_test ${catkin_LIBRARIES})
  target_link_libraries(add_test gtest)
  target_link_libraries(add_test prime_tables)
  target_link_libraries(add_test sample1)
endif()

我收到此错误:

[87%] Linking CXX exectuable

/usr/bin/ld -lprime_tables cannot find
/usr/bin/ld -lsample1 cannot find

问题是什么?你说吧,我对CMake不是很熟悉。

sample1.cpp 中是您想要并测试的库,并且您想创建链接到该库并运行单元测试的可执行文件?

一些基本的 cmake 信息: add_libraryadd_executabletarget_link_libraries 的第一个参数是 library/executable 的名称。因此,如果您将链接库指定给此库或可执行文件,则必须再次使用此名称。

并查看此处了解更多信息: http://wiki.ros.org/catkin/CMakeLists.txt http://docs.ros.org/jade/api/catkin/html/howto/format2/gtest_configuration.html

我的猜测是,您想要这样的 cmake:

cmake_minimum_required(VERSION 2.8.3)
project(project1)

catkin_package(
  INCLUDE_DIRS include
  LIBRARIES ${PROJECT_NAME}
  CATKIN_DEPENDS roscpp rostest
  DEPENDS
)
include_directories(include 
  ${catkin_INCLUDE_DIRS}
)

add_library(${PROJECT_NAME}_sample1 
  src/sample1.cpp
)
target_link_libraries(
  ${PROJECT_NAME}_sample1
  ${catkin_LIBRARIES}
)

if (CATKIN_ENABLE_TESTING)
  catkin_add_gtest(${PROJECT_NAME}_test 
    tests/sample1_unittest.cpp
  )
  target_link_libraries(${PROJECT_NAME}_test 
    ${PROJECT_NAME}_sample1
    ${catkin_LIBRARIES}
  )
endif()