This is actually a CMake question, not really ROS-specific.
See CMAKE_BUILD_TYPE in the CMake documentation, and perhaps this random post about CMake build types for some more background.
The build type essentially just configures the compilers optimisation level.
Should I always be running the second command instead with the build type explicitly mentioned as opposed to simply "catkin build <pkg_name>"? Is this a safe practice?
this is not easily answerable I believe.
The Release
build type will set the following flags: -O3 -NDEBUG
(at least for GCC type compilers). That is just about the maximum amount of optimisation and a debug features (such as asserts) are completely disabled.
If you're compiling your code for release purposes, then setting that build type might make sense. Especially in cases where code can make use of processor optimisations (such as SSEx, AVX, etc), allowing such optimisation might be beneficial. You've noticed quite a speed-up yourself, so that could point to a situation where it would make sense to set Release
as the build type (but be careful to do it correctly if you're contemplating doing it from within your CMakeLists.txt
).
However, optimised builds will not contain any debug symbols, will not be easily debuggable and will have disabled a lot of things like assertions that would help you write correct code. gdb
backtraces will be just about meaningless for optimised binaries (and not just because the symbols are missing).
So if you're still developing keeping the build type at its default makes more sense.
In cases where some optimisation is needed because otherwise your program doesn't work correctly (because it becomes too slow fi), the RelWithDebInfo
build type might work.
See #q172950 and #q71965 for what are almost duplicates btw.