Eri*_*und 1 c++ assembly g++ cmake
如果我编译C++程序/tmp/src/main.cc
#include <iostream>
int main() {
#ifdef demo1
std::cout << "Output from demo1\n";
#endif
#ifdef demo2
std::cout << "Output from demo2\n";
#endif
}
Run Code Online (Sandbox Code Playgroud)
使用文件/tmp/src/CMakeLists.txt中的构建指令
cmake_minimum_required(VERSION 3.11)
project(test_save_temps LANGUAGES CXX)
function(my_add_executable name)
add_executable(${name})
target_sources(${name} PRIVATE main.cc)
# target_compile_options(${name} PRIVATE --save-temps)
target_compile_definitions(${name} PRIVATE
${name}
)
endfunction()
my_add_executable(demo1)
my_add_executable(demo2)
Run Code Online (Sandbox Code Playgroud)
一切看起来都不错。
cmake_minimum_required(VERSION 3.11)
project(test_save_temps LANGUAGES CXX)
function(my_add_executable name)
add_executable(${name})
target_sources(${name} PRIVATE main.cc)
# target_compile_options(${name} PRIVATE --save-temps)
target_compile_definitions(${name} PRIVATE
${name}
)
endfunction()
my_add_executable(demo1)
my_add_executable(demo2)
Run Code Online (Sandbox Code Playgroud)
但是如果我从文件/tmp/src/CMakeLists.txt中删除注释 以激活该行
target_compile_options(${name} PRIVATE --save-temps)
Run Code Online (Sandbox Code Playgroud)
并做同样的事情
ubuntu@laptop:/tmp$ mkdir /tmp/build
ubuntu@laptop:/tmp$ cd /tmp/build
ubuntu@laptop:/tmp/build$ cmake -G Ninja /tmp/src
-- The CXX compiler identification is GNU 8.2.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/build
ubuntu@laptop:/tmp/build$ ninja
[4/4] Linking CXX executable demo1
ubuntu@laptop:/tmp/build$ ls
build.ninja CMakeCache.txt CMakeFiles cmake_install.cmake demo1 demo2 rules.ninja
ubuntu@laptop:/tmp/build$ ./demo1
Output from demo1
ubuntu@laptop:/tmp/build$ ./demo2
Output from demo2
ubuntu@laptop:/tmp/build$
Run Code Online (Sandbox Code Playgroud)
程序demo2的输出不正确。
我希望在构建目录中找到两个版本的汇编文件main.s 。
在这里我提供一些有关我的计算机系统的额外信息
如何修改/tmp/src/CMakeLists.txt以保留程序集文件main.s的两个版本?
当我按照用户fritzone的答案/sf/answers/3766774511/-save-temps=obj
中的建议使用时,一切都开始工作。
我更换了
target_compile_options(${name} PRIVATE --save-temps)
Run Code Online (Sandbox Code Playgroud)
和
ubuntu@laptop:/tmp$ mkdir /tmp/build_with_save_temps
ubuntu@laptop:/tmp$ cd /tmp/build_with_save_temps
ubuntu@laptop:/tmp/build_with_save_temps$ cmake -G Ninja /tmp/src
-- The CXX compiler identification is GNU 8.2.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/build_with_save_temps
ubuntu@laptop:/tmp/build_with_save_temps$ ninja
[4/4] Linking CXX executable demo1
ubuntu@laptop:/tmp/build_with_save_temps$ ls
build.ninja CMakeCache.txt CMakeFiles cmake_install.cmake demo1 demo2 main.ii main.s rules.ninja
ubuntu@laptop:/tmp/build_with_save_temps$ ./demo1
Output from demo1
ubuntu@laptop:/tmp/build_with_save_temps$ ./demo2
Output from demo1
ubuntu@laptop:/tmp/build_with_save_temps$ find . -name '*.s'
./main.s
ubuntu@laptop:/tmp/build_with_save_temps$
Run Code Online (Sandbox Code Playgroud)
现在我得到了两个汇编文件
target_compile_options(${name} PRIVATE --save-temps)
Run Code Online (Sandbox Code Playgroud)
可执行文件demo1和demo2按预期工作
target_compile_options(${name} PRIVATE -save-temps=obj)
Run Code Online (Sandbox Code Playgroud)
您需要为编译器指定-save-temps=obj
选项,以便根据目标文件保存临时文件,根据:
https: //gcc.gnu.org/onlinedocs/gcc/Developer-Options.html#Developer-Options
因此,您需要将 cmake 文件修改为:
target_compile_options(${name} PRIVATE -save-temps=obj)
Run Code Online (Sandbox Code Playgroud)
(或类似),以便根据您尝试编译的应用程序的名称保存临时文件。