CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.
# # For x64 compile, please use command # mkdir build && cd build # cmake .. # make -j8 # For arm cross compile, please use command # mkdir build && cd build # cmake -DPLATFORM=arm .. # make -j8 #
# For C++ Project # set_property(TARGET run_demo PROPERTY C_STANDARD 99) # set_property(TARGET run_demo PROPERTY C_STANDARD_REQUIRED ON)
# For C Project # set_property(TARGET run_demo PROPERTY CXX_STANDARD 11) # set_property(TARGET run_demo PROPERTY CXX_STANDARD_REQUIRED ON)
message(STATUS "Project Source path " ${PROJECT_SOURCE_DIR}) message(STATUS "Project Current list dir " ${CMAKE_CURRENT_LIST_DIR})
# Option can use the OFF or ON to build the each mode. option(BUILD_C++_PROJECT "Build C++ Project" ON) option(BUILD_C_PROJECT "Build C Project" ON)
# Set output file path. SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR} CACHE PATH "" )
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin CACHE PATH "" )
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR} CACHE PATH "" )
if(BUILD_C++_PROJECT) # Arm platform if (PLATFORM MATCHES "arm") # Set up the build operating system for Cmake. set(CMAKE_SYSTEM_NAME Linux) # Set up the Cmake compiler execution platform. set(CMAKE_SYSTEM_PROCESSOR arm)
MESSAGE(STATUS "PLATFORM NAME " ${PLATFORM}) # Set up the cross compilation tool for Cmake. set(CMAKE_CXX_COMPILER /yourpath/arm-none-linux-gnueabi-g++) set(CMAKE_C_COMPILER /yourpath/arm-none-linux-gnueabi-gcc)
MESSAGE(STATUS "CMAKE_CXX_COMPILER NAME " ${CMAKE_CXX_COMPILER}) MESSAGE(STATUS "CMAKE_C_COMPILER NAME " ${CMAKE_C_COMPILER}) # Set the compile options for g++ set(GNU_FLAGS "-mfpu=vfp -fPIC -g -W -O2 -DBOOST_ASIO_DISABLE_STD_FUTURE") set(CMAKE_CXX_FLAGS "${GNU_FLAGS}") set(CMAKE_C_FLAGS "${GNU_FLAGS}") # Set contains arm third-party library header files include_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/include) # Set including arm third-party libraries link_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/lib)
else() # Linux X86 platform. set(GNU_FLAGS "-DCOM_X64 -g -W") set(CMAKE_CXX_FLAGS "${GNU_FLAGS}") set(CMAKE_C_FLAGS "${GNU_FLAGS}")
# Contains x86 third-party library header files include_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/include) # Including x86 third-party libraries link_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/lib)
endif()
# Add your project header files. include_directories(${PROJECT_SOURCE_DIR}/include/)
# Add the file to the any_name variable. set(any_name ${PROJECT_SOURCE_DIR}/src/your_add_1.cpp ${PROJECT_SOURCE_DIR}/src/your_add_2.cpp ${PROJECT_SOURCE_DIR}/src/your_add_3.cpp)
# Build the any_name variable a lib. # STATIC, SHARED, and MODULE are used to specify the type of library file to be generated. # Share lib. add_library(any_name_lib STATIC ${any_name}) # Open build the shared library. option(BUILD_SHARED_LIBS "Build the shared library" ON) # Static lib. # add_library(any_name_lib STATIC ${any_name}) # Build the third-party input your lib, can link share lib or static lib. target_link_libraries(any_name_lib dl pthread ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a ) # Build the third-party header files input your lib, can link share lib or static lib. target_include_directories(any_name_lib ${CMAKE_CURRENT_LIST_DIR}/libs/json/include)
# Set any_name_lib lib target c++ stand version. set_property(TARGET any_name_lib PROPERTY CXX_STANDARD 11) set_property(TARGET any_name_lib PROPERTY CXX_STANDARD_REQUIRED ON)
# Set the lib of version num. set(MAJOR 1) set(MINOR 0) set(PATCH 0) if(BUILD_SHARED_LIBS) set_target_properties(any_name_lib PROPERTIES SOVERSION ${MAJOR} VERSION ${MAJOR}.${MINOR}.${PATCH} ) endif()
# Build with main function to be one run file. add_executable(main_app ${PROJECT_SOURCE_DIR}/src/main.cpp )
# Set the main_app target c++ stand version. set_property(TARGET main_app PROPERTY CXX_STANDARD 11) set_property(TARGET main_app PROPERTY CXX_STANDARD_REQUIRED ON)
# Link with main function with the link share lib or static lib to be one run file. target_link_libraries(main_app any_name_lib pthread ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a) # If no any_name_lib, link with main function with the link share lib or static lib to be one run file. # target_link_libraries(main_app # pthread # ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a)
endif()
if(BUILD_C_PROJECT) # Arm platform if (PLATFORM MATCHES "arm") # Set up the build operating system for Cmake. set(CMAKE_SYSTEM_NAME Linux) # Set up the Cmake compiler execution platform. set(CMAKE_SYSTEM_PROCESSOR arm)
MESSAGE(STATUS "PLATFORM NAME " ${PLATFORM}) # Set up the cross compilation tool for Cmake. set(CMAKE_CXX_COMPILER /yourpath/arm-none-linux-gnueabi-g++) set(CMAKE_C_COMPILER /yourpath/arm-none-linux-gnueabi-gcc)
MESSAGE(STATUS "CMAKE_CXX_COMPILER NAME " ${CMAKE_CXX_COMPILER}) MESSAGE(STATUS "CMAKE_C_COMPILER NAME " ${CMAKE_C_COMPILER}) # Set the compile options for g++ set(GNU_FLAGS "-mfpu=vfp -fPIC -g -W -O2 -DBOOST_ASIO_DISABLE_STD_FUTURE") set(CMAKE_CXX_FLAGS "${GNU_FLAGS}") set(CMAKE_C_FLAGS "${GNU_FLAGS}") # Set contains arm third-party library header files include_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/include) # Set including arm third-party libraries link_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/lib)
else() # Linux X86 platform. set(GNU_FLAGS "-DCOM_X64 -g -W") set(CMAKE_CXX_FLAGS "${GNU_FLAGS}") set(CMAKE_C_FLAGS "${GNU_FLAGS}")
# Contains x86 third-party library header files include_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/include) # Including x86 third-party libraries link_directories(${CMAKE_CURRENT_LIST_DIR}/libs/arm/boost/lib)
endif()
# Add your project header files. include_directories(${PROJECT_SOURCE_DIR}/include/)
# Add the file to the any_name variable. set(any_name ${PROJECT_SOURCE_DIR}/src/your_add_1.c ${PROJECT_SOURCE_DIR}/src/your_add_2.c ${PROJECT_SOURCE_DIR}/src/your_add_3.c)
# Build the any_name variable a lib. # STATIC, SHARED, and MODULE are used to specify the type of library file to be generated. # Share lib. add_library(any_name_lib STATIC ${any_name}) # Open build the shared library. option(BUILD_SHARED_LIBS "Build the shared library" ON) # Static lib. # add_library(any_name_lib STATIC ${any_name}) # Build the third-party input your lib, can link share lib or static lib. target_link_libraries(any_name_lib dl pthread ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a ) # Build the third-party header files input your lib, can link share lib or static lib. target_include_directories(any_name_lib ${CMAKE_CURRENT_LIST_DIR}/libs/json/include)
# Set any_name_lib lib target c++ stand version. set_property(TARGET run_demo PROPERTY C_STANDARD 99) set_property(TARGET run_demo PROPERTY C_STANDARD_REQUIRED ON)
# Set the lib of version num. set(MAJOR 1) set(MINOR 0) set(PATCH 0) if(BUILD_SHARED_LIBS) set_target_properties(any_name_lib PROPERTIES SOVERSION ${MAJOR} VERSION ${MAJOR}.${MINOR}.${PATCH} ) endif()
# Build with main function to be one run file. add_executable(main_app ${PROJECT_SOURCE_DIR}/src/main.c )
# Set the main_app target c++ stand version. set_property(TARGET run_demo PROPERTY C_STANDARD 99) set_property(TARGET run_demo PROPERTY C_STANDARD_REQUIRED ON)
# Link with main function with the link share lib or static lib to be one run file. target_link_libraries(main_app any_name_lib pthread ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a) # If no any_name_lib, link with main function with the link share lib or static lib to be one run file. # target_link_libraries(main_app # pthread # ${CMAKE_CURRENT_LIST_DIR}/libs/libjsoncpp.a) endif()
# # For x64 compile, please use command # cmake -DCMAKE_BUILD_TYPE=Debug .. # For arm cross compile, please use command # cmake -DPLATFORM=arm -DCMAKE_BUILD_TYPE=Release .. # make -j4 && make install # cmake_minimum_required (VERSION 3.5)
# 打开动态库开关 option(BUILD_SHARED_LIBS "Build the math shared library" ON) # 设置动态库的版本号 set(MAJOR 1) set(MINOR 0) set(PATCH 0)
# # For x64 compile, please use command # cmake -DCMAKE_BUILD_TYPE=Debug .. # For arm cross compile, please use command # cmake -DPLATFORM=arm -DCMAKE_BUILD_TYPE=Release .. # make -j4 && make install #
cmake_minimum_required(VERSION 3.5)
project(freeopc_demo)
set(CMAKE_CXX_STANDARD 11)
if(CMAKE_BUILD_TYPE MATCHES "Debug" OR CMAKE_BUILD_TYPE MATCHES "None") MESSAGE(STATUS "CMAKE_BUILD_TYPE is Debug") elseif(CMAKE_BUILD_TYPE MATCHES "Release") MESSAGE(STATUS "CMAKE_BUILD_TYPE is Release") else() MESSAGE(STATUS "unknown CMAKE_BUILD_TYPE = " ${CMAKE_BUILD_TYPE}) endif()
# Other Version # # For x64 compile, please use command # cmake -DCMAKE_BUILD_TYPE=Debug .. # For arm cross compile, please use command # cmake -DPLATFORM=arm -DCMAKE_BUILD_TYPE=Release .. # make -j4 && make install #
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
/ A simple program that computes the square root of a number #include<stdio.h> #include<stdlib.h> #include<math.h> #include"TutorialConfig.h" #ifdef USE_MYMATH #include"MathFunctions.h" #endif
# does the application run add_test (TutorialRuns Tutorial 25)
# does it sqrt of 25 add_test (TutorialComp25 Tutorial 25) set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")
# does it handle negative numbers add_test (TutorialNegative Tutorial -25) set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")
# does it handle small numbers add_test (TutorialSmall Tutorial 0.0001) set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")
# does the usage message work? add_test (TutorialUsage Tutorial) set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")
#define a macro to simplify adding tests, then use it macro (do_test arg result) add_test (TutorialComp${arg} Tutorial ${arg}) set_tests_properties (TutorialComp${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result}) endmacro (do_test)
# do a bunch of result based tests do_test (25"25 is 5") do_test (-25"-25 is 0")
# does this system provide the log and exp functions? include (CheckFunctionExists) check_function_exists (log HAVE_LOG) check_function_exists (exp HAVE_EXP)
当 CMake 在平台上发现它们时,我们在 TutorialConfig.h.in 中定义这些值:
1 2 3
// does the platform provide exp and log functions? #cmakedefine HAVE_LOG #cmakedefine HAVE_EXP
// if we have both log and exp then use them #if defined (HAVE_LOG) && defined (HAVE_EXP) result = exp(log(x)*0.5); #else// otherwise use an iterative approach . . .
// A simple program that builds a sqrt table #include<stdio.h> #include<stdlib.h> #include<math.h>
intmain(int argc, char *argv[]) { int i; double result;
// make sure we have enough arguments if (argc < 2) { return1; }
// open the output file FILE *fout = fopen(argv[1],"w"); if (!fout) { return1; }
// create a source file with a table of square roots fprintf(fout,"double sqrtTable[] = {\n"); for (i = 0; i < 10; ++i) { result = sqrt(static_cast<double>(i)); fprintf(fout,"%g,\n",result); }
// close the table with a zero fprintf(fout,"0};\n"); fclose(fout); return0; }
注意这张表会以有效的 C++ 代码的形式生成,输出文件的名字以参数的形式提供。接下来向 MathFunctions 的 CMakeLists.txt 文件中添加合适的命令以构建 MakeTable 的可执行文件,并运行它作为构建过程的一部分。需要几个命令来完成此操作,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# first we add the executable that generates the table add_executable(MakeTable MakeTable.cxx)
# add the command to generate the source code add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h DEPENDS MakeTable )
# add the binary tree directory to the search path for # include files include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
# add the main library add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h )
# should we use our own math functions option(USE_MYMATH "Use tutorial provided math implementation"ON)
# configure a header file to pass some of the CMake settings # to the source code configure_file ( "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" "${PROJECT_BINARY_DIR}/TutorialConfig.h" )
# add the binary tree to the search path for include files # so that we will find TutorialConfig.h include_directories ("${PROJECT_BINARY_DIR}")
# add the MathFunctions library? if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions") add_subdirectory (MathFunctions) set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) endif (USE_MYMATH)
# add the executable add_executable (Tutorial tutorial.cxx) target_link_libraries (Tutorial ${EXTRA_LIBS})
# do a bunch of result based tests do_test (4"4 is 2") do_test (9"9 is 3") do_test (5"5 is 2.236") do_test (7"7 is 2.645") do_test (25"25 is 5") do_test (-25"-25 is 0") do_test (0.0001"0.0001 is 0.01")
TutorialConfig.h.in 如下:
1 2 3 4 5 6 7 8
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ #cmakedefine USE_MYMATH
// does the platform provide exp and log functions? #cmakedefine HAVE_LOG #cmakedefine HAVE_EXP
MathFunctions 的 CMakeLists.txt 文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# first we add the executable that generates the table add_executable(MakeTable MakeTable.cxx) # add the command to generate the source code add_custom_command ( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h DEPENDS MakeTable COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h ) # add the binary tree directory to the search path # for include files include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
# add the main library add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h)
# build a CPack driven installer package include (InstallRequiredSystemLibraries) set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") set (CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") set (CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") include (CPack)