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命令)
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 -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-OptiPlex-5050:~/桌面/cmake_test/test2/build$ ./hello Hello World.
相关推荐
wonner 2020-05-07
adsadadaddadasda 2020-04-15
小惠 2020-03-04
hazing 2020-02-22
oZaoHua 2020-02-16
wbczyh 2020-01-19
82467413 2019-12-31
jackadmi 2019-12-28
Andrea0 2019-12-27
wenjs00 2019-12-14
pointfish 2019-12-14
greent00 2019-11-13
kevinzhangwen 2019-03-10
yhljxy 2014-10-19
cyhgogogo 2012-08-20