0%

cmake find_package for different packages

Packages

Boost

1
sudo apt-get install libboost-all-dev

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Boost
if(MSVC)
# use static boost on windows
set(Boost_USE_STATIC_LIBS ON)
else()
# use release boost on linux
set(Boost_USE_RELEASE_LIBS ON)
endif(MSVC)

set(Boost_USE_MULTITHREAD ON)
# Find Boost package 1.66.0
find_package(Boost REQUIRED COMPONENTS serialization date_time system filesystem thread timer)

include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(demo ${Boost_LIBRARIES})

GTest

1
git clone https://github.com/google/googletest.git

install to C:\Program Files\gtest on windows.

1
2
3
4
5
6
7
8
9
10
11
12
if(MSVC) 
SET(GTEST_ROOT "C:/Program Files/gtest")
else()
# BOOST_THREAD_LIBRARY /usr/lib/x86_64-linux-gnu/libpthread.so
MESSAGE( [Main] " BOOST_THREAD_LIBRARY = ${BOOST_THREAD_LIBRARY}")
endif(MSVC)

find_package(GTest REQUIRED) # GTest 1.8.0

find_package(GTest REQUIRED) # GTest 1.8.0
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(demo ${GTEST_LIBRARIES} ${BOOST_THREAD_LIBRARY})

gflags+glog

1
2
wget https://github.com/schuhschuh/gflags/archive/v2.2.1.tar.gz
wget https://github.com/google/glog/archive/v0.3.5.tar.gz

for find_package(GFLAGS REQUIRED) we copy user-defined gflags-config.cmake from caffe project to /usr/local/lib/cmake/gflags
for find_package(GLOG REQUIRED) we copy user-defined glog-config.cmake from caffe project to /usr/local/lib/cmake/glog
see compile glog and glags on ubuntu 16.04

1
2
3
4
5
6
7
8
9
10
11
find_package(GFLAGS REQUIRED) 
include_directories(${GFLAGS_INCLUDE_DIRS})

find_package(GLOG REQUIRED)
include_directories(${GLOG_INCLUDE_DIRS})

# for windows
#add_definitions( -DGLOG_NO_ABBREVIATED_SEVERITIES )

add_executable(demo glog_main.cpp)
target_link_libraries (demo ${GLOG_LIBRARIES} ${GFLAGS_LIBRARIES})

mysqlconcpp

1
2
sudo apt-cache search libmysqlcppconn
sudo apt-get -y install libmysqlcppconn-dev libmysqlcppconn7v5

for find_package(MYSQLCPPCONN REQUIRED) we copy user-defined mysqlcppconn-config.cmake to /usr/lib/cmake/mysqlcppconn

1
2
3
4
find_package(MYSQLCPPCONN REQUIRED)

include_directories(${MYSQLCPPCONN_INCLUDE_DIRS})
target_link_libraries (demo ${MYSQLCPPCONN_LIBRARIES})

libjpegturbo

1
git clone https://github.com/libjpeg-turbo/libjpeg-turbo.git

for find_package(LIBJPEGTURBO REQUIRED) we copy user-defined libjpegturbo-config.cmake to /usr/lib/cmake/libjpegturbo

1
2
3
find_package(LIBJPEGTURBO REQUIRED)
include_directories(${LIBJPEGTURBO_INCLUDE_DIRS})
target_link_libraries (example_jpeg ${LIBJPEGTURBO_LIBRARIES})

Poco

1
2
wget https://pocoproject.org/releases/poco-1.8.1/poco-1.8.1.zip
wget https://pocoproject.org/releases/poco-1.8.1/poco-1.8.1.tar.gz

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
find_package(Poco REQUIRED COMPONENTS Foundation Util Net XML JSON)

# no Poco_INCLUDE_DIRS, we have to set by hand
if(MSVC) # WIN32
SET(Poco_INCLUDE_DIRS "C:/Program Files/Poco/include")
else()
SET(Poco_INCLUDE_DIRS "/usr/local/include/Poco")
endif(MSVC)

MESSAGE( [Main] " Poco_INCLUDE_DIRS = ${Poco_INCLUDE_DIRS}")
MESSAGE( [Main] " Poco_LIBRARIES = ${Poco_LIBRARIES}")

include_directories(${Poco_INCLUDE_DIRS} )
target_link_libraries(demo ${Poco_LIBRARIES})

OpenCV

1
2
3
4
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc features2d calib3d) 

include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(demo ${OpenCV_LIBS})

