Ubuntu下使⽤cmake结合⽣成makefile⽂件并
进⾏编译
转⾃1:wwwblogs/cv-pr/p/6206921.html
转⾃2:blog.csdn/qqwangfan/article/details/79093527
1.CMake编译原理
CMake是⼀种跨平台编译⼯具,⽐make更为⾼级,使⽤起来要⽅便得多。CMake主要是编写⽂件,然后⽤cmake命令将⽂件转化为make所需要的makefile⽂件,最后⽤make命令编译源码⽣成可执⾏程序或共享库(so(shared object))。因此CMake的编译基本就两个步骤:
1. cmake
2. make
cmake 指向所在的⽬录,例如cmake .. 表⽰在当前⽬录的上⼀级⽬录。cmake后会⽣成很多编译的中间⽂件以及makefile⽂件,所以⼀般建议新建⼀个新的⽬录,专门⽤来编译,例如
mkdir build
cd build
cmake ..
make
make根据⽣成makefile⽂件,编译程序。
2.使⽤Cmake编译程序
我们编写⼀个关于开平⽅的C/C++程序项⽬,即b= sqrt(a),以此理解整个CMake编译的过程。
a.准备程序⽂件
⽂件⽬录结构如下:
.
├── build
├──
├── include
│└── b.h
└── src
├── b.c
└── main.c
头⽂件b.h,如下所⽰:
#ifndef B_FILE_HEADER_INC
cmake如何使用#define B_FIEL_HEADER_INC
#include<math.h>
double cal_sqrt(double value);
#endif
头⽂件b.c,如下所⽰:
#include "../include/b.h"
double cal_sqrt(double value)
{
return sqrt(value);
}
main.c主函数,如下所⽰:
#include "../include/b.h"
#include <stdio.h>
int main(int argc, char** argv)
{
double a = 49.0;
double b = 0.0;
printf("input a:%f\n",a);
b = cal_sqrt(a);
printf("sqrt result:%f\n",b);
return 0;
}
b.编写
接下来编写⽂件,该⽂件放在和src,include的同级⽬录,实际⽅哪⾥都可以,只要⾥⾯编写的路径能够正确指向就好了。⽂件,如下所⽰:
1 #1.cmake verson,指定cmake版本
2 cmake_minimum_required(VERSION 3.2)
3
4 #2.project name,指定项⽬的名称,⼀般和项⽬的⽂件夹名称对应
5 PROJECT(test_sqrt)
6
7 #3.head file path,头⽂件⽬录
8 INCLUDE_DIRECTORIES(
9 include
10 )
11
12 #4.source directory,源⽂件⽬录
13 AUX_SOURCE_DIRECTORY(src DIR_SRCS)
14
15 #5.set environment variable,设置环境变量,编译⽤到的源⽂件全部都要放到这⾥,否则编译能够通过,但是执⾏的时候会出现各种问题,⽐如"symbol lookup error xxxxx , undefined symbol"
16 SET(TEST_MATH
17 ${DIR_SRCS}
18 )
19
20 #6.add executable file,添加要编译的可执⾏⽂件
21 ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_MATH})
22
23 #7.add link library,添加可执⾏⽂件所需要的库,⽐如我们⽤到了libm.so(命名规则:lib+name+.so),就添加该库的名称
24 TARGET_LINK_LIBRARIES(${PROJECT_NAME} m)
<主要包含以上的7个步骤,具体的意义,请阅读相应的注释。
c.编译和运⾏程序
准备好了以上的所有材料,接下来,就可以编译了,由于编译中出现许多中间的⽂件,因此最好新建⼀个独⽴的⽬录build,在该⽬录下进⾏编译,编译步骤如下所⽰:
mkdir build
cd build
cmake ..
make
操作后,在build下⽣成的⽬录结构如下:
├── build
│├──
│├── CMakeFiles
││├── 3.2.2
│││├── ake
│││├── ake
│││├── CMakeDetermineCompilerABI_C.bin
│││├── CMakeDetermineCompilerABI_CXX.bin
│││├── ake
│││├── CompilerIdC
││││├── a.out
││││└── CMakeCCompilerId.c
│││└── CompilerIdCXX
│││├── a.out
│││└── CMakeCXXCompilerId.cpp
││├── cmake.check_cache
││├── ake
││├── CMakeOutput.log
││├── CMakeTmp
││├── feature_tests.bin
││├── feature_tests.c
││├──
││├── Makefile2
││├── ake
││├── progress.marks
││├──
││└── test_sqrt.dir
││├── build.make
││├── C.includecache
││├── ake
││├── ake
││├── depend.internal
││├── depend.make
││├── flags.make
││├──
││├── progress.make
││└── src
││├── b.c.o
││└──
│├── ake
│├── Makefile
│└── test_sqrt
├──
├── include
│└── b.h
└── src
├── b.c
└── main.c
注意在build的⽬录下⽣成了⼀个可执⾏的⽂件test_sqrt,运⾏获取结果如下:
命令:
./test_sqrt
结果:
input a:49.000000
sqrt result:7.000000
d.源码
地址:
3.参考资料
[1].
// 我⾃⼰的⽂件内容
project(GateDevLinux)
cmake_minimum_required(VERSION 2.8)
set(OUTDIR ${CMAKE_CURRENT_SOURCE_DIR}/build)
ADD_DEFINITIONS(-DGLIBCXX_USE_CXX11_ABI)
find_package(OpenCV REQUIRED)
include_directories(
/usr/local/include
/
wf/linux/include
)
#把所需要的源⽂件加进来
set(SRC_LIST
Base64.cpp
CameraDetect.cpp
CaptureThread.cpp
Config.cpp
log.cpp
zini.cpp
)
LINK_DIRECTORIES(
/usr/local/lib
/usr/lib
/wf/linux/lib
)
SET(EXECUTABLE_OUTPUT_PATH ${OUTDIR})
add_executable(${PROJECT_NAME} ${SRC_LIST})
set(KLink_LIBRARIES pthread AmFaceDet AmFaceRec AmFaceCompare)
target_link_libraries(${PROJECT_NAME} -Wl,--start-group ${KLink_LIBRARIES} ${OpenCV_LIBS} -Wl,--end-group) SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
MESSAGE(STATUS "cxx flags is ${CMAKE_CXX_FLAGS}")
#在命令窗⼝输⼊以下命令,即可进⾏编译
#mkdir build
#cd build
#cmake ..
#make
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论