我使用CMake构建项目,并使用Catch2进行测试。以下是我的项目结构
├── build
├── CMakeLists.txt
├── compile_commands.json
├── include
│ ├── node.h
│ ├── rrt.h
│ └── tree.h
├── Makefile
├── package.xml
├── scripts
├── src
│ ├── main.cpp
│ ├── node.cpp
│ ├── rrt.cpp
│ └── tree.cpp
├── test
│ ├── CMakeLists.txt
│ └── test.cpp
└── third_party
└── catch.hpp
在./CMakeLists.txt
中:
cmake_minimum_required(VERSION 2.8.3)
project(rrt_ros)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/include/rrt.h
${CMAKE_CURRENT_SOURCE_DIR}/include/tree.h
${CMAKE_CURRENT_SOURCE_DIR}/include/node.h
${CMAKE_CURRENT_SOURCE_DIR}/src/rrt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/tree.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/node.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs)
find_package(Armadillo REQUIRED)
FIND_PACKAGE(Eigen3 REQUIRED)
add_executable(
rrt
${SOURCES}
)
target_include_directories(
rrt
PUBLIC
${catkin_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIR}
${ARMADILLO_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(rrt ${catkin_LIBRARIES} ${ARMADILLO_LIBRARIES})
add_subdirectory(test)
在test/CMakeLists.txt
中,我有
project(rrt_ros)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs)
find_package(Armadillo REQUIRED)
FIND_PACKAGE(Eigen3 REQUIRED)
message("TESTING......" ${CMAKE_CURRENT_SOURCE_DIR}/../include/rrt.h)
add_executable(rrt_test test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../include/rrt.h
${CMAKE_CURRENT_SOURCE_DIR}/../include/tree.h
${CMAKE_CURRENT_SOURCE_DIR}/../include/node.h
${CMAKE_CURRENT_SOURCE_DIR}/../src/rrt.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/tree.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/node.cpp
)
target_include_directories(
rrt_test
PUBLIC
${catkin_INCLUDE_DIRS}
${Armadillo_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../third_party
${CMAKE_CURRENT_SOURCE_DIR}/../include
)
target_link_libraries(
rrt_test
${catkin_LIBRARIES}
${Aramdillo_LIBRARIES}
)
在rrt.cpp
中,我有一个函数调用犰狳的采样函数
arma::randi<arma::mat>(1, 2, arma::distr_params(1, 10))
当我编译项目时,它给了我一个未定义的错误:
rrt.cpp:(.text._ZTWN4arma23arma_rng_cxx11_instanceE[_ZTWN4arma23arma_rng_cxx11_instanceE]+0x15): undefined reference to 'arma::arma_rng_cxx11_instance'
collect2: error: ld returned 1 exit status
但是,当我在${CMAKE_CURRENT_SOURCE_DIR}/../src/rrt.cpp
中注释掉./test/CMakeLists.txt
时,它可以正常编译。