当前位置: 首页 > Linux

cmake相关

时间:2023-04-06 22:00:51 Linux

你好世界代码main.c#includeintmain(void){printf("helloworld\n");return0;}CMakeLists.txt#指定最低版本cmake_minimum_required(VERSION3.3)#指定项目名称和语言类型project(helloC)#debugset(CMAKE_BUILD_TYPE"Debug")#c99set(CMAKE_C_FLAGS"${CMAKE_C_FLAGS}-std=c99-g")#指定源代码列表aux_source_directory(.SRCS)#指定可执行程序add_executable(hello${SRCS})run[root@cstudyhello]#mkdirbuild[root@cstudyhello]#cdbuild/[root@cstudybuild]#cmake..--C编译器标识是GNU4.8.5--检查工作C编译器:/usr/bin/cc--检查工作C编译器:/usr/bin/cc--工作--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo--done--DetectingCcompilefeatures--检测Ccompilefeatures-done--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/root/code/cmake/hello/build[root@cstudybuild]#makeScanningdependenciesoftargethello[50%]构建C对象CMakeFiles/hello.dir/main.c.o[100%]LinkingCexecutablehello[100%]Builttargethello[root@cstudybuild]#./hellohelloworld[root@cstudybuild]#添加子目录目录结构├──CMakeLists.txt├──include│└──util.h└──src├──CMakeLists.txt├──main.c└──util.ctoplevelCMakeLists.txt#指定最低版本cmake_minimum_required(VERSION3.3)#指定项目名称和语言类型project(helloC)#debugset(CMAKE_BUILD_TYPE"Debug")#c99set(CMAKE_C_FLAGS"${CMAKE_C_FLAGS}-std=c99-g")#添加子目录add_subdirectory(src)子目录CMakeLists.txt#头文件目录include_directories(${PROJECT_SOURCE_DIR}/include)#sourcefileListaux_source_directory(.SRCS)#可执行文件目录set(EXECUTABLE_OUTPUT_PATH${PROJECT_BINARY_DIR}/bin)#可执行文件add_executable(hello${SRCS})code[root@cstudyhello]##main.c[root@cstudyhello]#catsrc/main.c#include#include"../include/util.h"intmain(){inta=1;整数b=2;printf("%d+%d=%d\n",a,b,sum(a,b));printf("你好,世界!\n");return0;}[root@cstudyhello]##util.h[root@cstudyhello]#catinclude/util.h#ifndefHELLO_UTIL_H#defineHELLO_UTIL_Hintsum(inta,intb);#endif//HELLO_UTIL_H[root@cstudyhello]##util.c[root@cstudyhello]#catsrc/util.c#include"../include/util.h"intsum(inta,intb){returna+b;}[root@cstudyhello]#运行[root@cstudyhello]#mkdirbuild[root@cstudyhello]#cdbuild[root@cstudybuild]#cmake..--C编译器标识是GNU4.8.5--检查工作C编译器:/usr/bin/cc--检查工作C编译器:/usr/bin/cc--工作--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo-done--DetectingCcompilefeatures--DetectingCcompilefeatures-done--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/root/code/目录cmake/hello/build[root@cstudybuild]#makeScanning目标hello的依赖关系[33%]BuildingCobjectsrc/CMakeFiles/hello.dir/util.c.o[66%]BuildingCobjectsrc/CMakeFiles/hello.dir/main.c.o[100%]LinkingCexecutable../bin/hello[100%]Builttargethello[root@cstudybuild]#./bin/hello1+2=3你好,世界![root@cstudybuild]#