CMake Simple usage

Source: Internet
Author: User

CMake is a cross-platform compilation tool, similar to the Automake

installation

# CD cmake-2.8.10.2
#/bootstrap # make
Install

Project

Project (<projectname> [CXX] [C] [JAVA])

Defines the name of the project and the supported language. This line introduces two variables (Hello_binary_dir and Hello_source_dir)

Set

Set (Var[value] [CACHE TYPE docstring [FORCE]])

Explicit definition of variables

Set (Src_list hello.c)

Message

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

Output user information to the terminal satus: Output information Send_error: Error generated, build process skipped Fatal_error: immediately terminate all cmake processes

Message (STATUS ' This is SOURCE dir ' ${hello_source_dir})

add_executable

Add_executable (<name> [WIN32] [macosx_bundle] [Exclude_from_all]

Introduces an executable file for the project using the given source file

Add_executable (${project_name} ${src_list})

examples

Project (Hello C CXX)

set (src_list helloworld.c) message

(STATUS ' This is BINARY dir ' ${hello_binary_dir})
Message (STATUS ' This is SOURCE dir ' ${cmake_source_dir} '

add_executable (${project_name} ${src_list})

Common variable names Project_name:project directive defines the project name Cmake_source_dir/project_source_dir: Project top-level directory cmake_current_list_file: This variable's CMakeLists.txt full path Cmake_current_list_line: The row where this variable is located Executable_output_path/library_output_path: The final result of the storage directory

Common Instructions
Include_directories: The location of the required header files for the program

Link_directories: The location of the required library files for the program

Target_link_libraries: Link library name

Target_link_libraries (${project_name} pthread)
target_link_libraries (${project_name}-lzlog)

Note: Target_link_libraries to be placed behind the add_executable, or report the following error

CMake Error at Cmakelists.txt:11 (target_link_libraries):
  cannot specify link libraries for Target "lwm2mclient" whic H is isn't built by this
  project.

Build_shared_libs: Generated as dynamic library

SET (Build_shared_libs on)

Add_library: Defines the name of the generated library, as well as the type and source files required

Add_library (Libname [shared| static| MODULE] [Exclude_from_all] source1 source2 ... sourcen)

add_library (Myhello ${src_list})//name do not conflict

# make
[ 50%] Built target Hello
scanning dependencies of target Myhello
[100%] Building C Object cmakefiles/myhello.dir/h ELLOWORLD.O
linking C Static library LIBMYHELLO.A

Add_definitions: Add-D definition. Make #ifdef Enable_debug Effective

Add_definitions (-denable_debug ...)

Add_test/enable_testing:

Add_test (testname exename arg1 arg2 ...)
Add_test (mytest./bin/hello)
enable_testing ()

Aux_source_directory: Temporarily build a list of source files. Find all source code files in a directory and store the list in a variable

Aux_source_directory (dir VARIABLE)
add_executable (${project_name} ${src_list})

Cmake_minimum_required: Version limit

cmake_minimum_required (version versionnumber [fatal_error])
cmake_minimum_required (version 2.8 Fatal_error)

Install:make INSTALL Use

INSTALL (PROGRAMS bin/hello destination bin)

# cmake-dcmake_install_prefix=./demo/.
# make install
install the project ...
--Install configuration: ""
-Installing:/home/thomas/demo/bin/hello

Add GDB Debugging

# Cmake-dcmake_build_type=debug.

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.