Cmake usage Summary (2)

Source: Internet
Author: User

Preface

In the previous articleArticleStarting with a sample, we learned the basic usage and syntax of cmake. However, there is still a gap between this example and actual development, and the following problems exist:

    • Binary generatedProgramMixed with the source program
    • Use GCC process programs instead of cross-compilation tools
    • To specify the compilation option, the debug version is usually generated for trial use. The release version is used for release.

In this chapter, we will gradually transform the sample program to solve the above problems.

Project file organization

A project usually contains several submodules. For example, in the previous sample, we can think that it contains two sub-modules. Hello is the library, and demo is the main program. Few projects
When the target binary file is put together with the source program, a bin directory is usually created to store the generated binary file, and the release program is put in release. According to my habits in project development
The structure is modified as follows:

 

Cmakesample
| --- Release
| --- Doc
| --- Lib
| --- Source
| --- Include
| --- Bin
| --- Hello
| --- Demo

Among them, release stores program publishing related files, including program files, scripts, parameters, and so on. Doc contains related documents in project development, such as design instructions and tools such as doxgen
SlaveCode. Lib stores the third-party libraries used in the project. The libraries written by the project are not stored in this directory and should be placed in the source directory as a module of the project. Include
Contains the public header files used in the entire project. If the header files in the sub-module are only used in the sub-module class, they do not need to be placed in the include directory. The bin directory stores the compiled debugging version code. Other sub-accounts
The directory is the code and header file of each module.

According to the above directory structure, move hello. h under hello to the include directory because this header file is included by the Demo module. The third-party library is not used in this sample, so it is empty for the moment.

Frequently Used built-in Variables

From the above, we know that you can use the set statement to customize variables. However, cmake also contains a large number of built-in variables, which are no different from the usage of custom variables, some common variables are listed below:

    • Cmake_c_compiler

      Specifies the C compiler. Generally, cmake can automatically detect the C language compiler during runtime. When developing an embedded system, you usually need to set this variable to specify the cross compiler.

    • Cmake_cxx_compiler

      Specify the C ++ Compiler

    • Cmake_c_flags

      Specifies the compilation option when compiling the c file, for example,-G specifies to generate debugging information. You can also use the add_definitions command to add compilation options.

    • Executable_output_path

      Specifies the path for storing executable files.

    • Library_output_path

      Specifies the path for storing the library file

Common commands

In addition to the built-in variables, we can also use commands to modify the compilation options. Some Common commands are listed as follows:

    • Include_directories

      Specify the header file search path, which is equivalent to specifying the-I parameter of the GCC compiler.

    • Link_directories

      The search path of the dynamic or static Link Library, which is equivalent to setting the-l parameter of GCC.

    • Add_subdirectory

      Contains subdirectories. This command is useful when the project contains multiple subdirectories.

    • Add_definitions

      Add compilation parameters. For example, add_definitions (-ddebug) adds a debug macro definition to the GCC command line.

    • Add_executable

      Compile executable programs

    • Target_link_libraries

      Specify the Link Library, which is the same as the specified-l parameter.

Summary

Ben
After modifying the directory structure, specify the compilation tool chain, generate dynamic link library, and modify the preceding sample to an embedded environment close to the actual project. Of course, this sample still only
It is a self-entertaining little thing, but it is enough to explain the usage of cmake. With these basic knowledge of cmake, it is possible to use cmake in projects. Of course, different environments will encounter
With the help of the Internet, there are no new problems that cannot be solved. Hurry up and get rid of the pain of writing makefile manually. Join the cmake user group.

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.