QT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Core)
find_package(Qt5Widgets)
find_package(Qt5Gui)
find_package(Qt5OpenGL)
find_package(Qt5Xml)

# cpp files
aux_source_directory(. SRC_LIST)
# ui files
qt5_wrap_ui(ui_FILES mainwindow.ui)
# resource files
qt5_add_resources(qrc_FILES resource.qrc)

message( [Main] ${SRC_LIST} ) # ./main.cpp./mainwindow.cpp
message( [Main] ${ui_FILES} ) # build/ui_mainwindow.h
message( [Main] ${qrc_FILES} )# build/qrc_resource.cpp

# Tell CMake to create the helloworld executable
add_executable(${PROJECT_NAME} ${SRC_LIST} ${ui_FILES} ${qrc_FILES})

qt5_use_modules(${PROJECT_NAME} Core Widgets OpenGL Xml Gui)

VTK

1
2
3
4
5
6
find_package(VTK 8.1 REQUIRED)
include(${VTK_USE_FILE})

add_executable(CylinderRenderingProperties MACOSX_BUNDLE CylinderRenderingProperties.cxx )

target_link_libraries(CylinderRenderingProperties ${VTK_LIBRARIES})

PCL

1
2
3
4
5
6
7
8
find_package(PCL 1.8.1 REQUIRED)

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

add_executable (cloud_viewer cloud_viewer.cpp)
target_link_libraries (cloud_viewer ${PCL_LIBRARIES})

or with components

1
2
3
4
5
6
7
8
find_package(PCL 1.8.1 REQUIRED COMPONENTS common io filters visualization)

target_link_libraries(cloud_viewer
${PCL_COMMON_LIBRARIES}
${PCL_IO_LIBRARIES}
${PCL_FILTERS_LIBRARIES}
${PCL_VISUALIZATION_LIBRARIES}
)

User-defined cmake

gflags-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set(GFLAGS_FOUND TRUE) # auto 
set(GFLAGS_VERSION 2.2.0)
set(GFLAGS_ROOT_DIR "C:/car_libs/gflags")

find_path(GFLAGS_INCLUDE_DIR NAMES gflags/gflags.h PATHS "${GFLAGS_ROOT_DIR}/include")
mark_as_advanced(GFLAGS_INCLUDE_DIR) # show entry in cmake-gui

find_library(GFLAGS_LIBRARY NAMES gflags.lib PATHS "${GFLAGS_ROOT_DIR}/lib")
mark_as_advanced(GFLAGS_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR} )
set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY} )

message( "gflags-config.cmake " ${GFLAGS_ROOT_DIR})

glog-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set(GLOG_FOUND TRUE) # auto 
set(GLOG_VERSION 0.3.5)
set(GLOG_ROOT_DIR "C:/car_libs/glog")

find_path(GLOG_INCLUDE_DIR NAMES glog/logging.h PATHS "${GLOG_ROOT_DIR}/include")
mark_as_advanced(GLOG_INCLUDE_DIR) # show entry in cmake-gui

find_library(GLOG_LIBRARY NAMES glog.lib PATHS "${GLOG_ROOT_DIR}/lib")
mark_as_advanced(GLOG_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR} )
set(GLOG_LIBRARIES ${GLOG_LIBRARY} )

message( "glog-config.cmake " ${GLOG_ROOT_DIR})

mysqlconcpp-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
set(MYSQLCPPCONN_FOUND TRUE) # auto 
set(MYSQLCPPCONN_VERSION 1.1)
set(MYSQLCPPCONN_ROOT_DIR "C:/car_libs/mysqlcppconn")

find_path(MYSQLCPPCONN_INCLUDE_DIR NAMES cppconn/driver.h PATHS "${MYSQLCPPCONN_ROOT_DIR}/include")
mark_as_advanced(MYSQLCPPCONN_INCLUDE_DIR) # show entry in cmake-gui

