Linux下使用cmake的簡單示例


一、單文件目錄

1. 編輯C程序文件,命名為main.c

#include <stdio.h>

int main(void) {
    printf("Hello World.\n");
    return 0;
}

 

2. 編寫CMakeLists.txt文件,保存在main.c同路徑下

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)

#把當前目錄(.)下所有源代碼文件和頭文件加入變量SRC_LIST AUX_SOURCE_DIRECTORY(. SRC_LIST)
#生成應用程序hello(在windows下生成hello.exe) ADD_EXECUTABLE(hello ${SRC_LIST})

 

3. 運行cmake命令生成MakeFile,再運行make命令生成hello可執行程序(為防止文件混亂,可建立build目錄,在此目錄下運行cmake命令)

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ cmake .. -- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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: /home/mgh/桌面/cmake_test/test2/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/test1.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$ ./hello 
Hello World.

 

二、多文件目錄

1. 目錄結構

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test$ tree test
test
├── CMakeLists.txt
├── print
│   ├── CMakeLists.txt
│   ├── print.c
│   └── print.h
└── test.c

 

2. 編輯C程序文件

test.c

#include <stdio.h>
#include "print/print.h"

int main(void) {
    print();
    return 0;
}

print.h

#ifndef PRINT_H
#define PRINT_H

extern void print();

#endif

print.c

#include <stdio.h>

extern void print(){
    printf("Hello World.\n");
}

 

3. 編輯CMakeLists.txt文件

這種多目錄的情況,需要在每個源文件路徑中分別編寫CMakeLists.txt文件,對應這個例子,需要在test根目錄和print目錄下編寫CMakeLists.txt文件。

為了方便,我們可以先將print目錄里的文件編譯成靜態庫再由main函數調用。

test目錄下的CMakeLists.txt文件:

#Minimum required CMake Version
cmake_minimum_required(VERSION 3.6.1)

#Project Name
project(hello)

#當前目錄下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIST)

#添加print子目錄
add_subdirectory(print)

#指定生成目標
ADD_EXECUTABLE(hello ${SRC_LIST})

#添加鏈接庫
target_link_libraries(hello printFunc)

print目錄下的CMakeLists.txt文件:

#當前目錄下所有源文件保存到SRC_LIST中
AUX_SOURCE_DIRECTORY(. SRC_LIB)

#生成鏈接庫
ADD_LIBRARY(printFunc ${SRC_LIB})

 

4. 編譯運行

mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ cmake .. -- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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: /home/mgh/桌面/cmake_test/test/build
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ make
Scanning dependencies of target printFunc
[ 25%] Building C object print/CMakeFiles/printFunc.dir/print.c.o
[ 50%] Linking C static library libprintFunc.a
[ 50%] Built target printFunc
Scanning dependencies of target hello
[ 75%] Building C object CMakeFiles/hello.dir/test.c.o
[100%] Linking C executable hello
[100%] Built target hello
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$ ./hello
Hello World.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM