c++编译链接模板

之前的一个博客(地址)总结了编译链接的思路,这次就总结各种使用的模板。
(有时间慢慢增加吧)

纯bash脚本

这个是最简单的模板,main.cpp里面要用三方库opencv的函数,然后直接编译生成可执行文件main。

fname=main.cpp
opencv_include=/....../install/opencv2/include
opencv_lib=/....../install/opencv2/lib
g++ -o ${fname%.*} $debug  $fname -I$opencv_include -L$opencv_lib -lopencv_core -lopencv_highgui -lopencv_objdetect -std=c++0x -lpthread

CMakefile.txt

简单的调用三方库然后编译

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall  -O3 -march=native --std=c++0x")
set(CUDA_USE_STATIC_CUDA_RUNTIME off)
include(/....../install/opencv2/share/OpenCV/OpenCVConfig.cmake)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/../include )
link_directories(${PROJECT_SOURCE_DIR}/../lib)
LINK_LIBRARIES(DBoW3 opencv_features2d)

# 要编译的文件
ADD_EXECUTABLE(main src/main.cpp)

目录结构
- lib 【三方库(DBoW3)】
- include 【三方库头文件】
- bin
- bin/src/main.cpp
- bin/CMakeLists.txt 【就是它】

记住铁三角INCLUDE_DIRECTORIES,link_directories,LINK_LIBRARIES基本就没啥问题了。

你可能感兴趣的:(c++学习笔记)