Linux下CMake简明教程(二)同一目录下多个源文件
接下来进入稍微复杂的例子:在同一个目录下有多个源文件。在之前的目录下添加2个文件,testFunc.c和testFunc.h。添加完后整体文件结构如下,
(base) ledi@ledi-MRC-WX0:~/slam_md40/learn_orbslam/makfile_learn/CMake简明教程/c2$ tree
.
├── build
├── CMakeLists.txt
├── main.c
├── testFunc.c
└── testFunc.h
1 directory, 4 files
testFunc.c内容如下,
/*
** testFunc.c
*/
#include <stdio.h>
#include "testFunc.h"
void func(int data)
{
printf("data is %d\n", data);
}
testFunc.h内容如下,
/*
** testFunc.h
*/
#ifndef _TEST_FUNC_H_
#define _TEST_FUNC_H_
void func(int data);
#endif
修改main.c,调用testFunc.h里声明的函数func(),
#include <stdio.h>
#include "testFunc.h"
int main(void)
{
func(100);
return 0;
}
修改CMakeLists.txt,在add_executable的参数里把testFunc.c加进来
cmake_minimum_required (VERSION 2.8)
project (demo)
add_executable(main main.c testFunc.c)
mkdir build
cd build
cmake ..
make
./main
data is 100

浙公网安备 33010602011771号