Linux下CMake怎么用
小编给大家分享一下Linux下CMake怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。
一、单文件目录
1. 编辑C程序文件,命名为main.c
#includeintmain(void){printf("HelloWorld.\n");return0;}
2. 编写CMakeLists.txt文件,保存在main.c同路径下
#MinimumrequiredCMakeVersioncmake_minimum_required(VERSION3.6.1)#ProjectNameproject(hello)#把当前目录(.)下所有源代码文件和头文件加入变量SRC_LISTAUX_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..--TheCcompileridentificationisGNU5.4.0--TheCXXcompileridentificationisGNU5.4.0--CheckforworkingCcompiler:/usr/bin/cc--CheckforworkingCcompiler:/usr/bin/cc--works--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo-done--DetectingCcompilefeatures--DetectingCcompilefeatures-done--CheckforworkingCXXcompiler:/usr/bin/c++--CheckforworkingCXXcompiler:/usr/bin/c++--works--DetectingCXXcompilerABIinfo--DetectingCXXcompilerABIinfo-done--DetectingCXXcompilefeatures--DetectingCXXcompilefeatures-done--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/home/mgh/桌面/cmake_test/test2/buildmgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$makeScanningdependenciesoftargethello[50%]BuildingCobjectCMakeFiles/hello.dir/test1.c.o[100%]LinkingCexecutablehello[100%]Builttargethellomgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test2/build$./helloHelloWorld.
二、多文件目录
1. 目录结构
mgh@mgh-OptiPlex-5050:~/桌面/cmake_test$treetesttest├──CMakeLists.txt├──print│├──CMakeLists.txt│├──print.c│└──print.h└──test.c
2. 编辑C程序文件
test.c
#include#include"print/print.h"intmain(void){print();return0;}
print.h
#ifndefPRINT_H#definePRINT_Hexternvoidprint();#endif
print.c
#includeexternvoidprint(){printf("HelloWorld.\n");}
3. 编辑CMakeLists.txt文件
这种多目录的情况,需要在每个源文件路径中分别编写CMakeLists.txt文件,对应这个例子,需要在test根目录和print目录下编写CMakeLists.txt文件。
为了方便,我们可以先将print目录里的文件编译成静态库再由main函数调用。
test目录下的CMakeLists.txt文件:
#MinimumrequiredCMakeVersioncmake_minimum_required(VERSION3.6.1)#ProjectNameproject(hello)#当前目录下所有源文件保存到SRC_LIST中AUX_SOURCE_DIRECTORY(.SRC_LIST)#添加print子目录add_subdirectory(print)#指定生成目标ADD_EXECUTABLE(hello${SRC_LIST})#添加链接库target_link_libraries(helloprintFunc)
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..--TheCcompileridentificationisGNU5.4.0--TheCXXcompileridentificationisGNU5.4.0--CheckforworkingCcompiler:/usr/bin/cc--CheckforworkingCcompiler:/usr/bin/cc--works--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo-done--DetectingCcompilefeatures--DetectingCcompilefeatures-done--CheckforworkingCXXcompiler:/usr/bin/c++--CheckforworkingCXXcompiler:/usr/bin/c++--works--DetectingCXXcompilerABIinfo--DetectingCXXcompilerABIinfo-done--DetectingCXXcompilefeatures--DetectingCXXcompilefeatures-done--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/home/mgh/桌面/cmake_test/test/buildmgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$makeScanningdependenciesoftargetprintFunc[25%]BuildingCobjectprint/CMakeFiles/printFunc.dir/print.c.o[50%]LinkingCstaticlibrarylibprintFunc.a[50%]BuilttargetprintFuncScanningdependenciesoftargethello[75%]BuildingCobjectCMakeFiles/hello.dir/test.c.o[100%]LinkingCexecutablehello[100%]Builttargethellomgh@mgh-OptiPlex-5050:~/桌面/cmake_test/test/build$./helloHelloWorld.
以上是“Linux下CMake怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。