find_library(MYSQLCPPCONN_LIBRARY NAMES mysqlcppconn.lib PATHS "${MYSQLCPPCONN_ROOT_DIR}/lib/opt")
mark_as_advanced(MYSQLCPPCONN_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(MYSQLCPPCONN_INCLUDE_DIRS ${MYSQLCPPCONN_INCLUDE_DIR} )
set(MYSQLCPPCONN_LIBRARIES ${MYSQLCPPCONN_LIBRARY} )

# cmake entry will be saved to build/CMakeCache.txt

message( "mysqlcppconn-config.cmake " ${MYSQLCPPCONN_ROOT_DIR})

libjpegturbo-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
set(LIBJPEGTURBO_FOUND TRUE) # auto 
set(LIBJPEGTURBO_VERSION 1.5.4)
set(LIBJPEGTURBO_ROOT_DIR "C:/car_libs/libjpeg-turbo64")

find_path(LIBJPEGTURBO_INCLUDE_DIR NAMES jpeglib.h turbojpeg.h PATHS "${LIBJPEGTURBO_ROOT_DIR}/include")
mark_as_advanced(LIBJPEGTURBO_INCLUDE_DIR) # show entry in cmake-gui

find_library(LIBJPEGTURBO_JPEG_LIBRARY NAMES jpeg.lib PATHS "${LIBJPEGTURBO_ROOT_DIR}/lib")
mark_as_advanced(LIBJPEGTURBO_JPEG_LIBRARY) # show entry in cmake-gui

find_library(LIBJPEGTURBO_TURBOJPEG_LIBRARY NAMES turbojpeg.lib PATHS "${LIBJPEGTURBO_ROOT_DIR}/lib")
mark_as_advanced(LIBJPEGTURBO_TURBOJPEG_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(LIBJPEGTURBO_INCLUDE_DIRS ${LIBJPEGTURBO_INCLUDE_DIR} )
set(LIBJPEGTURBO_LIBRARIES ${LIBJPEGTURBO_JPEG_LIBRARY} ${LIBJPEGTURBO_TURBOJPEG_LIBRARY} )

message( "libjpegturbo-config.cmake " ${LIBJPEGTURBO_ROOT_DIR})

winpcap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
set(WINPCAP_FOUND TRUE) # auto 
set(WINPCAP_VERSION 1.0.0)
set(WINPCAP_ROOT_DIR "C:/car_libs/winpcap")

find_path(WINPCAP_INCLUDE_DIR NAMES pcap.h PATHS "${WINPCAP_ROOT_DIR}/Include")
mark_as_advanced(WINPCAP_INCLUDE_DIR) # show entry in cmake-gui

find_library(WINPCAP_LIBRARY NAMES wpcap.lib PATHS "${WINPCAP_ROOT_DIR}/Lib")
mark_as_advanced(WINPCAP_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(WINPCAP_INCLUDE_DIRS ${WINPCAP_INCLUDE_DIR} )
set(WINPCAP_LIBRARIES ${WINPCAP_LIBRARY} )

message( "winpcap-config.cmake " ${WINPCAP_ROOT_DIR})

OpenNI2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set(OPENNI2_FOUND TRUE) # auto 
set(OPENNI2_VERSION 1.0.0)
set(OPENNI2_ROOT_DIR "C:/car_libs/OpenNI2")

find_path(OPENNI2_INCLUDE_DIR NAMES OpenNI.h PATHS "${OPENNI2_ROOT_DIR}/Include")
mark_as_advanced(OPENNI2_INCLUDE_DIR) # show entry in cmake-gui

find_library(OPENNI2_LIBRARY NAMES OpenNI2.lib PATHS "${OPENNI2_ROOT_DIR}/Lib")
mark_as_advanced(OPENNI2_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(OPENNI2_INCLUDE_DIRS ${OPENNI2_INCLUDE_DIR} )
set(OPENNI2_LIBRARIES ${OPENNI2_LIBRARY} )

message( "openni2-config.cmake " ${OPENNI2_ROOT_DIR})

CMake Options

how to find_package

CMAKE_PREFIX_PATH + find_package

1
2
3
4
5
6
7
8
9
10
set(ROOT_CMAKE_DIR /home/user/program/anakin/sdk/cmake)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${ROOT_CMAKE_DIR};${CMAKE_PREFIX_PATH}")
MESSAGE( [cmake] " CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH} for find_package")

# find anakin-config.cmake file in CMAKE_PREFIX_PATH
find_package(ANAKIN REQUIRED)
include_directories(${ANAKIN_INCLUDE_DIRS})

message( [anakin] ${ANAKIN_INCLUDE_DIRS} )
message( [anakin] ${ANAKIN_LIBRARIES} )

include xxx-config.cmake

1
2
3
4
5
include(cmake/anakin-config.cmake)
include_directories(${ANAKIN_INCLUDE_DIRS})

message( [anakin] ${ANAKIN_INCLUDE_DIRS} )
message( [anakin] ${ANAKIN_LIBRARIES} )

c++11

1
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

Reference

History

  • 20180208: created.
  • 20180301: add xxx-config.cmake.