Search code examples
c++cmakeincludefile-not-foundcmakelists-options

How do you add external libraries to 'self-made' libraries using CMake?


I'm not able to link external libraries with the libraries I wrote using CMake. I'm wondering if there's something that is needed to be added to my CMakeLists.txt? Or if I need to add another CMakeLists.txt in a lower level (inside src) and what would that need to contain?

I have the following project structure:

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

My CMakeList.txt is:

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})

In my file helper.cpp I have:

#include <pcl/io/pcd_io.h>

Which gives the error:

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

However I have the same include in main.cpp with no errors.

I would be very grateful for any help, please let me know if I need to clarify my question or error. Thank you.


Solution

  • There are several errors in the CMakeLists.txt with the following changes the project loads appropriate libraries and builds properly. Another note is that before, to include helper.h I needed to write: #include "../include/helper.h".
    Now it works as expected with #include "helper.h". Here is the modified 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})