Basic CMake tutorial

Source: Internet
Author: User

Basic CMake tutorial

If you need to configure and check all dependencies in our project, you can use the CMake tool. However, this is not necessary, because we can use other tools or IDE (such as Makefiles or Visual Studio) to configure our project. However, CMake is the best way to configure multi-platform C ++ projects.

1. Basic usage

Cmakeuses a file named cmakelists.txt, which defines the compilation and dependency processing processes. For a basic project, create an executable program from a source code file and add two lines of code to the cmakelists.txt file. The file content is as follows:

cmake_minimum_required (VERSION 2.6)project (CMakeTest)add_executable (${PROJECT_NAME} main.cpp)

The project file specifies the cmake function defined by the given version that can be used from the second line. The project function of the second line is used to define the project name (CMakeTest here ), this name is saved in a variable named PROJECT_NAME. The last line, in main. create an executable command (add_executable () in the cpp file. Its name is the same as the project name ($ {PROJECT_NAME, compile the source code to the executable file named CMakeTest, that is, set the executable file name to be the same as the project name. The $ {} expression allows access to any variables defined in the environment. Therefore, you can use the $ {PROJECT_NAME} variable as the output name of an executable program. Assume that the main. cpp file is a simple Hello World Program. The Code is as follows:

#include <iostream>using namespace std;int main(int argc, char *argv[]){    cout << "Hello World! " << argc << argv[0] << endl;    return 0;}

Place the two files in the same directory and execute the following commands in sequence:

cmake .make

In this way, we can see an executable file named CMakeTest under the current directory.

2. Create a library)

CMake allows you to create library files that can be used by the OpenCV compilation system. Code sharing among multiple applications is a common and practical practice during software development. This method is useful in large applications, or when the same code is shared among multiple applications.

In this case, we do not need to create a binary executable file. Instead, we create a compiled file that contains all functions and classes and use it for development. We can share this library file with other applications without sharing our source code. CMake contains an add_library () function to achieve this goal:

# Create our hello libraryadd_library(Hello hello.cpp hello.h)# Create our application that uses our new libraryadd_executable(executable main.cpp)# Link our executable with the new librarytarget_link_libraries(executable Hello)

Behavior comment lines starting with "#", which are ignored by CMake.

The command add_library (Hello hello. cpp hello. h) defines the name of the newly created database. The database name is Hello, and hello. cpp and hello. h are the source files of the database. We add header files to allow IDE to link to these header files. Execute this command to generate a shared file (which is generated under OS X or Unix *. so file, which is generated in Windows *. dll file), which depends on the operating system we use, whether to create a dynamic library or a static library.

Target_link_libraries (executable Hello) is used to link our executable File executable to the target library. Here, it will be linked to the Hello library.

Suppose we define a say () function in hello. cpp to output the string, and add the declaration of this function in the hello. h file:

1 # include <iostream>
2 # include "hello. h"
3
4 void say (std: string text)
5 {
6 std: cout <text <std: endl;
7} hello. cpp
#ifndef __HELLO_H__#define __HELLO_H__#include <string>void say(std::string text);#endif
Hello. h

Then, we add the hello. h header file declaration in main. cpp and reference the say () function defined in the Hello library. The Code is as follows:

1 # include "hello. h"
2
3 int main ()
4 {
5 say ("I can say Hello! ");
6 return 0;
7} main. cpp

Finally, run the "cmake." and "make" commands in sequence to view the final executable file. Click Run to view the result.

3. Manage dependencies)

CMake is capable of searching our dependent files and external libraries. It provides us with the infrastructure to compile complex projects,

4. Make the script more complex (Making the script more complex)

In this section

Use cmake to compile and install MySQL 5.5

CMake quick tutorial

Ubuntu 12.04 uses CMake-2.8.10.4 to compile OpenCV-2.4.4

CMake is not ideal

CMake sets the cross-compilation environment

CMake for MySQL compilation and Installation

Compile and install MySQL 5.5 Based on CMake

CMake details: click here
CMake: click here

This article permanently updates the link address:

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.