您当前的位置:首页 > 计算机 > 编程开发 > 编译原理

CMake代码优化编译选项

时间:07-20来源:作者:点击数:

https://www.ztyii.com/ex/cmake-o2-opt/

Mac有一次系统升级后,发现clang不支持了openmp了,最后brew安装了clang-omp。但是好不容易配置起来后发现程序运行速度巨慢,今天发现没有打开编译优化,在CMakeLists.txt文件中加入以下编译选项即可(o2优化,甚至o3):

SET(CMAKE_C_FLAGS “KaTeX parse error: Double subscript at position 9: {CMAKE_C_̲FLAGS} -O2") SE…{CMAKE_CXX_FLAGS} -O2”)

程序速度提升4~5倍

https://www.it1352.com/1984337.html

To address the first issue, you should be able to do this early in your CMakeList:

if(NOT CMAKE_BUILD_TYPE)

set(CMAKE_BUILD_TYPE Release)

endif()

set(CMAKE_CXX_FLAGS “-Wall -Wextra”)

set(CMAKE_CXX_FLAGS_DEBUG “-g”)

set(CMAKE_CXX_FLAGS_RELEASE “-O3”)

This will make sure that if you do not specify a build type at all, it will default to “Release” and thus CMAKE_CXX_FLAGS_RELEASE will be used.

The second one is harder to tackle. Variables passed from the command line (such as CMAKE_BUILD_TYPE=Debug) are cached by CMake and thus re-used in subsequent invocations (that is necessary, since CMake can re-trigger itself if you modify its inputs between builds).

The only solution is to make the user switch the build type explicitly again, using cmake … -DCMAKE_BUILD_TYPE=Release.

Consider why this is necessary: as I said, CMake can re-trigger itself as part of a build if CMake’s input (CMakeLists.txt files or their dependencies) has changed since last CMake ran. In such case, it will also be run without command-line arguments such as -DCMAKE_BUILD_TYPE=whatever, and will rely on the cache to supply the same value as last time. This scenario is indistinguishable from you manually running cmake … without additional arguments.

I could provide a hacky solution to always reset CMAKE_BUILD_TYPE to Release if not specified explicitly on the command line. However, it would also mean that a buildsystem generated as Debug would get re-generated as Release if automatic re-generation happened. I am pretty sure that’s not what you want.

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门