cmake basic introduction (1)

Keywords: cmake Linux less Unix

cmake basic introduction (1)

Official help documents

CMake is a cross platform make.

CMakeLists.txt The brief introduction is as follows:

# Auto CMakeLists.txt: frame  with  cppunit  use for lintcode/leetcode 
# minimum  cmake  version  
cmake_minimum_required(VERSION 3.7)   # cmake minimum version number requirements
PROJECT(demo)                         # Project name
SET(mname  demo  )                    # Assign demo to variable mname
SET( cppunit_test  cppunit_demo_test )
SET( ROOTPATH ${CMAKE_SOURCE_DIR})

# defining common source variables
aux_source_directory(main   SRC )            # Find all the source files in the main directory and save the file name to SRC
aux_source_directory(test_cppunit  TESTSRC ) # test_ Save all source files in CPPUnit directory to TESTSRC

# add_subdirectory(sub): contains the subdirectory sub. Compile the CMakeLists.txt And source code;
MESSAGE(STATUS "project name:  " ${PROJECT_NAME})
SET(LIBRARY_OUTPUT_PATH ${ROOTPATH}/lib)   # Set build library output directory
SET(EXECUTABLE_OUTPUT_PATH ${ROOTPATH}/bin)    # Set executable output directory (target)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${ROOTPATH}/bin/Debug ) 
MESSAGE(STATUS "library_output_path   : " ${LIBRARY_OUTPUT_PATH} )
MESSAGE(STATUS "executable_output_path: " ${EXECUTABLE_OUTPUT_PATH} )
MESSAGE(STATUS "cmake_runtime_output_directory_debug: " ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG} )

add_definitions(-DTYPEDEBUG)  #Add macro definition TYPEDEBUG
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

MESSAGE(">>>> cppbuilder init start:")
if(SRC) 
    add_definitions("-g -Wall -std=c++11")
    include_directories(include)            #Include directory, specify search path of header file;
    add_executable( t_${mname}   ${SRC} )   #Generate executable
    ADD_LIBRARY( ${mname} STATIC  ${SRC})   #Build STATIC library replace STATIC with SHARED build dynamic library
    #target_link_libraries( ${mname}  childlib  )  #  Add link library
    SET(LIBRARY_OUTPUT_PATH ${ROOTPATH}/lib) 
     
    #ADD_LIBRARY( ${mname}  SHARED ${SRC})   
    MESSAGE("project src file show: ")
        foreach(_var ${SRC})
        MESSAGE("   ${_var}")
        endforeach()
        MESSAGE(" ")
    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${ROOTPATH}/bin/Debug ) 
endif(SRC)

MESSAGE(">>>> cppbuilder init end <<<<")

1.1 introduction to basic grammar

1 set variable

###  character string
set(mystring "hello wold  " "xiaojiejie  ")
string(APPEND mystring "xiaogege ")
message("======:  " ${mystring} )
###  list
set(mylist "c " "c++ " "java " "python ")
set(mylist ${mylist} "lua ")
list(APPEND mylist "php "  "javascript ")
message("======:  " ${mylist} )

There are two ways to define cmake variables: implicit and explicit.

  • An example of implicit definition is given before implicit definition. It is the PROJECT instruction, which implicitly defines BINARY_DIR and SOURCE_DIR two variables.
  • 2. As we mentioned earlier, we can build a custom variable by using the SET instruction. ${valuename} to reference the variable
