0%

compile cmake from source on ubuntu and write your own user defined FindXXX.cmake

Compile

1
2
3
4
5
6
7
8
9
10
11
sudo apt-get install build-essential
wget http://www.cmake.org/files/v3.2/cmake-3.2.2.tar.gz
tar xf cmake-3.2.2.tar.gz
cd cmake-3.2.2
./configure
make && make install


which cmake
#/usr/local/bin/cmake
cmake --version

or by apt-get -y install cmake

User defined cmake

  • FindXXX.cmake in CMAKE_MODULE_PATH
  • xxx-config.cmake in CMAKE_PREFIX_PATH

cmake default package

FindXXX.cmake

  1. use find_package to find default package with name XXX

  2. and cmake file C:\Program Files\CMake\share\cmake-3.10\Modules\FindXXX.cmake

  3. use ${XXX_INCLUDE_DIRS} in include, and ${XXX_LIBRARIES} in libraries

usage

1
2
3
4
5
6
7
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

find_package(Boost 1.5.8 REQUIRED COMPONENTS date_time system filesystem)
include_directories(${Boost_INCLUDE_DIRS})

target_link_libraries(demo ${GTEST_LIBRARIES} ${Boost_LIBRARIES})

user-defined package

xxx-config.cmake

both names are ok.

  • xxx-config.cmake
  • XXXConfig.cmake, e.g. OpenCVConfig.cmake

mysqlcppconn-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Name: <Name>Config.cmake  or <lower name>-config.cmake
# mysqlcppconn-config.cmake or MYSQLCPPCONNConfig.cmake
# similar to OpenCVConfig.cmake

# Tips for MYSQLCPPCONN_ROOT_DIR
# use "C:/Program Files/MySQL/Connector.C++ 1.1", otherwise cmake-gui can not auto find include and library

set(MYSQLCPPCONN_FOUND TRUE) # auto
set(MYSQLCPPCONN_ROOT_DIR "C:/Program Files/MySQL/Connector.C++ 1.1")

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

halcon-config.cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# halcon-config.cmake or HALCONConfig.cmake  

set(HALCON_FOUND TRUE) # auto
set(HALCON_ROOT_DIR E:/git/car/windows/lib/halcon)

find_path(HALCON_INCLUDE_DIR NAMES halconcpp/HalconCpp.h PATHS "${HALCON_ROOT_DIR}/include")
mark_as_advanced(HALCON_INCLUDE_DIR) # show entry in cmake-gui

find_library(HALCON_LIBRARY NAMES halconcpp.lib PATHS "${HALCON_ROOT_DIR}/lib/x64-win64")
mark_as_advanced(HALCON_LIBRARY) # show entry in cmake-gui

# use xxx_INCLUDE_DIRS and xxx_LIBRARIES in CMakeLists.txt
set(HALCON_INCLUDE_DIRS ${HALCON_INCLUDE_DIR} )
set(HALCON_LIBRARIES ${HALCON_LIBRARY} )

message( "halcon-config.cmake " ${HALCON_ROOT_DIR})

usage

1
2
3
4
5
6
7
find_package(HALCON REQUIRED) # user-defined
include_directories(${HALCON_INCLUDE_DIRS})

find_package(MYSQLCPPCONN REQUIRED) # user-defined
include_directories(${MYSQLCPPCONN_INCLUDE_DIRS})

target_link_libraries(demo ${HALCON_LIBRARIES} ${MYSQLCPPCONN_LIBRARIES})

cmake-gui entry

  1. start cmake-gui, and at first,we should set
  • HALCON_DIR = E:/git/car/share/cmake-3.10/Modules
  • MYSQLCPPCONN_DIR = E:/git/car/share/cmake-3.10/Modules
  1. then configure
  • HALCON_INCLUDE_DIR and HALCON_LIBRARY will be found.
  • MYSQLCPPCONN_INCLUDE_DIR and MYSQLCPPCONN_LIBRARY will be found.

cmake-gui user defined entry

Advanced

execute_process

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
execute_process(
COMMAND bash "-c" "echo -n hello | sed 's/hello/world/;'"
OUTPUT_VARIABLE VARIABLE
)
MESSAGE( [Main] " VARIABLE = ${VARIABLE}") # world

execute_process(
COMMAND bash "-c" "echo -n $(uname -p)"
OUTPUT_VARIABLE ARCH
)
MESSAGE( [Main] " ARCH = ${ARCH}") # aarch64 x86_64

# glib header only
set(GLIB_INCLUDE_DIRS "/usr/include/glib-2.0/") # glib.h
set(GLIBCONFIG_INCLUDE_DIRS "/usr/lib/${ARCH}-linux-gnu/glib-2.0/include/") # glibconfig.h
MESSAGE( [Main] " GLIB_INCLUDE_DIRS = ${GLIB_INCLUDE_DIRS}")
MESSAGE( [Main] " GLIBCONFIG_INCLUDE_DIRS = ${GLIBCONFIG_INCLUDE_DIRS}")

PIC error

error

1
2
3
/usr/bin/ld: /usr/local/lib/libdlib.a(test_for_odr_violations.cpp.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libdlib.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

solution

1
set(CMAKE_BUILD_TYPE "Release") # release

Reference

History

  • 20180122: created.