cmakelist的作用及使用
cpp为编译型语言,需要把cpp文件编译成可执行文件
然后执行可执行文件
gcc main.cpp -o main
执行该可执行文件 ./main
Makefile 即为一个 顺序存了很多命令的文件。
我们执行make的时候,即运行makefile 按照顺序执行makefile里的指令
makefile的结构是这样的
目标:依赖目标
命令
makefile里默认一个目标就是一个文件
我们假如想要不生成这个文件,就要加上
.PHONY: 目标
使用make指令的时候,通常要加上各种参数,表明你想要的是哪个目标,比如
install:…
…
uninstall: …
…
make install 就会执行install
make uninstall 就会执行uninstall
直接 make 会自动执行第一个目标
首先 项目文件中一般会有一个 cmakelist
我们创建一个文件夹叫build(什么名字都可以),代表在这里生成可执行文件
在build路径下,执行cmake …
cmake会帮助链接好各个库,生成好makefile
再执行 make 指令,生成可执行文件
cmake_minimum_required(VERSION 2.6)
project(MY_PROJECT)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(PCL 1.9.1 REQUIRED COMPONENTS common io geometry kdtree search features surface visualization filters)
file(GLOB SOURCES main.cpp)
add_executable(server ${SOURCES})
#include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
#include_directories("/opt/libjpeg-turbo/include")
#link_directories("/opt/libjpeg-turbo/lib")
include_directories(server ${PCL_INCLUDE_DIRS} )
target_link_libraries(server ${PCL_LIBRARIES} turbojpeg zstd)
SOURCES /home/eminem/pcl_test/main.cpp
PCL_LIBRARY_DIRS /usr/lib/x86_64-linux-gnu
PCL_DEFINITIONS -DDISABLE_PCAP;-DDISABLE_PNG;-DDISABLE_LIBUSB_1_0
PCL_INCLUDE_DIRS /usr/include/pcl-1.10;/usr/include/eigen3;/usr/include;/usr/include/ni;/usr/include/openni2;/usr/include/vtk-7.1;/usr/include/freetype2;/usr/include/x86_64-linux-gnu
PCL_LIBRARIES pcl_common;pcl_octree;pcl_io;pcl_kdtree;pcl_search;pcl_features;pcl_surface;pcl_visualization;pcl_sample_consensus;pcl_filters;/usr/lib/x86_64-linux-gnu/libboost_system.so;/usr/lib/x86_64-linux-gnu/libboost_filesystem.so;/usr/lib/x86_64-linux-gnu/libboost_date_time.so;/usr/lib/x86_64-linux-gnu/libboost_iostreams.so;/usr/lib/x86_64-linux-gnu/libboost_regex.so;optimized;/home/eminem/.conda/envs/myTorch/lib/libqhull_p.so;debug;/home/eminem/.conda/envs/myTorch/lib/libqhull_p.so;/usr/lib/libOpenNI.so;/usr/lib/libOpenNI2.so;vtkChartsCore;vtkCommonColor;vtkCommonCore;vtksys;vtkCommonDataModel;vtkCommonMath;vtkCommonMisc;vtkCommonSystem;vtkCommonTransforms;vtkCommonExecutionModel;vtkFiltersGeneral;vtkCommonComputationalGeometry;vtkFiltersCore;vtkInfovisCore;vtkFiltersExtraction;vtkFiltersStatistics;vtkImagingFourier;vtkImagingCore;vtkalglib;vtkRenderingContext2D;vtkRenderingCore;vtkFiltersGeometry;vtkFiltersSources;vtkRenderingFreeType;/usr/lib/x86_64-linux-gnu/libfreetype.so;/usr/lib/x86_64-linux-gnu/libz.so;vtkFiltersModeling;vtkImagingSources;vtkInteractionStyle;vtkInteractionWidgets;vtkFiltersHybrid;vtkImagingColor;vtkImagingGeneral;vtkImagingHybrid;vtkIOImage;vtkDICOMParser;vtkmetaio;/usr/lib/x86_64-linux-gnu/libjpeg.so;/usr/lib/x86_64-linux-gnu/libpng.so;/usr/lib/x86_64-linux-gnu/libtiff.so;vtkRenderingAnnotation;vtkRenderingVolume;vtkIOXML;vtkIOCore;vtkIOXMLParser;/usr/lib/x86_64-linux-gnu/libexpat.so;vtkIOGeometry;vtkIOLegacy;vtkIOPLY;vtkRenderingLOD;vtkViewsContext2D;vtkViewsCore;vtkRenderingContextOpenGL2;vtkRenderingOpenGL2;FLANN::FLANN
project (HELLO) #指定项目名称,生成的VC项目的名称;
使用${HELLO_SOURCE_DIR}表示项目根目录
file(GLOB SOURCES *.cpp)
将所有cpp文件 的绝对路径 都加入到 SOURCES 变量中
find_package(PCL 1.9.1 REQUIRED …) 找库
include_directories:指定头文件的搜索路径,相当于指定gcc的-I参数
include_directories (${HELLO_SOURCE_DIR}/Hello) #增加Hello为include目录
link_directories:动态链接库或静态链接库的搜索路径,相当于gcc的-L参数
>> link_directories (${HELLO_BINARY_DIR}/Hello) #增加Hello为link目录
add_subdirectory:包含子目录
>> add_subdirectory (Hello)
add_executable:编译可执行程序,指定编译,好像也可以添加.o文件
>> add_executable (helloDemo demo.cxx demo_b.cxx) #将cxx编译成可执行文件——
add_definitions:添加编译参数
add_definitions(-DDEBUG)将在gcc命令行添加DEBUG宏定义;
add_definitions( “-Wall -ansi –pedantic –g”)
target_link_libraries:添加链接库,相同于指定-l参数
target_link_libraries(demo Hello) #将可执行文件与Hello连接成最终文件demo
add_library:
add_library(Hello hello.cxx) #将hello.cxx编译成静态库如libHello.a
add_custom_target:
message( status|fatal_error, “message”):
set_target_properties( … ): lots of properties… OUTPUT_NAME, VERSION, …
link_libraries( lib1 lib2 …): All targets link with the same set of libs