CMakeList.txt文件

1、最简单格式,即只有一个主cpp文件。
文件目录

demo
	|__build
	|__src
	| 	 |__main.cpp
	|__CMakeLists.txt

CMakeLists.txt文件

cmake_minimum_required(VERSION 2.8)	  		# CMake最低版本要求,低于2.6的构建过程会被终止
project( HelloWorld )					  	# 定义工程名称
add_executable( Hello src/main.cpp ) 		# 生成一个叫 Hello的可执行文件,其中可执行源文件路径为 src/main.cpp

main.cpp文件

#include <iostream>
using namespace std;
int main()
{
	cout << "hello world!" << endl;
   	return 0;
}

2、带头文件
文件目录

demo
	|__build
	|__include
	|		|__hello.h
	|		|__my_sum.h
	|_____src
	| 		|__main.cpp
	|__CMakeLists.txt

CMakeLists.txt文件

cmake_minimum_required(VERSION 2.8)			# CMake最低版本要求,低于2.6的构建过程会被终止
project( HelloWorld )						# 定义工程名称
include_directories( ${PROJECT_SOURCE_DIR}/include) 		# 头文件路径
add_executable( Hello src/main.cpp )		# 生成一个叫 Hello的可执行文件,其中可执行源文件路径为 src/main.cpp

注:如果有多个.cpp文件,可以add_executable( Hello src/hello_world src/main.cpp )

hello.h文件

#ifndef __HELLO__
#define __HELLO__

#include <iostream>
using namespace std;

void Hello_world()
{
	cout << "Hello world" << endl;
}

#endif /*__HELLO__*/

my_sum.h文件

#ifndef __MYSUM__
#define __MYSUM__
 
#include <iostream>
using namespace std;

int my_sum(int a, int b)
{
	return a+b;
}
 
#endif /*__MYSUM__*/

main.cpp文件

#include <iostream>
#include "hello.h"
#include "my_sum.h"

using namespace std;
int main()
{
	Hello_world();
   	cout << my_sum(1,2) << endl;
   	return 0;
}

3、添加opencv库
文件目录

demo
	|____data
	|       |__1.jpg
	|_____src
	| 		|__main.cpp
	|__CmakeLists.txt

CMakeLists.txt文件

cmake_minimum_required(VERSION 2.8)		# CMake最低版本要求,低于2.6的构建过程会被终止
# set(CMAKE_CXX_STANDARD 14)		#opencv4以上加此句
project(DisplayImage)					# 定义工程名称
find_package( OpenCV REQUIRED )			# opencv包 
add_executable( Display src/main.cpp )	# 生成一个叫 Hello的可执行文件,其中可执行源文件路径为 src/main.cpp
target_link_libraries( Display  ${OpenCV_LIBS}) # 链接库

这里需要注意opencv4需要C++11支持,因此需要添加set(CMAKE_CXX_STANDARD 14)

main.cpp文件

#include <iostream>  
#include <opencv2/opencv.hpp>

using namespace cv; 
using namespace std;

int main()
{
	Mat img = imread("../data/1.jpg", 1);
	imshow("img", img);
	waitKey(0);
	
	return 0;
}

4、添加pcl库
文件目录

demo
	|_____src
	| 		|__main.cpp
	|__CmakeLists.txt

CMakeLists.txt文件

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(MY_GRAND_PROJECT)
find_package(PCL 1.9 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(view_pcl src/main.cpp)
target_link_libraries(view_pcl ${PCL_LIBRARIES})

main.cpp文件

// main.cpp
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/io.h>


using namespace std;
using namespace pcl;
using namespace io;

int main()
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
    point_cloud_ptr->height = 1;
    for (float z(-1.0); z <= 1.0; z += 0.05)
    {
        for (float angle(0.0); angle <= 360.0; angle += 5.0)
        {
            pcl::PointXYZ point;
            point.x = z;
            point.y = z;
            point.z = z;
            point_cloud_ptr->points.push_back (point);
        }
    }


    boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));

    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(point_cloud_ptr, "z"); // 按照z字段进行渲染

    viewer->addPointCloud<pcl::PointXYZ>(point_cloud_ptr, fildColor, "sample cloud");
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud"); // 设置点云大小

    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
        boost::this_thread::sleep(boost::posix_time::microseconds(100000));
    }

    return 0;
}

cuda+tensorrt+opencv+pcl
CMakeLists.txt

cmake_minimum_required(VERSION 3.0)		# CMake最低版本要求,低于2.6的构建过程会被终止
set(CMAKE_CXX_STANDARD  14)		#opencv4以上加此句
project(cmake_test)				# 定义工程名称
find_package(OpenCV     REQUIRED)
find_package(PCL 	    REQUIRED)
find_package(CUDA 	    REQUIRED)
find_package(TensorRT 	REQUIRED)
include_directories(
                    ${PROJECT_SOURCE_DIR}/include
                    ${PCL_INCLUDE_DIRS} 
                    ${CUDA_INCLUDE_DIRS} 
                    ${TensorRT_INCLUDE_DIRS}
                    ${TensorRT_INCLUDE_DIRS}/../samples/common
                    )
add_executable(cmake_test src/main.cpp)	# 生成一个叫 cmake_test的可执行文件,其中可执行源文件路径为 src/main.cpp
# 链接库
target_link_libraries( cmake_test 
                        ${OpenCV_LIBS} 
                        ${PCL_LIBRARIES} 
                        ${CUDA_LIBRARIES} 
                        ${TensorRT_LIBS}) 

MacOS系统CMakeLists.txt添加外部库

include_directories(/path/include/)
file(GLOB XXX_LIBS /path/lib*.dylib)
target_link_libraries(exe ${XXX_LIBS}) # 链接库

# set(XXX_INCLUDE_DIRS /path/include)
# target_include_directories(name PUBLIC ${XXX_INCLUDE_DIRS}

参考:https://blog.csdn.net/qq_33179208/article/details/53558744

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值