Introduction to Cmake and the use of Cmake practices-I think it's better to know what I 've learned-C ++ blog

Source: Internet
Author: User

Introduction to Cmake and the use of Cmake practices-I think it's better to know what I 've learned-C ++ blog

Cmake introduction and Cmake practices

Advantages of Cmake:

1. Development source code, license class BSD license release.

2. cross-platform, native compilation configuration files can be generated, makefile can be generated on linux/unix platforms, xcode can be generated on mac platforms, and msvc project configuration files can be generated on windows platforms.

3. Ability to manage large projects

4. Simplify the compilation and build processes. You only need cmake + make.

5. High Efficiency

6. scalability. You can compile modules with specific functions for cmake and expand the cmake function.

How to install cmake

1. You can use autotoolsto install cmake. click the cmake-2.8.6.tar.gz link to download the software.

2../configure

3. make

4. sudo make install

 

Cmake principles

 

Helloworld cmake

// Main. cpp

# Include <cstdio>

 

Int main ()

{

Printf ("hello world from main \ n ");

Return 0;

}

 

Create cmakelists.txt (note that a letter is case-insensitive)

Add the following lines to the file (which will be explained later)

PROJECT (HELLO)

SET (SRC_LIST main. cpp)

MESSAGE (STATUS "This is BINARY dir" $ {HELLO_BINARY_DIR })

MESSAGE (STATUS "This is SOURCE dir" $ {HELLO_SOURCE_DIR })

ADD_EXECUTABLE (hello $ {SRC_LIST })

Run the following command:

Cmake. (do not forget to add this point to indicate the current directory)


Note that after this statement is executed, several files will be generated as follows:


CMakeFiles, CMakeCache.txt, cmake_install.cmake, and other files are generated.

Then execute make to generate the executable file hello


This is the executable file generated in the current directory, for example:


Explanation of the example:

The content of CMakeLists.txt is as follows:

PROJECT (HELLO)

SET (SRC_LIST main. cpp)

MESSAGE (STATUS "This is BINARY dir" $ {HELLO_BINARY_DIR })

MESSAGE (STATUS "This is SOURCE dir" $ {HELLO_SOURCE_DIR })

ADD_EXECUTABLE (hello $ {SRC_LIST })

 

The Project command syntax is:

PROJECT (projectname [CXX] [C] [JAVA])

This execution is used to define the name of the project and define the language supported by the project. This command also implicitly defines two cmake variables: <projectname> _ BINARY_DIR and <projectname> _ BINARY_DIR. Here HELLO_BINARY_DIR and HELLO_SOURCE_DIR. Both variables refer to the path of the current project.

SET command syntax:

