当前位置: 代码迷 >> 综合 >> linux cpp makefile or cmakelist
  详细解决方案

linux cpp makefile or cmakelist

热度:42   发布时间:2023-12-11 23:54:16.0

目录结构
在这里插入图片描述
程序1:main,foo_1.cpp,main.h,foo_1.h
程序2:other.cpp
makefile2只是个别名,可make -f makefile2来编译

对于makefile

方法1

all:maincc otherr
#第一个maincc是自己随便起的要生成的可执行文件的名字,main.o和foo_1.o是生成maincc要用到的文件, 编译顺序是main.o再到foo_1.o
maincc:main.o foo_1.o                   g++ foo_1.o main.o -o maincc    #用g++把foo_1.o和main.o两个文件编译成可执行文件#并用 -o 将其命名为maincc
#这一步是main.o的生成,main.cpp是生成main.o要用到的文件
main.o:main.cpp main.h foo_1.hg++ -c main.cpp   #用g++ -c 把main.cpp编译成.o文件
#这一步是foo_1.o的生成,foo_1.cpp是生成foo_1.o要用到的文件
foo_1.o:foo_1.cpp foo_1.hg++ -c foo_1.cpp   #用g++ -c 把foo_1.cpp编译成.o文件#编译第二个文件
otherr:other.cpp g++ other.cpp  -o otherrother.o:other.cpp g++ -c other.cpp #编译完成生成的.o文件用不到,可以加两句话自动把.o删掉(编译时要在终端里输入make clean)
.PHONY: clean
clean:rm -f *.o# 方法2
# 另外,生成.o文件是为了加深理解,也可以直接用g++将两个.cpp文件编译成一个可执行文件
# maincc:main.cpp foo_1.cpp
# g++ foo_1.cpp main.cpp -o maincc

方法3(大项目必须会使用的makefile写法)参考这位博主

#Makefile有三个非常有用的变量。分别是$@,$^,$<代表的意义分别是:
#$@--目标文件的集合,$^--所有的依赖文件的集合,$<--第一个依赖文件,若有多个目标文件,依次编译依赖文件。
target1=maincc        #服务器运行目标文件server
target2=otherr        #客户端目标文件client
src=$(wildcard *.cpp)   #用wildcard函数找到所有.c文件,server.c、client.c、wrap.c
deps=$(wildcard *.h)   #用wildcard函数找到所有的.h文件,wrap.h
obj=$(patsubst %.cpp,%.o,$(src))    #用patsubst函数将所有.c文件替换成.o文件#目标文件server和client,多个目标文件一定形成此依赖关系
all:$(target1) $(target2)    @echo "ok"
#server的依赖为server.o和wrap.o
$(target1):main.o foo_1.o g++ $^ -o $@ -Wall   #利用gcc编译器生成可执行文件server#client的依赖为client.o和wrap.o
$(target2):other.o  g++ $^ -o $@ -Wall  #利用gcc编译器生成可执行文件client#任意一个.o中间目标文件的依赖是其对应的.c文件,如:client.o的依赖为client.c
%.o:%.cpp $(deps)  g++ -c $< -o $@ -Wall   #根据目标文件编译的需求依次将依赖编译成对应的中间目标文件#伪文件,需要在终端输入make clean才会调用
.PHONY:clean  
clean:-rm -rf $(target1) $(target2) $(obj)   #删除所有的目标文件以及中间目标文件,用于重新编译。

之后就变成这样了
在这里插入图片描述

对于cmakelist

#cmake的版本要求
cmake_minimum_required(VERSION 3.6)
#工程的名称
project(algorTest)
#添加可执行文件
add_executable(other other.cpp)
#第2个选项必须要有,可以选PUBLIC|PRIVATE|INTERFACE
target_sources(other PUBLIC )#编译多个文件的方法1
add_executable(other2 other2.cpp)
#编译多个文件的方法2,两句
#set(SOURCES other2.cpp) 
#add_executable(other2 ${SOURCES}) 

在这里插入图片描述

mkdir build && cd build
cmake ..
make

在这里插入图片描述

对于catkin_make

先说明一下catkin_make和cmake关系

cmake编译是这样的流程, cmake指令依据你的CMakeLists.txt
文件,生成makefiles文件,make再依据此makefiles文件编译链接生成可执行文件.

catkin_make是将cmake与make的编译方式做了一个封装的指令工具, 规范了工作路径与生成文件路径.

我通常是在ros下用catkin_make
附上代码,里面有对应解释

