CMake Beginner's Introductory tutorial

Source: Internet
Author: User
Tags network function

Linux environments use CMake to compile large projects 1. A simple example

Compiling large projects with CMake is convenient and makes a simple record.

Create a directory win, which is the project file. The directory of WIN is as follows:

    win    \-- CMakeLists.txt    |-- build.sh    |-- libs               外部库    |-- src        |-- CMakeLists.txt        |-- common         公用头文件        |-- win98          win98工程        |   |-- CMakeLists.txt        |   |-- main.cc        |   |-- win98.h        |   \-- win98.cc        |        |-- win2000        win2000工程            |-- CMakeLists.txt            |-- main.cc            |-- win2000.h            \-- win2000.cc

Win, SRC, Win98, Win2000 four directories have a CMakeLists.txt file, the file is required, CMake based on this file to generate Makefile.

Win98 's CMakeLists.txt content is as follows

    add_executable(win98        main.cc        win98.cc      )

Assuming that Win2000 has added a new network function, which needs to be used to link the library file, you need to specify in the CMakeLists.txt

    add_executable(robot        main.cc        win2000.cc      )            target_link_libraries(win2000 network)

The CMakeLists.txt in the SRC directory will need to specify the directories that the subordinate needs to compile.

    add_subdirectory(win98)    add_subdirectory(win2000)

The content of the CMakeLists.txt file in the win directory is a bit more complicated, here is a simple example

    # 指定版本和项目名称    cmake_minimum_required(VERSION 2.6)    project(windows CXX)    # 指定源码目录    add_subdirectory(src)

Run under the win directory

    cmake .    后面有个点    make

The entire project is compiled, and execution files are available under Win98 and Win2000.

2. Combining Shell Scripts

The above method has been able to successfully compile the project, there is a problem is that the compilation produced by the intermediate files in the source code directory, let the directory confusion, if the intermediate files in other directories, you can ensure that the source code directory is neat.

Suppose the intermediate directory is wintemp and the win directory sibling. Add the build.sh file in the win directory with the content

    #!/bin/sh    set -x    SOURCE_DIR=`pwd`    BINARY_DIR=`pwd`    BUILD_DIR=${BUILD_DIR:-../wintemp}    mkdir -p $BUILD_DIR/$BUILD_TYPE       && cd $BUILD_DIR/$BUILD_TYPE       && cmake         -DBINARY_DIR=$BINARY_DIR         -DSOURCE_DIR=$SOURCE_DIR         $SOURCE_DIR       && make $*

Execute this script and you can see that the intermediate files are all in the Wintemp directory. You can also specify the path to the executable file

    # 指定版本和项目名称    cmake_minimum_required(VERSION 2.6)    project(windows CXX)    # 指定生成的执行文件路径    set(EXECUTABLE_OUTPUT_PATH ${SOURCE_DIR}/bin)    # 指定源码目录    add_subdirectory(src)

As a result, the resulting execution file is in the source directory.

3. Macro Parameters

Multiple projects exist common code situation, for the simplest example, Win98 and Win2000 shared common/win.h files, this file defines the Microsoft operating system code, such as "Win98", "Win2000", it is natural to think of the macro processing this part.

    #ifdef __WIN98__    #define SYSNAME "win98"    #endif    #ifdef __WIN2000__    #define SYSNAME "win2000"    #endif

So how do you define your own compilation parameters in your project? This method can be used set_target_properties.
In the CMakeLists.txt in the Win98 directory, add

    set_target_properties(win98 PROPERTIES COMPILE_DEFINITIONS "__WIN98__")

In the CMakeLists.txt in the Win2000 directory, add

    set_target_properties(win2000 PROPERTIES COMPILE_DEFINITIONS "__WIN2000__")

With this configuration, the Win98 and Win2000 two items contain common/win.h, which can result in different definitions.

4. Compiling short Programs

Programmers will have some directories, which are written by their own short code, to test the syntax, function usage and so on. These small files can be easily compiled with CMake.

Assume that in the SRC directory exists a.cc, b.cc, c.cc and many other files, each file is a separate applet, under the SRC created under the CMakeLists.txt can write a script, they are programmed into the corresponding execution file.

    aux_source_directory (. filelist)    foreach(filename ${filelist})      string( REGEX MATCH "([a-z_]+)" binname ${filename} )      message( ${binname} )      add_executable(${binname} ${filename})    endforeach()

I finally attached a CMakeLists.txt example, and I benefited a lot from it.

CMake Beginners Tutorial

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.