CMakeLists编写和测试实例

视觉十四讲高博代码github
视觉十四讲代码

  • 自动分锅
cmake_minimum_required(VERSION 3.5)
project(auto_sub_module)

find_package(jsoncpp CONFIG REQUIRED)

set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include ${JSON_INC_PATH})

aux_source_directory(src SRC_LIST)

#add_executable(${PROJECT_NAME} ${SRC_LIST} src/Traffic_light_data.cpp)
#add_executable(${PROJECT_NAME} ${SRC_LIST} include/Operator_json.h)
add_executable(${PROJECT_NAME} ${SRC_LIST})

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)
  • tinyxml中的cmakelist
cmake_minimum_required(VERSION 3.17)
project(Leetcode)
find_package(tinyxml2 REQUIRED)
MESSAGE("find tinyxml2")

set(CMAKE_CXX_STANDARD 11)
add_executable(Leetcode main.cpp)
target_link_libraries(${PROJECT_NAME} tinyxml2)
  • json文件的cmakelist
cmake_minimum_required(VERSION 3.5)

project(BA)
find_package(jsoncpp CONFIG REQUIRED)

#一定要加上11的标准,不然ifstream无法读取string格式的文件名
set(CMAKE_CXX_STANDARD 11)

#需要用到标准目录的时候,include需要将文件夹下目录全加进来,src需要取到别名
#include_directories(
#        ${CMAKE_CURRENT_SOURCE_DIR}/include ${JSON_INC_PATH}
#)
#aux_source_directory(src SRC_LIST)
#add_executable(${PROJECT_NAME} ${SRC_LIST})

#只有main文件的场景
include_directories(${JSON_INC_PATH})
add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)
  • json的测试main文件
#include 
#include 
#include 
#include 

using namespace std;

int main() {
    string file_name = "../config.json";
    Json::Value root;
    Json::Reader reader;
    std::ifstream ifs;
    ifs.open(file_name, std::ios::in);
    if (!reader.parse(ifs, root, false)) {
        cerr << "read file false " << file_name << endl;
        return 0;
    } else {
        cout << root.toStyledString() << endl;
    }
    ifs.close();
    return 0;
}
  • OpenCV中的cmakelist
cmake_minimum_required(VERSION 3.5)
project(opencv_test)
find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(demo main.cpp)
target_link_libraries(demo ${OpenCV_LIBS})
  • OpenCV测试程序
#include 
#include 
#include 

using namespace std;

int main() {
    string png = "../1.png";
    cv::Mat image1;
    image1 = cv::imread(png);
    cout << image1.cols << endl;
    return 0;
}

Eigen安装教程
Sophus是依赖Eigen进行安装的,所以在安装之前必须先安装上Eigen,并且版本是需要在3以上的
Sophus安装教程
Sophus包括了模板类和非模板类两种类型,一般使用的是非模板类直接使用

git checkout a621ff

Eigen+Sophus一般都是搭起来用,cmakelist的写法

cmake_minimum_required(VERSION 3.5)
project(testgroup)

find_package(Eigen3 REQUIRED)
find_package(Sophus REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR} ${Sophus_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} main.cpp)
#连target都是不需要的
#target_link_libraries(${PROJECT_NAME} ${Sophus_LIBRARISE})
  • Eigen+Sophus测试代码
#include 
#include 
#include 
#include 
#include "sophus/so3.h"
#include "sophus/se3.h"

using namespace std;
int main() {
    Eigen::Matrix3d R = Eigen::AngleAxisd(M_PI / 2, Eigen::Vector3d(0, 0, 1)).toRotationMatrix();
    cout << R << endl;
    return 0;
}

ceres会比较麻烦,需要手动的进行一些设置
ceres安装和CMakelists教程

在视觉slam十四讲中使用ceres库的办法是,尝试过,可行.

cmake_minimum_required( VERSION 2.8 )
project( ceres_curve_fitting )

set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )

# 添加cmake模块以使用ceres库
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )

# 寻找Ceres库并添加它的头文件
find_package( Ceres REQUIRED )
include_directories( ${CERES_INCLUDE_DIRS} )

# OpenCV
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_DIRS} )

add_executable( curve_fitting main.cpp )
# 与Ceres和OpenCV链接
target_link_libraries( curve_fitting ${CERES_LIBRARIES} ${OpenCV_LIBS} )

sophus需要链接到动态库,不然无法使用

cmake_minimum_required(VERSION 3.5)
project(slam)
find_package(Eigen3 REQUIRED)
find_package(Sophus REQUIRED)
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED COMPONENT common io)
find_package(Boost REQUIRED COMPONENTS filesystem )

set(CMAKE_CXX_STANDARD 11)
set(Sophus_LIBRARIES ${PROJECT_SOURCE_DIR}/test/libSophus.so)
#set(LIBS ${PROJECT_SOURCE_DIR}/test/libboost_system.so ${PROJECT_SOURCE_DIR}/test/libboost_filesystem.so)


include_directories(${EIGEN3_INCLUDE_DIR} ${Sophus_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS}
        ${PCL_INCLUDE_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${Sophus_LIBRARIES} ${OpenCV_LIBS}
        ${PCL_LIBRARIES} )

pcl直接安装的话会有问题,导致Clion智能提示变慢,首先需要卸载掉原先的pcl

sudo apt-get purge pcl*

安装的时候参考网址
安装pcl
pcl库的测试

https://www.cnblogs.com/shuimuqingyang/p/10246648.html

你可能感兴趣的:(c++,开发语言,后端)