cmake_minimum_required(VERSION 3.0.2)
project(pcl_learning)## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages# find_package(catkin 中)添加一个自己写的功能包,它会被当前package依赖, 因此,它也需要在package.xml中配置build_depend和run_depend
find_package(catkin REQUIRED COMPONENTSroscpprospystd_msgspcl_ros
)# 如果找到libLAS库就把头文件路径和库文件路径赋值给 include_directories中的 ${
    libLAS_INCLUDE_DIRS}、 target_link_libraries中的${
    libLAS_LIBRARIES}find_package(PCL 1.8 REQUIRED)  
#find_package(libLAS REQUIRED)# 对存在.cmake库文件的情况直接find_package(),而对于可能没有***.cmake和***Config.cmake的库文件,可以直接找到其头文件和库文件所在文件夹,直接进行路径赋值:
#SET(libLAS_DIR /usr/local/lib/) 
#SET(libLAS_INCLUDE_DIRS /usr/local/include) 
#SET(libLAS_LIBRARIES /usr/local/lib)## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()################################################
## Declare ROS messages, services and actions ##
################################################## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)## Generate messages in the 'msg' folder
# add_message_files(
# FILES
# Message1.msg
# Message2.msg
# )## Generate services in the 'srv' folder
# add_service_files(
# FILES
# Service1.srv
# Service2.srv
# )## Generate actions in the 'action' folder
# add_action_files(
# FILES
# Action1.action
# Action2.action
# )## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# std_msgs
# )################################################
## Declare ROS dynamic reconfigure parameters ##
################################################## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(INCLUDE_DIRS include
# LIBRARIES pcl_learningCATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)###########
## Build ##
############# Specify additional locations of header files
## Your package locations should be listed before other locations# 添加头文件目录(四件套include_directories()+link_directories()+add_executable()+target_link_libraries()和三件套include_directories()+LINK_LIBRARIES()+add_executable())
include_directories(
# include${
    catkin_INCLUDE_DIRS}${
    PCL_INCLUDE_DIRS}${
    libLAS_INCLUDE_DIRS}/home/jzi/pcl_test/src/pcl_learning/lib/libLAS/include
)# 添加需要链接的库文件目录
# link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib )
# link_directories(/usr/lib/aarch64-linux-gnu/)
# link_directories(/home/jzi/pcl_test/src/pcl_learning/lib/libLAS/include)# 调用静态库或者动态库,用在add_executable之前,需要全路径(有这个就不需要target_link_libraries和link_directories)
# LINK_LIBRARIES(/usr/lib/aarch64-linux-gnu/libboost_system.so)
# LINK_LIBRARIES(libboost_system.a)
LINK_LIBRARIES(/home/jzi/pcl_test/src/pcl_learning/lib/libLAS/build/bin/Release/liblas.so)# 添加源文件,导入单个文件也可以不添加源文件生成so库,可以直接用:add_executable(1_pcl_lasTopcd 1_pcl_lasTopcd.cpp depend.cpp)
FILE(GLOB SOURCE_FILES src/*/*.c src/*/*.cpp src/1_pcl_lasTopcd.cpp)# 将pcl_test_learning下的源文件编译成库文件,生成so包,名字叫libpcl_test_learning.so
add_library(pcl_test_learning ${
    SOURCE_FILES})  
link_libraries(pcl_test_learning)
# 添加一个子目录并对子文件夹项目进行cmake编译,add_subdirectory(src)## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/pcl_learing.cpp
# )## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/pcl_learing_node.cpp)## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})## Specify libraries to link a library or executable target against
# target_link_libraries(${PROJECT_NAME}_node
#   ${
    catkin_LIBRARIES}
# )#############
## Install ##
############## all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# catkin_install_python(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
# install(TARGETS ${PROJECT_NAME}_node
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )## Mark libraries for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_libraries.html
# install(TARGETS ${PROJECT_NAME}
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
# )## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
#   # myfile1
#   # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )#############
## Testing ##
############### Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_pcl_learing.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()## Add folders to be run by python nosetests
# catkin_add_nosetests(test)# 添加要编译的可执行文件编译成可执行文件
add_executable(1_pcl_lasTopcd 1_pcl_lasTopcd.cpp)
# 提醒编译器需要先生成pcl_vehicle_node后面的依赖再生成pcl_vehicle_node
add_dependencies(1_pcl_lasTopcd ${
    ${
    PROJECT_NAME}_EXPORTED_TARGETS} ${
    catkin_EXPORTED_TARGETS})
# 设置要链接的库文件的名称,用在add_executable之后(按照header file + .lib + .dll方式隐式调用动态库的.lib库),pcl_vehicle_node才能找到第三方库
target_link_libraries(1_pcl_lasTopcd ${
    catkin_LIBRARIES} ${
    libLAS_LIBRARIES}${
    PCL_LIBRARIES})