为什么 cmake 生成带有 debug_info 的二进制文件而不是剥离

发布于 2025-01-09 13:37:18 字数 866 浏览 0 评论 0原文

为什么 cmake 生成带有 debug_info 的二进制文件而不被删除?

这是我的 CMakeLists.txt

cmake_minimum_required(VERSION 3.22)

# project name
project(c-program)

set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_C_STANDARD "90")
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_C_FLAGS "-ansi -Wall")
set(CMAKE_C_FLAGS_DEBUG "-g3 -ggdb3")
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")

add_executable(c-program c-program.c)

我从项目根文件夹 $ cmake -S 构建

。 -B build/release -DCMAKE_BUILD_TYPE=Release

$ cmake --build build/release

运行时

$ file build/release/c-program

我得到

build/release/c-program:ELF 64 位 LSB 饼可执行文件,x86-64,版本 1 (SYSV),动态链接,解释器 /lib/ld-musl-x86_64.so.1,使用 debug_info,未剥离

我对调试/发布版本应该如何工作感到困惑,并且 cmake 教程指南或文档或其他在线内容中没有任何内容可以帮助我弄清楚为什么会发生这种情况

why is cmake producing a binary with debug_info and not stripped?

here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.22)

# project name
project(c-program)

set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_C_STANDARD "90")
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_C_FLAGS "-ansi -Wall")
set(CMAKE_C_FLAGS_DEBUG "-g3 -ggdb3")
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")

add_executable(c-program c-program.c)

i build like this from the project root folder

$ cmake -S . -B build/release -DCMAKE_BUILD_TYPE=Release

$ cmake --build build/release

when running

$ file build/release/c-program

i get

build/release/c-program: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, with debug_info, not stripped

i am confused about how debug/release builds are supposed to work, and nothing in the cmake tutorial guides or docs or otherwise online is helping me figure out why this is happening

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送舟行 2025-01-16 13:37:19

CMakeLists.txt 中设置标志存在一些问题,因此添加和使用工具链文件会有所帮助,并在工具链文件。此外,链接器正在添加调试信息。将 -s 标志添加到 cmake 发布标志,并将 -Wl,--strip-debug 添加到链接器标志是我正在寻找的,

这是我的最终 CMakeLists .txt工具链 文件

#CMakeLists.txt
cmake_minimum_required(VERSION 3.22)

# set toolchain file
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/toolchain.cmake"
    CACHE PATH "Path to the desired toolchain file.")

# project name
project(c-program C)

add_executable(c-program c-program.c)
# toolchain.cmake
# CMake toolchain file

# c compiler and standard
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_C_STANDARD "90")
set(CMAKE_C_STANDARD_REQUIRED YES)

# linker flags
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--strip-debug")

# cflags
set(CMAKE_C_FLAGS_INIT "-ansi -Wall")

# cflags for debug build
set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -ggdb3")

# cflags for release build
set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG -s")

there are some issues with setting flags in CMakeLists.txt, so adding and using a toolchain file helped, and setting appropriate CMAKE_<LANG>_FLAGS_<CONFIG>_INIT vars in the toolchain file. also, the linker is adding debug info. adding -s flag to cmake release flags, and adding -Wl,--strip-debug to linker flags is what i was looking for

here is my final CMakeLists.txt and toolchain files

#CMakeLists.txt
cmake_minimum_required(VERSION 3.22)

# set toolchain file
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/toolchain.cmake"
    CACHE PATH "Path to the desired toolchain file.")

# project name
project(c-program C)

add_executable(c-program c-program.c)
# toolchain.cmake
# CMake toolchain file

# c compiler and standard
set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_C_STANDARD "90")
set(CMAKE_C_STANDARD_REQUIRED YES)

# linker flags
set(CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--strip-debug")

# cflags
set(CMAKE_C_FLAGS_INIT "-ansi -Wall")

# cflags for debug build
set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -ggdb3")

# cflags for release build
set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG -s")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文