Cmake usage Summary (1)

Source: Internet
Author: User
Preface

During the first Linux project, makefile is typed in one line. After the second project, cmake is used. I don't think cmake is a good choice,
It was just because a Linux predecessor in the project team recommended this to us. After some research and use in the project, we will summarize the usage experience for your reference.

Entry

When learning a new knowledge, it is best to start from the sample. Cmake Official Website
A simple example is provided.
. Install the cmake program before you start. It is very simple in Ubuntu. Enter the following command:

$sudo apt-get install cmake

Let's take a look at the content in the sample. Sample contains a main directory, which has two subdirectories:
Demo and hello. Hello contains the library code. In the demo, it is an executable program. You need to connect to the library in hello. There are three cmakelists.txt files.
File, next to each directory. The content of the cmakelists.txt file in the main directory is:

    # The name of our project is "HELLO".  CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
project (HELLO)
# Recurse into the "Hello" and "Demo" subdirectories. This does not actually
# cause another cmake executable to run. The same process will walk through
# the project's entire directory structure.
add_subdirectory (Hello)
add_subdirectory (Demo)

The cmakelists.txt file under hellodirectory is:

    # Create a library called "Hello" which includes the source file "hello.cxx".
# The extension is already found. Any number of sources could be listed here.
add_library (Hello hello.cxx)

The final demopack contains the cmakelists.txt file:

    # Make sure the compiler can find include files from our Hello library.
include_directories (${HELLO_SOURCE_DIR}/Hello)
# Make sure the linker can find the Hello library once it is built.
link_directories (${HELLO_BINARY_DIR}/Hello)
# Add executable called "helloDemo" that is built from the source files
# "demo.cxx" and "demo_b.cxx". The extensions are automatically found.
add_executable (helloDemo demo.cxx demo_b.cxx)
# Link the executable to the Hello library.
target_link_libraries (helloDemo Hello)

Invalid.

Literally, we can understand the meaning of the three files. The first cmakelists.txt file contains two subdirectories: Hello and demo. The second file
Specify to generate the hello library file. The third file is to generate an executable file hellodemo. The other two additional statements are used to specify the header file path and the library to be linked.

At first glance, I compiled a program and wrote three cmakelists.txt files. Isn't it more complicated than writing only one MAKEFILE file? Actually not enough
However, although three cmakelists files are to be written, each file is very simple. In total, it is not as much as a makefile. More importantly, this implies that
Linux philosophy: divide and conquer. Each module writes its own configuration file and is only responsible for its own transactions, so it is highly scalable. In large-scale project development, the advantages can be reflected.

Cmakelists.txt defines a set of rules for generating makefile files. The MAKEFILE file can be generated as follows:

$cmake .

Note: .htm is the precheck. If cmakelists.txt is not in the current directory, specify it after cmake. A makefile file is generated in the main directory and in the demo and hello subdirectories. With this file, you can input make to compile the target program.

To sum up, the process for compiling an application using cmake is as follows:

Cmake syntax

The cmake syntax is very simple and contains comments, commands, and spaces. A behavior annotation line starting with #. A command consists of a command name, Parentheses, and parameters separated by spaces. Commands can be
Built-in commands such as add_library can also be sub-defined macros or functions. The cmakeinput is the cmakelists.txt file under the main directory. This file can be used
Include or add_directory command to add other input files.

Command

The command format is as follows:

command (args ...)

<I> command </I>
Is the command name, <B> ARGs </B>
List of parameters separated by spaces. If a parameter contains spaces, double quotation marks are used. The command is case insensitive.

The basic data type of lists and strings. cmake is a string, which can also be a list type. There are two methods: one is separated by a semicolon, and the other is separated by a space. For example, the following example assigns the same value to VaR:

    set(VAR a;b;c)    set(VAR a b c) 

The string list is mainly used for foreach iteration, and some commands are also used to process the list.

Cmake supports simple variables of the string and list types. variables are referenced in the form of $ {var. You can use the set command to form a list of multiple parameters. The command will expand the list, for example:

    set(Foo a b c)
command(${Foo})

Equivalent

    command(a b c)

If you want to pass the list as a parameter to the command, you should use double quotation marks to cause the list, such as command ("$ {Foo }") equivalent to command ("a B C ")

Process Control

Writing a cmakelists.txt file is like writing a simple program. cmake provides three process control structures:

  • Conditional statement 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)
  • Loop Structure
        set(VAR a b c) 
    # loop over a, b,c with the variable f
    foreach(f ${VAR})
    some_command(${f})
    endforeach(f)
  • Macros and functions are supported only in versions 2.6 and later. The difference between a function and a macro is that a local variable can be defined in a function, while a macro variable is a global variable.
        # 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)
Summary

This document uses a simple project example to compile the cmakelists.txt file. cmake generates the makefile accordingly. Then
After a simple cmake' syntax, We can compile the cmakelists.txt file on our own. Of course, for a large project, it is not enough to grasp this.
If you want to write cmakelists.txt files, you can continue with this topic.

Bibliography

Jan
Engels
.
Cmake tutorial
.

.
Cmake Official Website: www.cmake.org

.

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.