CmakeLists Tpl

本文介绍了一个使用CMake的项目模板,包括如何设置编译器、定义宏、链接库以及安装步骤。同时提供了生成静态库和动态库的具体配置。

深入研究了下Cmake的文档,自己写了个Project模板:

###Cmake tpl by Crazybaby SET(projectName ok) SET(include_dir ${${projectName}_SOURCE_DIR}/D-include /usr/include/ /usr/local/include/) SET(lib_dir ${${projectName}_SOURCE_DIR}/D-lib /usr/lib/ /usr/local/lib/) SET(links_name A) PROJECT(${projectName}) #Cmake Version cmake_minimum_required(VERSION 2.6.4) #2.8 not support relative path #Rm Cmake cache EXEC_PROGRAM(rm ARGS -rf CMakeCache.txt) AUX_SOURCE_DIRECTORY(./src SRC_LIST) #set SET(CMAKE_C_COMPILER "g++") #Function must be declared #Add define ADD_DEFINITIONS(-g -O0 -Wall -DAC_HAS_INFO -DAC_HAS_DEBUG -DAC_HAS_WARNING -DAC_HAS_ERROR) #detail compile progress SET(CMAKE_VERBOSE_MAKEFILE ON) INCLUDE_DIRECTORIES(${include_dir}) LINK_DIRECTORIES(${lib_dir}) ADD_EXECUTABLE(${projectName} ${SRC_LIST}) #link_bin TARGET_LINK_LIBRARIES(${projectName} ${links_name}) ##Message MESSAGE(STATUS "#---------------------------------#") MESSAGE(STATUS "#->Bin dir " ${${projectName}_BINARY_DIR}) MESSAGE(STATUS "#->Source dir " ${${projectName}_SOURCE_DIR}) MESSAGE(STATUS "#---------------------------------#") #Install INSTALL(FILES ${${projectName}_BINARY_DIR}/${projectName} DESTINATION /usr/local/bin/${projectName})

这个是生成bin用的,需要自己建立 src ,D-include , D-lib, build目录 ,并把所有源文件放入src中,进入build中cmake .. && make && make install

如图:

. |-- build |-- CMakeLists.txt |-- D-include | `-- libA.h |-- D-lib | `-- libA.a `-- src |-- libA.c `-- main.c

静态库和动态库生成:

###Cmake tpl by Crazybaby SET(projectName ok) SET(include_dir ${${projectName}_SOURCE_DIR}/D-include /usr/include/ /usr/local/include/) SET(lib_dir ${${projectName}_SOURCE_DIR}/D-lib /usr/lib/ /usr/local/lib/) SET(links_name A) PROJECT(${projectName}) #Cmake Version cmake_minimum_required(VERSION 2.6.4) #2.8 not support relative path #Rm Cmake cache EXEC_PROGRAM(rm ARGS -rf CMakeCache.txt) AUX_SOURCE_DIRECTORY(./src SRC_LIST) #set SET(CMAKE_C_COMPILER "g++") #Function must be declared #Add define ADD_DEFINITIONS(-g -O0 -Wall -DAC_HAS_INFO -DAC_HAS_DEBUG -DAC_HAS_WARNING -DAC_HAS_ERROR) #detail compile progress SET(CMAKE_VERBOSE_MAKEFILE ON) INCLUDE_DIRECTORIES(${include_dir}) LINK_DIRECTORIES(${lib_dir}) ADD_LIBRARY(${projectName} STATIC ${SRC_LIST}) ADD_LIBRARY(${projectName}_o SHARED ${SRC_LIST}) #link_bin SET_TARGET_PROPERTIES(${projectName}_o PROPERTIES OUTPUT_NAME "${projectName}") ##Message MESSAGE(STATUS "#---------------------------------#") MESSAGE(STATUS "#->Bin dir " ${${projectName}_BINARY_DIR}) MESSAGE(STATUS "#->Source dir " ${${projectName}_SOURCE_DIR}) MESSAGE(STATUS "#---------------------------------#") #Install INSTALL(FILES ${${projectName}_BINARY_DIR}/${projectName} DESTINATION /usr/local/bin/${projectName}) 把源文件放入src中,再建立个build , cmake .. && make

大致情况如下:

. |-- build |-- CMakeLists.txt `-- src |-- hello.c `-- hello.h

ERROR: Failed :entry:default@BuildNativeWithCmake... > hvigor ERROR: Exceptions happened while executing: Not searching for unused variables given on the command line. CMake Warning (dev) at CMakeLists.txt:1 (project): cmake_minimum_required() should be called prior to this top-level project() call. Please see the cmake-commands(7) manual for usage documentation of both commands. This warning is for project developers. Use -Wno-dev to suppress it. CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. CMake Deprecation Warning at E:/MyApplicationRn/entry/oh_modules/@rnoh/react-native-openharmony/src/main/cpp/CMakeLists.txt:6 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. CMake Deprecation Warning at E:/MyApplicationRn/entry/oh_modules/@rnoh/react-native-openharmony/src/main/cpp/third-party/boost/libs/predef/CMakeLists.txt:23 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- Boost.Context: architecture arm64, binary format elf, ABI aapcs, assembler gas, suffix .S, implementation fcontext Experimental C-API architecture enabled CMake Error at CMakeLists.txt:23 (add_subdirectory): The binary directory E:/MyApplicationRn/entry/.cxx/default/default/debug/arm64-v8a/permissions is already used to build a source directory. It cannot be used to build source directory E:/MyApplicationRn/entry/oh_modules/@react-native-oh-tpl/react-native-permissions/src/main/cpp Specify a unique binary directory name. -- Configuring incomplete, errors occurred!鸿蒙项目运行报错
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值