Use CMake in linux to build applications

Source: Internet
Author: User
Tags windows visual
Article Title: Use CMake in linux to build applications. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

This article describes how to use CMake, a cross-platform automated build system, on linux. CMake is a tool easier to use than automake, which can free programmers from complicated compilation and connection processes. This article describes how to use CMake to process multi-source file directories, how to find and use other development kits, and how to Generate debug and release programs.
CMake Introduction

CMake is a cross-platform automated construction system. It uses a file named CMakeLists.txt to describe the building process and generate standard building files, for example, Makefile of Unix or projects/workspaces of Windows Visual C ++. The file CMakeLists.txt needs to be manually written. You can also write a script to generate it semi-automatically. CMake provides more concise syntax than autoconfig. The process for generating and compiling Makefile using CMake on linux is as follows:

1. Write CmakeLists.txt.
2. Run "cmake PATH" or "ccmake PATH" to generate the Makefile (PATH is the directory where CMakeLists.txt is located ).
3. Use the make command for compilation.

First Project

Assume that there is only one source file main. cpp in our project.

Listing 1 source file main. cpp

1 #include
     
      2 3 int main()4 {5     std::cout<<"Hello word!"<
      
     

To build this project, we need to write the file CMakeLists.txt and put it in the same directory as main. cpp:

Listing 2 CMakeLists.txt

1 PROJECT(main)2 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)3 AUX_SOURCE_DIRECTORY(. DIR_SRCS)4 ADD_EXECUTABLE(main ${DIR_SRCS})

The syntax of CMakeLists.txt is simple and consists of commands, comments, and spaces. commands are case-insensitive and the content after the symbol "#" is considered as comments. The command consists of the command name, Parentheses, and parameters. The parameters are separated by spaces. For example, for the CMakeLists.txt file in Listing 2, the first line is a command named PROJECT and the parameter is main. This command indicates that the PROJECT name is main. The command line 2 limits the CMake version. The third line uses the command AUX_SOURCE_DIRECTORY to assign the source file name in the current directory to the variable DIR_SRCS. The description of the command AUX_SOURCE_DIRECTORY in the CMake manual is as follows:

 

aux_source_directory(
      
      
       ) 
      
     

This command sets the Parameter

All source file names are assigned to parameters. . The fourth line uses the command ADD_EXECUTABLE to indicate that the source file in the DIR_SRCS variable needs to be compiled into an executable file named main.

After compiling the file CMakeLists.txt, you must use the cmake or ccmake command to generate the Makefile. The difference between ccmake and cmake is that ccmake provides a graphical operation interface. The cmake command is executed as follows:

cmake [options] 
        
       

Here we enter the directory where main. cpp is located and run "cmake." To Get The Makefile and compile it using make, as shown in.

Figure 1. camke running result

[1] [2] [3] [4] Next page

Related Article

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.