[Using CMake to organize C projects] CMake Hello World

[Foreword]
This article introduces how to use CMake to build a Hello World C project.

[CMake usage routine process]

Friends who have used CMake to build projects know that the use of CMake has the following "conventions":

 1. 在项目根目录建立一个build目录:mkdir build && cd build.(该目录用于存储编译生成的相应文件)
 2. 进入build目录,编译源码生成Makefile
 3. 确定生成Makefile成功,执行make.(或者打开生成的工程文件,如Visual Studio项目文件或者是Xcode项目文件)

【Directory Structure】

Next, I want to build a Hello World and use CMake to build according to this process.

The entire project directory structure is as follows


.
└── cmaketest
    ├── build
    ├── CMakeLists.txt
    └── hello.c


main process

Step 1: Create a new hello.c

#include <stdio.h>  
int main()  
{  
  printf("hello,this is my first using cmake project/n");  
  return 0;
}  

Step 2: Create a new CMakeLists.txt

add_executable(hello hello.c)

Step 3: Use the "conventional" method to build

  1. Create a build directory under cmaketest

  2. Enter the build directory, compile the source code to generate the Makefile

    cmake [options] <source path>

    So execute under buildcmake ../

    write picture description here

    [phpuser@node1 build]$ cmake ../
    输出如下:
    -- The C compiler identification is GNU 4.8.5
    -- The CXX compiler identification is GNU 4.8.5
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /phpuser/rubbish/cmaketest/build

    At this point, view the contents of the build directory:

    [phpuser@node1 build]$ ll -a
    总用量 28
    drwxrwxr-x 3 phpuser phpuser    89 1月  30 15:10 .
    drwxrwxr-x 3 phpuser phpuser    56 1月  30 15:10 ..
    -rw-rw-r-- 1 phpuser phpuser 12617 1月  30 15:10 CMakeCache.txt
    drwxrwxr-x 5 phpuser phpuser   303 1月  30 15:10 CMakeFiles
    -rw-rw-r-- 1 phpuser phpuser  1504 1月  30 15:10 cmake_install.cmake
    -rw-rw-r-- 1 phpuser phpuser  4748 1月  30 15:10 Makefile

    You can see the generated Makefile

  3. implementmake

    write picture description here

    [phpuser@node1 build]$ make
    Scanning dependencies of target hello
    [ 50%] Building C object CMakeFiles/hello.dir/hello.c.o
    [100%] Linking C executable hello
    [100%] Built target hello

    Check the contents of the build directory again, you can see the generated hello file

    [phpuser@node1 build]$ ll -a
    总用量 40
    drwxrwxr-x 3 phpuser phpuser   102 1月  30 15:20 .
    drwxrwxr-x 3 phpuser phpuser    56 1月  30 15:10 ..
    -rw-rw-r-- 1 phpuser phpuser 12617 1月  30 15:10 CMakeCache.txt
    drwxrwxr-x 5 phpuser phpuser   303 1月  30 15:20 CMakeFiles
    -rw-rw-r-- 1 phpuser phpuser  1504 1月  30 15:10 cmake_install.cmake
    -rwxrwxr-x 1 phpuser phpuser  8520 1月  30 15:20 hello
    -rw-rw-r-- 1 phpuser phpuser  4748 1月  30 15:10 Makefile
  4. run./hello

    [phpuser@node1 build]$ ./hello 
    hello,this is my first using cmake project


Summarize

The above is the process of using CMake to build a Hello World C++ project. You can see that using CMake is much simpler than writing Makefiles yourself. You only need to add a sentence to CMakeLists.txt:

add_executable(hello, hello.c)

That's it.

Many parameters can be configured in CMakeLists.txt, please refer to

Detailed explanation of the writing and parameters of CmakeList

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325449008&siteId=291194637