cmake使⽤-⽣成头⽂件⽬录结构如下:
lqd@ubuntu:~/lqd/cmake/test$ tree
.
├── build
├──
├── config
│  └── config.h.in
├── inc
├── lib
│  ├──
│  ├── sum.c
│  └── sum.h
└── src
├──
└── main.c
5 directories, 7 files
⽬标:
config.h.in⾥⾯定义了⼀些宏定义,我们将config.h.in配置⽂件⽣成头⽂件,供源码使⽤
~/lqd/cmake/
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT(demo1)
ADD_SUBDIRECTORY(./lib)
ADD_SUBDIRECTORY(./src)
#ADD_EXECUTABLE(main main.c)
~/lqd/cmake/test/config/config.h.in
#cmakedefine USE_MY_DEFINE
~/lqd/cmake/test/
#CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
#PROJECT_BINARY_DIR 永远执⾏cmake命令执⾏的⽬录
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/out/lib)
aux_source_directory(./ DIR_LIB_SRCS)
#add_library(sum SHARED ${DIR_LIB_SRCS})
add_library(sum STATIC ${DIR_LIB_SRCS})
~
/lqd/cmake/test/lib/sum.c
int sum(int a, int b)
{
int sum;
sum = a + b;
return sum;
}
~/lqd/cmake/test/lib/sum.h
#ifndef _SUM_H_
#define _SUM_H_
int sum(int a, int b);
#endif
~/lqd/cmake/test/
#PROJECT_SOURCE_DIR 永远指向最顶层的⽬录INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/out/bin)
#将 config.h.in 配置⽂件⽣成 test.h 头⽂件
CONFIGURE_FILE(
"${PROJECT_SOURCE_DIR}/config/config.h.in"
"${PROJECT_SOURCE_DIR}/config/test.h"
)
#添加 USE_MY_DEFINE 配置属性
OPTION(USE_MY_DEFINE
ON
)
#宏控判断
IF(USE_MY_DEFINE)
#这个判断⾥⾯什么都没有做
ENDIF(USE_MY_DEFINE)
#包含config.h 头⽂件路径
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/config)
aux_source_directory(./ DIR_LIB_SRCS)
ADD_EXECUTABLE(main ${DIR_LIB_SRCS})
TARGET_LINK_LIBRARIES(main sum)为什么现在都用cmake
~
/lqd/cmake/test/src/main.c
#include "sum.h"
#include "test.h"
int main(int argc, char *argv[])
{
int result = 0;
printf("Hello worlod.\n");
result = sum(1, 2);
printf("result=%d \n", result);
#ifdef USE_MY_DEFINE
printf("Define USE_MY_DEFINE.\n");
#else
printf("Don't define USE_MY_DEFINE.\n");
#endif
return 0;
}
使⽤⽅法;
进⼊build⽬录,执⾏命令:ccmake ..
进⼊到选择终端⾥⾯后,按c开始导⼊配置
导⼊配置完成后,再按c,进⼊配置配置。
通过上下切换⾏,通过enter切换状态
配置完成后,再按c完成配置,然后再按g,退出并⽣成相应的配置⽂件然后执⾏命令:make
这个时候,所有的库和程序就都⽣成好了。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。