[Reprinted] Write cmake

Source: Internet
Author: User

The cmakeinput is the cmakelists.txt file in the source code directory. This file can be added to other input files by using the include or add_subdirectory command.

Syntax

The cmakelist.txt file consists of comments, commands, and blank characters.

The comment starts from # and ends with the end of the line.

A command is composed of command names, (parameters separated by spaces, and.

Example: Command (ARGs ....)

The preceding command can be a command name, a macro, or a function name.

ARGs is a parameter example Table separated by spaces (if the parameter contains spaces, double quotation marks are required)

The blank characters (spaces, line numbers, and tabs) used to separate parameters are ignored. Any character contained in double quotation marks is used as a parameter. A backslash is used to convert codes.

The command name is case-insensitive.

String and string list (lists)

The basic data type of cmake is string ). Cmake also supports a string list composed of strings. The string list can be separated by spaces. For example, the following variable VAR is equivalent.

Set (var a; B; c)

Set (var a B C)

You can use the foreach command to perform the iterated or list operations on the string list.

Variable

Cmake supports simple variables: string or string list. Use the $ {var} syntax to reference the variable.

You can use a set command to set a string list as a variable, and then pass the variable to the function that needs to pass multiple parameters. For example:

Set (foo a B C)

Command ($ {Foo })

The above two lines are equivalent to command (a B C)

If you want to pass a string list as a separate parameter to the function, use double quotation marks to include it. For example:

Command ("$ {Foo }")

Equivalent to: Command ("a B C ")

Environment variable:

Use $ ENV {var} To get environment variable reference

Set environment variables:

Set (ENV {var}/Home)

Program Flow Control

Cmake provides three program flow control structures:

1. Condition Declaration: If

# Some_command will be called if the variable's value is not:
# Empty, 0, N, NO, off, false, notfound, or-notfound.
If (VAR)
Some_command (...)
Endif (VAR)

2. Loop Control: foreach and while

Set (var a B C)
# Loop over a, B, c with the variable F
Foreach (F $ {var })
Message ($ {f })
Endforeach (f)

3. Program definition: macros or functions (supported by functions in Versions later than cmake2.6 ). The function creates a variable in the local range, and the macro is used in the global range.

# define a macro hello macro(hello MESSAGE)  message(${MESSAGE})endmacro(hello) # call the macro with the string "hello world" hello("hello world") # define a function hello function(hello MESSAGE)  message(${MESSAGE}) endfunction(hello)

The function can return data. You can use the return () command to return data. If you want to return a value from a function, you can only return the value through the parameter:

# Define the function get_lib to find the specified library from the given directory and pass it back to the lib_file parameter.

function(get_lib lib_FILE lib_NAME lib_PATH)#message("lib_name:""${lib_NAME}")    set(__LIB "__LIB-NOTFOUND")    #message("__LIB:""${__LIB}")    find_library(__LIB ${lib_NAME} ${lib_PATH})    if(__LIB STREQUAL "__LIB-NOTFOUND")        message("don‘t find ${lib_NAME} librarys in ${lib_PATH}")        return()    endif()    #message("__LIB:""${__LIB}")    set(${lib_FILE} ${__LIB}PARENT_SCOPE)endfunction(get_lib)

In the SET command, parent_scope indicates the variables passed to the function caller.

Template

├── bin│   └── main├── build├── CMakeLists.txt├── doc├── README└── src    ├── CMakeLists.txt    ├── libs    │   ├── hello.cpp    │   └── hello.h    ├── main.cpp    └── modules

Cmakelists.txt in the root directory:

# Project name Project (Hello) # minimum declared version cmake_minimum_required (version 2.6) Set (cmake_c_flags "$ {cmake_c_flags}-g-wall-O2 ") set (cmake_cxx_flags "$ {cmake_cxx_flags}-g-wall-O2") # Add a subdirectory to be compiled. I understand it as: Compile the next cmakelists.txt add_subdirectory (SRC)

In SRC:

# Add the search directory of the header file & in the compilation parameter "-l", specify the path of the search header file include_directories ($ {project_source_dir}/src/libs) Set (sec_list main. CPP libs/hello. CPP) add_executable (main $ {sec_list}) # target_link_libraries is used to link the file dependency after-L # target_link_libraries (crnode thread) # executable_output_path & library_output_path is used to specify the binary file location and library file path, which does not include the intermediate file set (executable_output_path $ {project_binary_dir}/bin) # Install please refer to the http://now-code.com/archives/208

Directory containing the Library:

├── bin├── build├── CMakeLists.txt├── doc├── README└── src    ├── apps    │   ├── CMakeLists.txt    │   └── main.cpp    ├── CMakeLists.txt    ├── includes    │   └── hello.h    ├── libs    │   ├── CMakeLists.txt    │   └── hello.cpp    └── modules

Under the root directory:

PROJECT(hello)cmake_minimum_required(VERSION 2.6)SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O2")add_subdirectory(src)

Src-> apps:

Set (myexe_exec_srcs main. CPP # other CPP files) # Be sure to include main. CPP contains "*. H "file include_directories ($ {project_source_dir}/src/Includes/# $ {project_binary_dir}/src/libs/# $ {project_source_dir}/src/libs/markup # $ {project_source_dir} /src/modules/#/opt/ICE/include) # library file path link_directories (/usr/local/lib # $ ENV {ORACLE_HOME}/LIB) # Add a self-generated dynamic library/static library to set the Link Library required for the target # link_libraries (myexe markup) link_libraries (main hellolib) add_executable (main $ {myexe_exec_srcs }) set (executable_output_path $ {project_source_dir}/bin/) # Install (targets main runtime destination bin)

Src-> libs:

Set (utils_lib_srcs hello. CPP # other CPP files) include_directories ($ {project_source_dir}/src/Includes # $ {project_source_dir}/src/libs/markup # $ {project_source_dir}/src/libs) link_directories (/usr/local/lib # other library paths) # link_libraries (utils markup) # link_libraries (utils UUID) # staticadd_library (hellolib shared $ {utils_lib_srcs}) Set (library_output_path $ {project_binary_dir}/src/libs /)

Reprinted from

Cmake use -- cmake syntax http://blog.csdn.net/kl222/article/details/7519340

Cmake template and syntax http://blog.csdn.net/feibuhui123/article/details/8552719

Recommended reading

Cmake Study Notes (I) http://blog.csdn.net/dbzhang800/article/details/6314073

Cmake 2.8.8 documentation http://www.cmake.org/cmake/help/v2.8.8/cmake.html.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.