cmake-05-3ppPackage

施永贞
2023-12-01

cmake helper and apt-get

  • cmake helper

    cmake installer itself may include some tools (FindXX.cmake) to help check 3pp installed on host

  • manualy install 3pp c++ lib , boost

	#install 3pp package boost
	sudo apt-get install libboost-all-dev

use 3pp source

  • use subproject

    $ tree
    .
    ├── 3rd_party
    │   └── catch2
    │       ├── catch2
    │       │   └── catch.hpp
    │       └── CMakeLists.txt
    ├── CMakeLists.txt
    ├── src
    │   └── example.cpp
    
    	cmake_minimum_required(VERSION 3.5)
    	project (catch2_unit_test)
    	set(CMAKE_CXX_STANDARD 11)
    	
    	# add the CMakeFile that defines catch2
    	add_subdirectory(3rd_party/catch2)
    	
    	add_library(example_unit_test
    	    Reverse.cpp
    	    Palindrome.cpp
    	)
    	
    	
    	#############################################
    	# Unit tests
    	
    	# enable CTest testing
    	enable_testing()
    	
    	# Add a testing executable
    	add_executable(unit_tests unit_tests.cpp)
    	
    	target_link_libraries(unit_tests
    	    example_unit_test
    	    Catch2::Test
    	)
    	
    	add_test(test_all unit_tests)
    
  • Exceptions

    If the third party code doesn’t support CMake, you may need to create
    a “shim” layer on top of the project to allow it to be build and
    discovered from CMake

external project

detect boost on-host

	cmake_minimum_required(VERSION 3.5)
	project (third_party_include)

	# find a boost install with the libraries filesystem and system
	find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
	
	# check if boost was found
	if(Boost_FOUND)
	    message ("boost found")
	else()
	    message (FATAL_ERROR "Cannot find Boost")
	endif()
	
	# Add an executable
	add_executable(third_party_include main.cpp)
	
	# link against the boost libraries
	target_link_libraries( third_party_include
	    PRIVATE
	        Boost::filesystem
	)
	//main.cpp
	#include <iostream>
	#include <boost/shared_ptr.hpp>
	#include <boost/filesystem.hpp>
	
	int main(int argc, char *argv[])
	{
	    std::cout << "Hello Third Party Include!" << std::endl;
	
	    // use a shared ptr
	    boost::shared_ptr<int> isp(new int(4));
	
	    // trivial use of boost filesystem
	    boost::filesystem::path path = "/usr/share/cmake/modules";
	    if(path.is_relative())
	    {
	        std::cout << "Path is relative" << std::endl;
	    }
	    else
	    {
	        std::cout << "Path is not relative" << std::endl;
	    }
	
	   return 0;
	}
	
	#log
	- The C compiler identification is GNU 9.3.0
	-- The CXX compiler identification is GNU 9.3.0
	-- Check for working C compiler: /usr/bin/cc
	-- Check for working C compiler: /usr/bin/cc -- works
	-- Detecting C compiler ABI info
	-- Detecting C compiler ABI info - done
	-- Detecting C compile features
	-- Detecting C compile features - done
	-- Check for working CXX compiler: /usr/bin/c++
	-- Check for working CXX compiler: /usr/bin/c++ -- works
	-- Detecting CXX compiler ABI info
	-- Detecting CXX compiler ABI info - done
	-- Detecting CXX compile features
	-- Detecting CXX compile features - done
	-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.46.1") found components: filesystem system 
	boost found
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-examples/01-basic/H-third-party-library
	jianleya@jianleya-VirtualBox:~/trainning/cmake/cmake/cmake-examples/01-basic/H-third-party-library$ make 
	Scanning dependencies of target third_party_include
	[ 50%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o
	[100%] Linking CXX executable third_party_include
	[100%] Built target third_party_include
	jianleya@jianleya-VirtualBox:~/trainning/cmake/cmake/cmake-examples/01-basic/H-third-party-library$ ls
	CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  main.cpp  Makefile  README.adoc  third_party_include
	jianleya@jianleya-VirtualBox:~/trainning/cmake/cmake/cmake-examples/01-basic/H-third-party-library$ ./third_party_include 
	Hello Third Party Include!
	Path is not relative
	

detect python interpreter on-host

# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# project name and language
project(recipe-01 LANGUAGES NONE)

# detect python
find_package(PythonInterp REQUIRED)

# Execute a tiny Python script
execute_process(
  COMMAND
    ${PYTHON_EXECUTABLE} "-c" "print('Hello, world!')"
  RESULT_VARIABLE _status
  OUTPUT_VARIABLE _hello_world
  ERROR_QUIET
  OUTPUT_STRIP_TRAILING_WHITESPACE
  )

message(STATUS "RESULT_VARIABLE is: ${_status}")
message(STATUS "OUTPUT_VARIABLE is: ${_hello_world}")

# compare the "manual" messages with the following handy helper
include(CMakePrintHelpers)
cmake_print_variables(_status _hello_world)

#log
	$ cmake .
	-- Found PythonInterp: /usr/bin/python3.8 (found version "3.8.5") 
	-- RESULT_VARIABLE is: 0
	-- OUTPUT_VARIABLE is: Hello, world!
	-- _status="0" ; _hello_world="Hello, world!"
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-03/recipe-01/example

detect python library on-host

# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# project name and language
project(recipe-02 LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)

find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)

