文章目录
参考链接:
https://www.cnblogs.com/ningskyer/articles/7158948.html
https://cmake.org/cmake-tutorial/
CMakeLists.txt
库的制作
# 查找当前目录下的所有源文件,保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (mylib STATIC ${DIR_LIB_SRCS}) #静态库 libmylib.a:STATIC 共享库libmylib.so:SHARED ,默认静态库
基本的CMakeLists.txt
说明:
- 目录结构:src(源文件)、include(头文件)、lib(库文件)、CMakeLists.txt
- 使用的时候将下面CMakeLists.txt中库名替换
- mkdir build-> cd build-> cmake … -> make
- bulid目录下会生成MakeFile 和 目标程序
#申明要求的cmake的最低版本
cmake_minimum_required (VERSION 2.6)
#申明一个cmake工程
project (Tutorial)
include_directories(include) #包含头文件
add_subdirectory(lib) #添加一个子目录, 指明本cmake项目包含一个子目录
#将src目录下的所有源文件名保存到DIR_SRCS变量中
aux_source_directory(src DIR_SRCS)
#生成gdb调试程序
#set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
#set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
add_executable(Tutorial ${DIR_SRCS}) #添加一个可执行文件 语法:add_executable(程序名 原码文件)
target_link_libraries(Tutorial 库名)
find_package
find_package命令最终通过 pkg-config 命令来查找是否有对应的package 模块,那么pkg-config 又是如何来知道我们的系统是否安装了某个模块呢? pkg-config是通过 *.pc 的文件来判断。 例如 如果pkg-config找到了 libswscale2.11.pc 则就说明我们有 libswscale 2.11 库,
而 libswscale2.11.pc 里则定义了 header和lib的目录位置,通常在以下目录能找到pkgconfig目录:/usr/local/lib /usr/local/share/ /usr/lib
。如果想加入其它目录:export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/Cellar/opencv/3.4.0/lib/pkgconfig
find_package(OpenCV REQUIRED)会在Ubuntu系统中找到OpenCVConfig.cmake,该文件定义了OpenCV_INCLUDE_DIRS和OpenCV_LIBS 等变量,因而可以使用 include_directories和target_link_libraries来访问这两个变量。
可以通过打开terminal输入locate OpenCVConfig.cmake来找到这个.cmake文件,用以确认系统确实安装了OpenCV.
可以指定库的查找路径,相当于指定特定的库版本
set(OpenCV_DIR /opt/share/OpenCV)
多目录CMakeLists.txt的编写
方法一:每个类的头文件和源文件在同一个文件夹
文件结构如下:而且Student继承Person
- Person
Person.h
#ifndef __PERSON_H
#define __PERSON_H
#include <iostream>
using namespace std;
class Person
{
public:
Person(string name, int id);
~Person();
void Print();
protected:
string m_name;
int m_ID;
};
#endif
Person.cpp
#include "Person.h"
Person::Person(string name, int id)
{
m_name = name;
m_ID = id;
}
Person::~Person()
{
}
void Person::Print()
{
cout << m_name << " : " << m_ID << endl;
}
CMakeLists.txt
aux_source_directory(. DIR_PERSON_SRC)
add_library(Person ${DIR_PERSON_SRC})
- Student
Student.h
#ifndef __STUDENT_H
#define __STUDENT_H
#include "Person.h"
class Student : public Person
{
public:
Student( string name, int id, int scores);
~Student();
void Print();
private:
int m_scores;
};
#endif
Student.cpp
#include "Student.h"
Student::Student(string name, int id, int scores):Person(name, id),m_scores(scores)
{
}
Student::~Student()
{
}
void Student::Print()
{
cout << m_name << " : " << m_ID << endl;
cout << m_scores << endl;
}
CMakeLists.txt
include_directories(../Person)
aux_source_directory(. DIR_STUDENT_SRC)
add_library(Student ${DIR_STUDENT_SRC})
target_link_libraries(Student Person)
- 顶层
main.cpp
#include "Student.h"
#include "Student.h"
int main(void)
{
Student s1( "juliana" , 1, 90);
s1.Print();
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(my_test)
aux_source_directory(. DIRSRC)
include_directories(Person Student)
add_subdirectory(Person)
add_subdirectory(Student)
add_executable(my_test ${DIRSRC})
target_link_libraries(my_test Person Student)
方法二:所有类的头文件在一起(include),源文件在一起(src)
文件结构如下:
cmake_minimum_required(VERSION 3.5)
project(my_test)
set(CMAKE_CXX_FLAGS "-std=c++11")
#OpenCV
find_package(OpenCV REQUIRED)
#pcl
find_package(PCL REQUIRED COMPONENT common io)
add_definitions(${PCL_DEFINITIONS})
#头文件目录
include_directories("/usr/include/eigen3/")
include_directories(include)
#自己编写的源文件和头文件
FILE(GLOB_RECURSE CURRENT_INCLUDE include/*.hpp)
FILE(GLOB_RECURSE CURRENT_SOURCES src/*.cpp)
#Debug
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
add_executable(my_test ${CURRENT_SOURCES} ${CURRENT_INCLUDE})
target_link_libraries(my_test ${OpenCV_LIBS} ${PCL_LIBRARIES})
常用
cmake_minimum_required(VERSION 2.8)
project(VIO_Data_Simulation)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_BUILD_TYPE Debug) # Release or Debug
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
FILE(GLOB_RECURSE CURRENT_INCLUDE include/*.hpp)
FILE(GLOB_RECURSE CURRENT_SOURCES src/*.cpp)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Sophus REQUIRED)
include_directories(
${EIGEN3_INCLUDE_DIR}
${SOPHUS_INCLUDE_DIR}
${OPENCV_INCLUDE_DIR}
"include"
)
LIST(APPEND LINK_LIBS
${OpenCV_LIBS}
${Sophus_LIBRARIES}
)
add_executable(data_generation main/main.cpp ${CURRENT_SOURCES} ${CURRENT_INCLUDE})
target_link_libraries(data_generation ${LINK_LIBS})
cmake-gui
- 安装:apt-get install cmake-qt-gui
- 打开:cmake-gui
使用:见opencv_contrib博客