0

我无法将外部库与我使用 CMake 编写的库链接。我想知道是否需要将某些内容添加到我的CMakeLists.txt? 或者,如果我需要CMakeLists.txt在较低级别(内部src)添加另一个,那需要包含什么?

我有以下项目结构:

ProjectFolder
│   ├── CMakeLists.txt
│   ├── build
│   │   └── 
│   ├── include
│   │   └── helper.h
│   └── src
│       ├── helper.cpp
        └── main.cpp

CMakeList.txt的是:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(object_detection)

find_package(PCL 1.5 REQUIRED)
find_package(OpenCV REQUIRED)


file(GLOB SOURCES src/*.cpp)
file(GLOB INCLUDE include/*.h)

include_directories(${PCL_INCLUDE_DIRS} ${INCLUDE})
link_directories(${PROJECT_NAME} ${PCL_LIBRARY_DIRS} ${SOURCES} )
add_definitions(${PCL_DEFINITIONS})


add_executable (${PROJECT_NAME} src/main.cpp)
target_link_libraries (${PROJECT_NAME}  ${OpenCV_LIBS} ${PCL_LIBRARIES} ${SOURCES})

在我的文件helper.cpp中,我有:

#include <pcl/io/pcd_io.h>

这给出了错误:

fatal error: 'pcl/io/pcd_io.h' file not found
#include <pcl/io/pcd_io.h>
         ^~~~~~~~~~~~~~~~~

但是我有相同的包含main.cpp没有错误。

我将非常感谢任何帮助,如果我需要澄清我的问题或错误,请告诉我。谢谢你。

4

1 回答 1

0

CMakeLists.txt项目加载适当的库并正确构建的以下更改中有几个错误。另一个注意事项是,之前,要包括helper.h我需要写: #include "../include/helper.h"
现在它可以按预期使用#include "helper.h". 这是修改后的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

project(object_detection)

find_package(PCL 1.5 REQUIRED)
find_package(OpenCV REQUIRED)


file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp)


include_directories(${PCL_INCLUDE_DIRS} include)
link_directories(${PROJECT_NAME} ${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})


#add_executable (${PROJECT_NAME} src/helper.cpp src/main.cpp )
add_executable (${PROJECT_NAME} ${SRC_FILES} )

target_link_libraries (object_detection ${PCL_LIBRARIES} ${OpenCV_LIBS})
于 2021-05-23T22:22:58.463 回答