简单CMakeLists的编写

我建了一个简单的工程来学习一下CMakeLists.txt 是如何编写的
目录结构如下
CMakeLists.txt
src/CMakeLists.txt helloworld.c test1.c test.c test.h TutorialConfig.h.in
include

其中CMakeLists.txt中为

  1 cmake_minimum_required (VERSION 2.6)
  2 project(HELLOWORLD)
  3
  4 add_subdirectory (src)

src中CMakeLists.txt中为

1 set (Tutorial_VERSION_MAJOR 1)
  2 set (Tutorial_VERSION_MINOR 0)
  3
  4 option (USE_MYTEST "Use tutorial provided math implementation" ON)
  5
  6 if (USE_MYTEST)
  7     add_library(libtest SHARED test.c)
  8     set(EXTRA_LIBS ${EXTRA_LIBS} libtest)
  9 endif (USE_MYTEST)
 10
 11 configure_file (
 12           "${PROJECT_SOURCE_DIR}/src/TutorialConfig.h.in"
 13           "${PROJECT_BINARY_DIR}/src/TutorialConfig.h"
 14         )
 15
 16 include_directories("${PROJECT_BINARY_DIR}/src")
 17
 18 include_directories ("${PROJECT_SOURCE_DIR}/src")
 19
 20 add_executable(helloworld helloworld.c)
 21 target_link_libraries(helloworld ${EXTRA_LIBS})
 22
 23
 24 # first we add the executable that generates the table
 25 add_executable(test1 test1.c)
 26
 27
 28 add_custom_target(run ALL
 29         COMMAND test1 ${CMAKE_CURRENT_BINARY_DIR}/test1.h
 30             DEPENDS test1)
 31
 32 # add the binary tree directory to the search path for
 33 # include files
 34 include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
 35
 36
 37 install (TARGETS helloworld DESTINATION bin)
 38 install (TARGETS ${EXTRA_LIBS} DESTINATION lib)

CMakeLists中cmake_minimum_required是要求cmake的最低版本,如果
低于这个版本,则不会编译
project指的是工程的名字
add_subdirectory (src)则是说明要编译的子目录位置,然后就会到指定目录编译

src CMakeLists 中对set (Tutorial_VERSION_MAJOR 1)的设置会在TutorialConfig.h中生成相应的宏
同时option的作用是如果定义了USE_MYTEST的宏,则它的选项是ON,则会通过
add_library来生成共享库,如果只是需要生成静态库,则需要将shared去掉。

include_directories则是指定了头文件的寻找地址

add_executable为生成可执行文件
target_link_libraries是所依赖的库文件

最后,add_custom_target 为自己想要在build使其所执行的命令,还有一个
add_custom_command ,但是实际运行时发现并无反应。可能用法还不是很了解

最后install 是自己要将文件安装的位置,默认为/usr/local/bin ,这里指定为/usr/bin

TutorialConfig.h.in

  1 #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
  2 #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
  3 #cmakedefine USE_MYTEST

```///
分隔线
CMakeLists如何添加依赖
以下是CMake依赖glib的方式
在pkg_check_modules中g开头的都是glib所引用的库,
首先检测glib的 modules,然后设置CFLAGS编译依赖及
LDFLAGS编译依赖,直接引用<glib.h>就可以调用了,同时, 

set( TEST_MANAGER device-manager )
pkg_check_modules( ${TEST_MANAGER} REQUIRED
glib-2.0
gio-2.0
gio-unix-2.0
gmodule-2.0
librpc-ipc
)

set( EXTRA_CFLAGS “” )
foreach( cflag KaTeX parse error: Expected '}', got 'EOF' at end of input: {{TEST_MANAGER}_CFLAGS} )
set( EXTRA_CFLAGS “${EXTRA_CFLAGS} ${cflag}” )
endforeach( cflag )

set( EXTRA_LDFLAGS “” )
foreach( ldflag KaTeX parse error: Expected '}', got 'EOF' at end of input: {{TEST_MANAGER}_LDFLAGS} )
set( EXTRA_LDFLAGS “${EXTRA_LDFLAGS} ${ldflag}” )
endforeach( ldflag )

set( CMAKE_C_FLAGS “${CMAKE_C_FLAGS} ${SPEC_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -g3 -O0”)

IF(GCOV)
set( CMAKE_C_FLAGS “${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage”)
ENDIF()

#set target link library
target_link_libraries (
${TEST_MANAGER}
${EXTRA_LDFLAGS}
)
###########################################
补充,link_directories是指向链接库位置的意思

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值