使 gcc 将相关文件名放入调试信息中 [英] Make gcc put relative filenames in debug information

查看:22
本文介绍了使 gcc 将相关文件名放入调试信息中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编译的项目使用 CMake,它喜欢绝对路径名.

The project I'm compiling uses CMake, which loves absolute pathnames.

当我在启用调试信息的情况下编译时,gcc 将这些长名称放入 .debug_str 部分,这对调试不利.我想在那里使用较短的相对于项目根路径名.

When I compile with debugging information enabled, gcc puts those long names into .debug_str sections, which is bad for debugging. I'd like to have short relative-to-project-root pathnames there instead.

是否有一些选项可以告诉 gcc 在发出调试数据之前去除路径名的某些部分?或者,也许有一些工具可以对编译后的二进制文件执行此操作?

Is there some option to tell gcc to strip some part of pathname before emitting debug data? Or, maybe, there is some tool that could do that on compiled binaries?

我尝试使用 SET(CMAKE_USE_RELATIVE_PATHS ON)(这似乎是 不赞成 开发者) 选项,但由于我使用的是源外构建,路径名仍然不是我希望它们的形式.IE.它们是 ./../src/mod_foo/foo.c 而不是 mod_foo/foo.c.

I've tried using SET(CMAKE_USE_RELATIVE_PATHS ON) (which seems to be frowned upon by devs) option, but as I'm using out-of-source builds, pathnames are still not in the form I'd want them to be. I.e. they're ./../src/mod_foo/foo.c instead of mod_foo/foo.c.

推荐答案

您可以设置RULE_LAUNCH_COMPILE 属性,让 CMake 调用 shell 脚本,该脚本在调用 gcc 之前将源文件路径转换为项目相对路径.使用 CMake 函数 configure_file 生成一个 shell 脚本,它知道项目的 PROJECT_SOURCE_DIRPROJECT_BINARY_DIR.

You can set the RULE_LAUNCH_COMPILE property of a CMake target to have CMake invoke a shell script which transforms the source file path to a project relative path before invoking gcc. Use the CMake function configure_file to generate a shell script which knows about the PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR of your project.

在最外面的CMakeLists.txt 中添加以下代码:

In your outermost CMakeLists.txt add the following code:

configure_file(
    "${PROJECT_SOURCE_DIR}/gcc_debug_fix.sh.in"
    "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh"
    @ONLY)

add_executable (MyExecutable ...)

set_target_properties(MyExecutable PROPERTIES 
    RULE_LAUNCH_COMPILE "${PROJECT_BINARY_DIR}/gcc_debug_fix.sh")

以下模板shell脚本gcc_debug_fix.sh.in需要到CMake项目的根目录:

The following template shell script gcc_debug_fix.sh.in needs to go to the root directory of the CMake project:

#!/bin/sh

PROJECT_BINARY_DIR="@PROJECT_BINARY_DIR@"
PROJECT_SOURCE_DIR="@PROJECT_SOURCE_DIR@"

# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -o OBJECT_FILE -c SOURCE_FILE

# extract parameters
SOURCE_FILE="${@: -1:1}"
OBJECT_FILE="${@: -3:1}"
COMPILER_AND_FLAGS=${@:1:$#-4}

# make source file path relative to project source dir
SOURCE_FILE_RELATIVE="${SOURCE_FILE:${#PROJECT_SOURCE_DIR} + 1}"

# make object file path absolute
OBJECT_FILE_ABSOLUTE="$PROJECT_BINARY_DIR/$OBJECT_FILE"

cd "$PROJECT_SOURCE_DIR"

# invoke compiler
exec $COMPILER_AND_FLAGS -c "${SOURCE_FILE_RELATIVE}" -o "${OBJECT_FILE_ABSOLUTE}"

shell 脚本使用来自变量 PROJECT_BINARY_DIRPROJECT_SOURCE_DIR 的信息将源文件的路径转换为相对于项目根目录和目标文件路径的路径到绝对路径.因为 gcc 现在传递了一个项目相对路径,.debug_str 也应该使用该路径.

The shell script uses the information from the variables PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR to transform the path of the source file to a path relative to the project root and the object file's path to an absolute path. Because gcc gets passed a project relative path now, .debug_str should use that path, too.

以下注意事项适用:

  • 一定要设置gcc_debug_fix.sh.in的可执行位.
  • 要使脚本运行 CMAKE_USE_RELATIVE_PATHS 必须再次设置为 OFF.
  • 脚本对命令行中文件路径的位置进行了假设.如果 CMake 使用不同的规则来调用编译器,这可能不起作用.更可靠的解决方案是扫描 -o-c 标志的脚本参数.
  • Be sure to set the executable bit of gcc_debug_fix.sh.in.
  • For the script to work CMAKE_USE_RELATIVE_PATHS has to set to OFF again.
  • The script makes assumptions about the location of the file paths on the command line. This may not work if CMake uses a different rule to invoke the compiler. A more robust solution would be to scan the script arguments for the -o and -c flags.

这篇关于使 gcc 将相关文件名放入调试信息中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