CMake common variables
Variable name Variable description
PROJECT_NAME Returns the PROJECT name defined by the PROJECT directive
PROJECT_SOURCE_DIR Cmake source address, that is, the address specified after the cmake command
PROJECT_BINARY_DIR The directory where the cmake command is run, usually project_ SOURCE_ build directory under dir
CMAKE_MODULE_PATH Define the path of your own cmake module
CMAKE_CURRENT_SOURCE_DIR Currently processed CMakeLists.txt Path
CMAKE_CURRENT_LIST_DIR Current folder path
CMAKE_CURRENT_LIST_FILE The output calls the CMakeLists.txt Full path to
CMAKE_CURRENT_LIST_LINE Output the line of this variable
CMAKE_RUNTIME_OUTPUT_DIRECTORY Build executable path
CMAKE_LIBRARY_OUTPUT_DIRECTORY Folder path to build library
CMAKE_BUILD_TYPE Specify the build type of the make based generator (Release, Debug)
CMAKE_C_FLAGS *. C file compilation options, such as - std=c99 -O3 -march=native
CMAKE_CXX_FLAGS *. CPP file compilation options, such as - std=c++11 -O3 -march=native
CMAKE_CURRENT_BINARY_DIR target compilation directory
CMAKE_INCLUDE_PATH Environment variable, non cmake variable
CMAKE_LIBRARY_PATH environment variable
CMAKE_STATIC_LIBRARY_PREFIX Static library prefix, which is lib by default under Linux
CMAKE_STATIC_LIBRARY_SUFFIX Static library suffix, default is. a under Linux
CMAKE_SHARED_LIBRARY_PREFIX Dynamic library prefix, which is lib by default under Linux
CMAKE_SHARED_LIBRARY_SUFFIX Dynamic library suffix, default is. so under Linux
BUILD_SHARED_LIBS If ON, add_library create shared library by default
CMAKE_INSTALL_PREFIX Configure the installation path, which is / usr/local by default
CMAKE_ABSOLUTE_DESTINATION_FILES Use the ABSOLUTE DESTINATION path when installing the file list
CMAKE_AUTOMOC_RELAXED_MODE Switch between strict and loose automoc mode
CMAKE_BACKWARDS_COMPATIBILITY Build the CMake version required by the project
CMAKE_COLOR_MAKEFILE When on, using the Makefile generator produces color output
CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS Used to control the writing mode of IF ELSE statements
System variable
  • CMAKE_ MAJOR_ Version, cmake major version number, such as 2 in 2.4.6
  • CMAKE_ MINOR_ Version, cmake minor version number, such as 4 in 2.4.6
  • CMAKE_ PATCH_ Version, cmake patch level, such as 6 in 2.4.6
  • CMAKE_SYSTEM, system name, such as Linux-2.6.22
  • CMAKE_SYSTEM_NAME, excluding the system name of the version, such as Linux
  • CMAKE_SYSTEM_VERSION, system version, such as 2.6.22
  • CMAKE_SYSTEM_PROCESSOR, processor name, such as i686
  • UNIX, TRUE on all UNIX like platforms, including OS X and cygwin
  • win32, TRUE on all win32 platforms, including cygwin

2 process control

set(VAR1  "world")
if(VAR1 MATCHES "Hello")
    message("this is hello")
    message("this is hello2")
elseif(VAR1 MATCHES "world")
    message("this is world")
    message("this is world2")
else()
    message(" un expect value")
endif()


foreach(_value ${mylist})
    message("  " ${_value} )
endforeach(_value ${})
# foreach(loop_var RANGE start stop [step]). In foreach loop, support break() and continue().
set(result 0)
foreach(_var RANGE 0 100)
     math(EXPR result "${result}+${_var}")
endforeach()
message("from 0 plus to 100 is:${result}")

#Size used to compare numbers LESS: LESS than grader: GREATER than EQUAL: EQUAL to grader_ EQUAL: GREATER than or EQUAL to LESS_EQUAL: LESS than or EQUAL to
#Used to compare string LESS: LESS than structer: greater than equal: equal to struct_ Equal: LESS than or equal to Strider_ Equal: greater than or equal to
set(i 5 )
while( i GREATER 0 )
    message("i " ${i})
    math(EXPR i "${i} -1 ")
endwhile()

3 macro and function

function( [arg1 [arg2 [arg3 ...]]]) COMMAND1(ARGS ...) COMMAND2(ARGS ...) ... endfunction()

Define a function with the name arg1 arg2 arg3( ). The commands in the function body are not executed until the function is called.

variable explain
ARGV# #Is a subscript, 0 points to the first parameter, cumulative
ARGV All definitions require parameters passed in
ARGN When defining, parameters other than those passed in are required. For example, when defining a macro (function), one is required, and the other three are entered by the secretary. Then the remaining two will be stored in ARGN as arrays
ARGC The number of actual parameters passed in, that is, the number of parameters passed in when the calling function is called
macro(FOO arg1 arg2 arg3)
    message(STATUS "this is arg1:${arg1},ARGV0=${ARGV0}")
    message(STATUS "this is arg2:${arg2},ARGV1=${ARGV1}")
    message(STATUS "this is arg3:${arg3},ARGV2=${ARGV2}")
    message(STATUS "this is argc:${ARGC}")
    message(STATUS "this is args:${ARGV},ARGN=${ARGN}")
endmacro(FOO)


function(FUNCTION_TEST arg1)
    message("\n")
    message(STATUS "this is arg1:${arg1} ARGV0=${ARGV0}")
    message(STATUS "this is argn:${ARGN}")
    message(STATUS "this is ARGC:${ARGC}")
    message("\n")
endfunction(BAR arg1)

set(p1 one)
set(p2 two)
set(p3 three)
set(p4 four)
set(p5 five)
set(p6 first)
set(p7 second)

FOO(${p1} ${p2} ${p3} ${p4} ${p5})

FUNCTION_TEST(${p6} ${p7} ${p1} )

Posted by Cyberspace on Sun, 07 Jun 2020 00:39:06 -0700