2019.03.04更新:一些CMakelists里面的编译选项,这里参考了https://blog.csdn.net/feisy/article/details/17711957的博文
set(CMAKE_BUILD_TYPE "Release")
if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -Wall -Wno-unused-variable -pthread")
else( CMAKE_BUILD_TYPE STREQUAL "Debug" )
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -pthread -fopenmp")
endif( CMAKE_BUILD_TYPE STREQUAL "Debug" )
message("Build Type:"${CMAKE_BUILD_TYPE} ${CMAKE_CXX_FLAGS})
debug模式下,加-Os,速度会快
Release模式下,加 -O2,和不加,程序运行速度有比较显著的差异
再补充一些关于GCC的优化等级的问题:
少优化->多优化:
O0 -->> O1 -->> O2 -->> O3
-O0表示没有优化,-O1为缺省值,-O3优化级别最高
下面参考 https://blog.csdn.net/yanghan1233/article/details/51201967 中的说法:
gcc - o1 首先o1上面还有一个o0,那个是不提供任何优化,项目中几乎不会使用,而o1使用就非常广泛了,o1是最基本的优化,主要对代码的分支,表达式,常量来进行优化,编译器会在较短的时间下将代码变得更加短小,这样体积就会变得更小,会减少内存的占用率,在操作系统进行内存调度时就会更快。但是事情没有绝对的优点,当一个庞大的程序被拆碎细分的话,内存占用会大大增加,由于当今系统大多数都是多线程,就会出现卡顿和反应延迟。
gcc - o2 这个优化级别是o1的进阶,在上一级的基础上会进行更严格的细分,最重要的是加入了寄存器的使用。寄存器是cpu中重要的组成部分,此外还有运算器和控制器,计算机顾名思义,要进行各种庞杂的计算,由于cpu速度较快,所以计算的中间结果都会保存在寄存器中,这样可以大大提高系统的效率,但是寄存器造价高昂,数量有限,所以一般来说程序不会放在寄存器中,另一种将代码放在寄存器的方式是使用register修饰变量,适用于频繁调用的变量。
gcc - o3 这个优化属于非常强大的优化,因为编译器会进行预测,对循环每一层的预测,以便于将循环拆分,可以提高执行效率。编译器还会试图用已有的值来代替未知的值,并且还会用加代替乘,因为运算器的特性,乘法十分复杂耗时。当然o3的缺点最明显,那就是o3因为试图预测程序的走向,可能会出现误差,导致错误和程序不可逆转的走向。所以一般o3不建议使用。
博文 https://blog.csdn.net/lanmanck/article/details/5776173 对-Os有如下描述:
在研究编译驱动的makefile的时候,发现GCC的命令行里面有一个-Os的优化选项。
遍查GCC文档,发现了-O0, -O1, -O2, -O3,就是没有发现-Os。
祭出GOOGLE大法搜了一下,终于发现这篇文章说明了-Os的作用:
http://www.linuxjournal.com/article/7269原来-Os相当于-O2.5。是使用了所有-O2的优化选项,但又不缩减代码尺寸的方法。
详细的说明如下:
Level 2.5 (-Os)The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations. The alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. Skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particular optimizations are disabled. The size optimization level is enabled as:
gcc -Os -o test test.c
In gcc 3.2.2, reorder-blocks is enabled at -Os, but in gcc 3.3.2 reorder-blocks is disabled.==============================
补充:在GCC的官方文档里又发现了关于-Os的说明:
http://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Optimize-Options.html#Optimize-Options
//=====================下面是原文的参考样例=======================
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation)
## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)
## Generate added messages and services
add_service_files(
FILES
AddTwoInts.srv
)
generate_messages(DEPENDENCIES std_msgs)
## Declare a catkin package
catkin_package()
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
add_dependencies(add_two_ints_server beginner_tutorials_gencpp)
add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
add_dependencies(add_two_ints_client beginner_tutorials_gencpp)
cmake_minimum_required(VERSION 2.8.3)
project(carla_localization)
find_package(catkin REQUIRED
roscpp
rospy
carla_ros_bridge
sensor_msgs
pcl_ros
pcl_conversions
message_generation)
find_package(PCL REQUIRED)
add_message_files(FILES speed.msg)
## Generate added messages and services with any dependencies listed here
generate_messages(
DEPENDENCIES
sensor_msgs
# geometry_msgs
std_msgs # Or other packages containing msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES carla_localization
# CATKIN_DEPENDS other_catkin_pkg
# DEPENDS system_lib
)
# 指定构建project所需要的资源
include_directories(${PCL_INCLUDE_DIRS})#包含头文件的位置
link_directories(${PCL_LIBRARY_DIRS})#添加链接器的lib库文件路径
add_definitions(${PCL_DEFINITIONS})
include_directories("./src/BasicFuns")
add_subdirectory("./src/BasicFuns") #这里还是文件夹的名字
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
# add_executable(receiver src/receiver.cpp)
# target_link_libraries(receiver ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
# add_executable(localization src/receive_localization.cpp)
# target_link_libraries(localization ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
add_executable(localizationICP src/localization_ICP.cpp)
target_link_libraries(localizationICP ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
add_executable(local src/local_localization.cpp)
target_link_libraries(local ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
add_executable(localization src/localization_online.cpp)
target_link_libraries(localization ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
add_executable(localGT src/local_gt.cpp)
target_link_libraries(localGT ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
add_executable(mapsending src/Map2rviz.cpp)
target_link_libraries(mapsending ${catkin_LIBRARIES} ${PCL_LIBRARIES} basicFun)
下面是package.xml
carla_localization
0.0.1
The carla_localization package
think33
TODO
catkin
rospy
roscpp
sensor_msgs
tf
libpcl-all-dev
rospy
roscpp
sensor_msgs
tf
roscpp
sensor_msgs
tf
tf2
rviz
ackermann_msgs
cv_bridge
geometry_msgs
std_msgs
rosbag_storage
beginner_tutorials
0.0.0
The beginner_tutorials package
think33
TODO
catkin
roscpp
rospy
std_msgs
roscpp
rospy
std_msgs
roscpp
rospy
std_msgs
message_generation