Add External Libraries to Cmakelist.Txt C++

Add external libraries to CMakeList.txt c++

I would start with upgrade of CMAKE version.

You can use INCLUDE_DIRECTORIES for header location and LINK_DIRECTORIES + TARGET_LINK_LIBRARIES for libraries

INCLUDE_DIRECTORIES(your/header/dir)
LINK_DIRECTORIES(your/library/dir)
rosbuild_add_executable(kinectueye src/kinect_ueye.cpp)
TARGET_LINK_LIBRARIES(kinectueye lib1 lib2 lib2 ...)

note that lib1 is expanded to liblib1.so (on Linux), so use ln to create appropriate links in case you do not have them

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

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

including external libraries in cmakelists.txt file

The simplest way for build your project alongside with 3d party project is add_subdirectory() that subproject. Approach below implies that you (manually) download(git clone) sources of mraa project into mraa-lib subdirectory of your project's sources.

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

MESSAGE( STATUS "Starting build process")

# Configure subproject before definition of variables for main project.
#
# Subproject defines *mraa* library target.
add_subdirectory(mraa-lib)

# Before installation, public mraa headers are contained under *api* subdirectory.
include_directories(mraa-lib/api)

... # create executable ${APPLICATION_NAME}

# Linking with mraa library is straightforward.
target_link_libraries(${APPLICATION_NAME} mraa)

That way mraa subproject will be configured, built and installed alongside with your project. So, if you cross-compile your project(using toolchain file, or inside IDE), the subproject will also be cross-compiled.

CMake link to external library

Set libraries search path first:

link_directories(${CMAKE_BINARY_DIR}/res)

And then just do

target_link_libraries(GLBall mylib)


Related Topics



Leave a reply



Submit