SET (VAR [VALUE] [cache type docstring [FORCE])

The Set command is used to explicitly define variables. We used SET (SRC_LIST main. cpp) if multiple source files exist, it can also be defined as SET (SRC_LIST main. cpp t1.cpp t2.cpp ).

 

The syntax of the MESSAGE command is:

MESSAGE ([SEND_ERROR | STATUS | FATAL_ERROR] "message to display "...)

This command is used to output user information to the terminal, including three types:

SEND_ERROR: an error is generated and the generation process is skipped.

SATUS, which outputs Information prefixed.

FATAL_ERROR: Terminate all cmake processes immediately.

Here we use the STATUS information output, showing the top two dietary variables HELLO_BINARY_DIR and HELLO_SOURCE_DIR under the PROJECT command.

 

ADD_EXECUTABLE (hello $ {SRC_LIST })

If this project is defined, an executable file named hello is generated. The source file is the list of source files defined in SRC_LIST. In this example, you can directly write ADD_EXECUTABLE (hello main. c ).

 

Change this example to the simplest cmakelists.txt:

PROJECT (HELLO)

ADD_EXECUTABLE (hello main. c)

 

Next we will introduce a more practical example, that is, the cmake demo that includes generating static libraries and introducing external header files and link libraries.

First, create a project directory according to the project specifications and write code. The following project directory is used as an example to explain this example. the directory structure of the project is as follows:

 

 

Goals of the compilation project:

1. The Notebook sub-directory is used for storing the project's document hello.txt

2. Generate the hello static library and link the hello static library in the main executable program

3. Add COPYRIGHT and README to this project

4. Add a run. sh script to the project directory to call the generated binary executable file.

5. Generate the binary file to the bin subdirectory.

6. Compile the installation program

 

1. Compile cmakelists.txt

A project directory contains multiple projects, including the util project and main project. the util Project is a static library used to generate the main program, main is used to generate executable files.

. The content of cmakelists.txt in the project directory is defined as follows:

PROJECT (HELLO)

ADD_SUBDIRECTORY (src)

In src, cmakelists.txt is used to define the dependencies between the two projects contained in the src directory for compilation.

Cmakelists.txt in the utilcatalog is used to define the rules for generating util static libraries. The content is as follows:

SET (LIBRARY_OUTPUT_PATH $ {HELLO_SOURCE_DIR}/lib)

SET (CMAKE_C_COMPILER g ++)

SET (SRC_LIST hello. c)

 

INCLUDE_DIRECTORIES ($ {HELLO_SOURCE_DIR}/include)

ADD_LIBRARY (util STATIC $ {SRC_LIST })

SET (LIBRARY_OUTPUT_PATH $ {HELLO_SOURCE_DIR}/lib) defines the path generated by the library. LIBRARY_OUTPUT_PATH is an internal variable that stores the path generated by the library.

SET (SRC_LIST hello. c) is the source file used to define the library file.

INCLUDE_DIRECTORIES ($ {HELLO_SOURCE_DIR}/include) is used to define the path to be searched by non-standard library header files. The format of the INCLUDE_DIRECTORIES command is:

INCLUDE_DIRECTORIES ([AFTER | BEFORE] [SYSTEM] dir1 dir2 ...)

ADD_LIBRARY (util STATIC $ {SRC_LIST}) is used to define the name of the generated library, the type of the generated library, and the source files required for the generated library. The ADD_LIBRARY command format is:

ADD_LIBRARY (libname [SHARED | STATIC | MODULE]

[EXCLUDE_FROM_ALL]

Source1 source2... sourceN)

SET (CMAKE_C_COMPILER g ++) is used to define the c compiler as g ++ to prevent the use of gcc by default when C and C ++ Code are not specified, this causes confusion in system compilation.

In the maindirectory, cmakelists.txt is used to define some commands or environments required for executable program compilation and linking. The content is as follows:

SET (EXECUTABLE_OUTPUT_PATH $ {HELLO_SOURCE_DIR}/bin)

SET (SRC_LIST main. cpp)

 

INCLUDE_DIRECTORIES ($ {HELLO_SOURCE_DIR}/include)

LINK_DIRECTORIES ($ {HELLO_SOURCE_DIR}/lib)

 

ADD_EXECUTABLE (hello $ {SRC_LIST })

TARGET_LINK_LIBRARIES (hello util)

The INCLUDE_DIRECTORIES command defines the include folder of the project, where the header file of the library used is stored, and LINK_DIRECTORIES is the library file of the definition project, where the library file is stored, ADD_EXECUTABLE is the executable file generated by definition. TARGET_LINK_LIBRARIES is used to define the library files required for the link.

2. Create the build directory under the project directory and compile the project in the out-of-source mode. Run make... and the execution result is as follows:


Execute make. An intermediate compilation file is generated under the build directory:


Run the make command. The result is as follows:


You can see that the project has been created and compiled successfully.

2. Installation

In the project directory, copy copyright‑readme‑and run.sh to recompile the cmakelists.txt file in the project directory. Add the following command to cmakelists.txt:

INSTALL (files copyright readme destination share/doc/cmake_demo)

INSTALL (PROGRAMS run. sh DESTINATION bin)

INSTALL (PROGRAMS bin/hello DESTINATION bin)

INSTALL (DIRECTORY doc/DESTINATION share/doc/cmake_demo)

These commands indicate that when the make install command is executed, the installer will copy the corresponding files, directories, or programs to the directory starting with the specified prefix. The cmake command is as follows:

Cmake-DCMAKE_INSTALL_PREFIX = ~ /Data/cmake_demo .. install the project directory ~ /Data/cmake_demo directory. The execution result is as follows:


Cmake has compiled c and c ++ projects.

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.