"Turn" CMAKE

Source: Internet
Author: User

Use of CMake

Version 1.0

2009-3-18

First, basic use

Installation: After downloading the binary package, it can be extracted directly.

Install from source to execute the command:./bootstrap; Make Make install--attempt to perform bootstrap failure

Use: CMake dir_path, generate a project file or makefile file

Second, the concept

Out-of-source Build, in contrast to In-source build, will compile output files and source files into different directories;

III. 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)

Iv. Grammar

1. #注释

2. Variables: Use the SET command to explicitly define and assign a value, in a non-if statement, using the ${} reference, if in the direct use of the variable name reference, the subsequent set command will clean up the value of the original variable;

3. Command (args ...) #命令不分大小写, parameters are separated by a space, using double quotation marks to enclose the parametric hollow lattice

4. Set (Var a;b;c) <=> set (var a b c) #定义变量var并赋值为a; b;c such a string list

5. Add_executable (${var}) <=> add_executable (a b c) #变量使用 ${xxx} reference

6. Conditional statements:

if (Var) #var not empty 0 N no OFF FALSE ... #非运算使用NOT

...

Else ()/elseif () endif (VAR)

7. Looping statements

Set (VAR a b c)

Foreach (f ${var}) ... Endforeach (f)

8. Looping statements

while () ... Endwhile ()

Five, 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

Vi. Order

Project (HELLO) #指定项目名称, the name of the generated VC project;

>> using ${hello_source_dir} to represent the project root directory

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_subdirectory: Include subdirectories

>> add_subdirectory (Hello)

add_executable: Compile executable program, specify compile, as if you can add. o File

>> add_executable (Hellodemo demo.cxx demo_b.cxx) #将cxx编译成可执行文件--

add_definitions: Adding compilation parameters

>> add_definitions (-ddebug) will add the Debug macro definition on the GCC command line;

>> add_definitions ("-wall-ansi–pedantic–g")

target_link_libraries: Adding a link library, same as specifying the-l parameter

>> target_link_libraries (demo Hello) #将可执行文件与Hello连接成最终文件demo

add_library:

>> add_library (Hello hello.cxx) #将hello. Cxx compiled into a static library such as LIBHELLO.A

add_custom_target:

message (Status|fatal_error, "message"):

set_target_properties (...): Lots of properties ... Output_name, VERSION, ....

link_libraries (Lib1 lib2 ...): All targets link with the same set of Libs

Vii. description

1,cmake generated makefile can handle well. h files only compile the required CPP files when the file is changed;

Viii. FAQ1) How to get all the source files in a directory

>> aux_source_directory (<dir> <variable>)

>> saves all source files in Dir (excluding header files) to the variable variable and can then be used add_executable (SS7GW ${variable}).

2) How to specify the project compilation target

The >> Project command specifies

3) How to add a dynamic library and a static library

>> target_link_libraries command can be added

4) How to print a message when executing CMake

>> message ([Send_error | STATUS | Fatal_error] "message to display" ...)

>> Note case

5) How to specify the header file and library file path

>> Include_directories and Link_directories

>> can be called multiple times to set multiple paths

>> link_directories only the targets behind it Works

6) How to distinguish debug, release version

>> build debug/release two directory, in which to execute Cmake-dcmake_build_type=debug (or release), you need to compile different versions of the different directories to execute make;

Debug version will use the parameter-G ; Release version using-o3–dndebug

>> another setup method-for example, debug version set compilation Parameters Ddebug

IF (Debug_mode)

Add_definitions (-ddebug)

ENDIF ()

Add parameters when executing CMake, such as Cmake-d Debug_mode=on

7) How to set up conditional compilation

For example debug version set compilation option Debug, and change should not change CMakelist.txt

>> using option Command,eg:

Option (Debug_mode "on for DEBUG or OFF for release" on)

IF (Debug_mode)

Add_definitions (-ddebug)

ENDIF ()

>> How to make it effective: first cmake generate makefile, then makes Edit_cache edit compilation options; Linux opens a text box that you can change, and then make to generate the target file--emacs does not support make Edit_ Cache

>> limitations: This method cannot directly set the generated makefile, but must use the command to set the parameters before make, for the debug, release version, the equivalent of two directories, first CMake once, and then make Edit_ Cache one time;

>> Desired Effect: Specify a switch item directly by parameter when executing CMake, generate corresponding makefile--can do so, for example Cmake–ddebugversion=on

8) How to add a compilation macro definition

>> using the Add_definitions command, see the command section for instructions

9) How to add a compilation dependency

Used to ensure that the current dependency on the compilation target must be built first.

>>add_dependencies

10) How to specify the target file directory

>> Create a new directory in which to execute the cmake generated makefile file so that the compilation results are saved in the directory-similar

>> set_target_properties (SS7GW PROPERTIES

Runtime_output_directory "${bin_dir}")

11) Many folders, do you need to compile each folder into a library file?

>> you can specify subdirectories directly in the top-level directory by not using CMakeList.txt in subdirectories

12) How to set the dependent CMake version

>>cmake_minimum_required (VERSION 2.6)

13) How the relative path is specified

>> ${projectname_source_dir} represents the source file directory, ${projectname _binary_dir} represents the root binary file directory?

14) How to set the directory to compile intermediate files

>> TBD

15) How to use string or numeric comparisons in an IF statement

>> number comparison Less, GREATER, EQUAL, string than Strless, Strgreater, Strequal,

>> Eg:

Set (Cmake_allow_loose_loop_constructs on)

Set (AAA ABC)

IF (AAA strequal ABC)

message (STATUS "true") # should print true

ENDIF ()

16) whether to compile only the necessary CPP files when changing h files

>> is

17) The VC7 and Vc8,cmake installed on the machine will automatically search for the compiler, but how can I specify a version?

>> TBD

18) How to specify compilation options based on OS

>> IF (APPLE); IF (UNIX); IF (WIN32)

19) Can I perform some pre-and post-compilation commands automatically?

>> Yes, TBD.

20) How to print make output

Make verbose=1

"Turn" CMAKE

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.