add_executable(hello-embedded-python hello-embedded-python.c)

target_include_directories(hello-embedded-python
  PRIVATE
    ${PYTHON_INCLUDE_DIRS}
  )

target_link_libraries(hello-embedded-python
  PRIVATE
    ${PYTHON_LIBRARIES}
  )


#log
	$ cmake .
	-- The C compiler identification is GNU 9.3.0
	-- Check for working C compiler: /usr/bin/cc
	-- Check for working C compiler: /usr/bin/cc -- works
	-- Detecting C compiler ABI info
	-- Detecting C compiler ABI info - done
	-- Detecting C compile features
	-- Detecting C compile features - done
	-- Found PythonInterp: /usr/bin/python3.8 (found version "3.8.5") 
	-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.8.so (found suitable exact version "3.8.5") 
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-03/recipe-02/c-example

detect any 3pp using pkg-config

cmake_minimum_required(VERSION 3.6 FATAL_ERROR)

project(recipe-09 LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)

find_package(PkgConfig REQUIRED QUIET)

pkg_search_module(
  ZeroMQ
  REQUIRED
    libzeromq libzmq lib0mq
  IMPORTED_TARGET
  )

if(TARGET PkgConfig::ZeroMQ)
  message(STATUS "Found ZeroMQ")
endif()

add_executable(hwserver hwserver.c)

target_link_libraries(hwserver PkgConfig::ZeroMQ)

add_executable(hwclient hwclient.c)

target_link_libraries(hwclient PkgConfig::ZeroMQ)

#log
	$ cmake .
	-- The C compiler identification is GNU 9.3.0
	-- Check for working C compiler: /usr/bin/cc
	-- Check for working C compiler: /usr/bin/cc -- works
	-- Detecting C compiler ABI info
	-- Detecting C compiler ABI info - done
	-- Detecting C compile features
	-- Detecting C compile features - done
	-- Checking for one of the modules 'libzeromq;libzmq;lib0mq'
	CMake Error at /usr/share/cmake-3.16/Modules/FindPkgConfig.cmake:707 (message):
	  None of the required 'libzeromq;libzmq;lib0mq' found
	Call Stack (most recent call first):
	  CMakeLists.txt:11 (pkg_search_module)
	
	
	-- Configuring incomplete, errors occurred!
	See also "/home/jianleya/trainning/cmake/cmake/cmake-cookbook/cmake-cookbook/chapter-03/recipe-09/c-example/CMakeFiles/CMakeOutput.log".

Add find-module to cmake

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-10 LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_STANDARD_REQUIRED ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})

find_package(ZeroMQ REQUIRED)

add_executable(hwserver hwserver.c)

target_include_directories(hwserver
  PRIVATE
    ${ZeroMQ_INCLUDE_DIRS}
  )

target_link_libraries(hwserver
  PRIVATE
    ${ZeroMQ_LIBRARIES}
  )

add_executable(hwclient hwclient.c)

target_include_directories(hwclient
  PRIVATE
    ${ZeroMQ_INCLUDE_DIRS}
  )

target_link_libraries(hwclient
  PRIVATE
    ${ZeroMQ_LIBRARIES}
  )

 类似资料: