CMake Practical Guide (i): Use of BASIC commands

Source: Internet
Author: User
Tags glob log log mkdir parent directory regular expression relative root directory
Reference URL: https://my.oschina.net/u/1046919/blog/477645
Http://www.cnblogs.com/lidabo/p/3974305.html

Basic Structure
1, dependent on the CMakeLists.txt file, the project master target one, the main directory can be specified to include subdirectories;
2, in Project CMakeLists.txt, use project to specify the project name, add_subdirectory add subdirectories
3, subdirectory CMakeLists.txt will inherit settings from parent directory CMakeLists.txt (TBD, pending test)

Internal Variables
Cmake_c_compiler: Specifying the C compiler

Cmake_cxx_compiler:

Cmake_c_flags: Options for compiling C files, such as-G; You can also add compilation options via Add_definitions

Executable_output_path: Storage path for executable files

Library_output_path: library file path

Cmake_build_type::build type (Debug, Release,) cmake_build_type=debug

Build_shared_libs:switch between SHARED and static libraries

use of built-in variables:
>> specified in CMakeLists.txt, using set
>> CMake used in commands such as Cmake-dbuild_shared_libs=off
Common commands:
CMake Dir_path, generate a project file or makefile file
Example: mkdir Build
CD Build
CMake.
Make
Role: Build the Build folder and generate the makefile file

===========================================================

Project (HELLO) #指定项目名称, the name of the generated VC project;
Use ${hello_source_dir} to represent the project root directory

===========================================================

Message command
Statement: Message ([status| Warning| Author_warning| fatal_error| Send_error] "message to display" ...)
Function: Output information
Example: Message ("Hello World")

===========================================================

File command
Statement: File (write filename "message to WRITE" ...)
Action: The Write option will write a message to the file named filename. If the file already exists, the command overwrites the existing file, and if the file does not exist, it creates the file.

Statement: File (APPEND filename "message to write" ...)
Function: The append option, like the Write option, will write a message to the file named filename, except that the message is appended to the end of the file.

Statement: file (READ filename variable [LIMIT numbytes] [offset offset] [HEX])
Role: The read option will read the contents of a file and store it in a variable. The location of the read file starts at offset and reads up to numbytes bytes.
If the hex parameter is specified, the binary code is converted to a hexadecimal representation and stored in a variable.


Statement: file (STRINGS filename variable [limit_count num] [limit_input numbytes] [limit_output numbytes]
[Length_minimum NumBytes] [Length_maximum NumBytes]
[Newline_consume]  [Regex regex] [No_hex_conversion])
Role: Strings will parse a list of ASCII strings from a file and store them in the variable variable. Binary data in the file is ignored.
Carriage return line break is ignored. It can also be used in Intel's hex and Motorola S-record files, which are automatically converted to binary format when they are read.
You can use the No_hex_conversion option to disable this feature. The Limit_count option sets the maximum number of strings that are returned. Limit_input sets the maximum number of bytes read from the input file.
Limit_output sets the maximum number of bytes stored in the output variable. Length_minimum sets the minimum length of the string to be returned, and a string less than that length is ignored.
Length_maximum sets the maximum length of the returned string, and the longer string is split into a string that is not longer than the maximum length.
The Newline_consume option allows new rows to be included in the string instead of terminating them. The Regex option specifies a regular expression that the string to be returned must satisfy.


Statement: File (GLOB variable [RELATIVE path] [globbing expressions] ...)
Function: The GLOB option will generate a list of files for all files that match the query expression and store the list in the variable variable. File name query expressions are similar to regular expressions, but are simpler. If the relative flag is specified for an expression, the returned result will be a relative path relative to the given path.

Statement: File (glob_recurse variable [RELATIVE path] [follow_symlinks] [globbing expressions] ...)
Function: The Glob_recurse option will generate a list similar to the usual GLOB option, except that it will search all the files that match the sub-paths of the directory and match the query expression.
The subpath as a symbolic link is only available if the given follow_symlinks option or CMake policy CMP0009 is set to new.
See CMake--help-policy CMP0009 for more useful information.

Statement: File (RENAME <oldname> <newname>)
Role: The Rename option renames a file or directory under the same file system.


Statement: File (REMOVE [file1 ...])
Action: The Remove option will delete the specified file, including the file under the subpath.

Statement: File (Remove_recurse [file1 ...])
Role: The Remove_recurse option deletes the given file and directory, including non-empty directories.

Statement: File (make_directory [Directory1 directory2 ...])
Role: The Make_directory option will create the specified directory and will also be created if their parent directory does not exist. (Similar to mkdir command--)

Statement: File (relative_path variable directory file)
Action: The Relative_path option determines the relative path from the Direcroty parameter to the specified file.

Statement: File (to_cmake_path PATH result)
Action: The To_cmake_path option converts path to a Unix-CMAKE-style path. The input can be a single path, or it can be a system path,
such as "$ENV {PATH}". Note that the double quotes around the env called To_cmake_path can only have one parameter (note the double quotes around the env call To_cmake_path only takes a argument. This.

Statement: File (to_native_path PATH result)
Action: The To_native_path option is similar to the To_cmake_path option, but it converts the CMAKE-style path to the local path style: Windows uses \, and UNIX uses/.

Statement: file (DOWNLOAD URL file [Timeout timeout] [status status] [LOG log] [expected_md5 sum] [show_progress])
Role: DOWNLOAD download the given URL to the specified file. If the log var option is specified, the download log will be output to Var. If the status var option is specified,
The status of the download operation is output to Var. The status return value is a list with a length of 2. The first element of the list is the numeric return value of the operation, and the second return value is the wrong string value.
Error message if it is a number 0, no error occurred in the operation. If the timeout time option is specified, the operation times out after a second, and it should be an integer. If the expected_md5 sum option is specified,
The download operation authenticates the actual MD5 of the downloaded file and whether it matches the expected value. If they do not match, the operation returns an error. If the show_progress option is specified, the progress information is printed in the form of status information,
Until the operation is complete.

===========================================================

Include_directories command

Statement: include_directories ([after| Before] [SYSTEM] dir1 dir2 ...)
Function: Used to set the directory, these settings directory will be used by the compiler to find the include file
Example: Include_directories (${project_source_dir}/lib)

Include_directories: Specifies the search path for the header file, equivalent to the-i parameter of the specified GCC
Include_directories (${hello_source_dir}/hello) #增加Hello为include目录

===========================================================

Link_directories: The search path for a dynamic-link library or a static-link library, equivalent to the-l parameter of GCC
Link_directories (${hello_binary_dir}/hello) #增加Hello为link目录

===========================================================

add_executable command

Statement: add_executable (<name> [WIN32] [macosx_bundle] [Exclude_from_all] source1 source2 ... sourcen)
Function: Compiles the specified file source into an executable file named bit name
Example: add_executable (Hello hello.cpp)

===========================================================

Target_link_libraries command

Statement: Target_link_libraries (<target> [Item1] [ITEM2 [...]] [[Debug|optimized|general]] ...)
Function: Used to specify target needs to link item1 item2 .... The target must have been created here, and the linked item can be a target that already exists (the dependency will be added automatically)
Example: Target_link_libraries (Main Lib)

Target_link_libraries (app.fcgi
Common.a
Mongoclient.a
Boost_system.a
Boost_thread.a
Boost_filesystem.a
Boost_program_options.a
Jsoncpp.a
Pthread
Rt
Z
Libfcgi.a
Sybdb
Rt
)

Target_link_libraries: Adding a link library, same as specifying the-l parameter
Target_link_libraries (demo Hello) #将可执行文件与Hello连接成最终文件demo

===========================================================

Add_subdirectory command
Statement: Add_subdirectory (Source_dir [Binary_dir] [Exclude_from_all])
Role: Used to add a subdirectory that needs to be built
Example: Add_subdirectory (directory)

Add_subdirectory: Include subdirectories
Add_subdirectory (Hello)

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.