gpt4 book ai didi

gcc - 如何在构建目标之外生成 gcc 调试符号?

转载 作者:行者123 更新时间:2023-11-30 16:58:44 26 4
gpt4 key购买 nike

我知道我可以使用 -g 选项生成调试符号。但是该符号嵌入在目标文件中。 gcc 可以在结果可执行文件/库之外生成调试符号吗?就像windows VC++编译器的.pdb文件一样。

最佳答案

您需要使用objcopyseparate the debug information :

objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"

我使用下面的 bash 脚本将调试信息分离到 .debug 目录中具有 .debug 扩展名的文件中。这样我就可以将库和可执行文件打包到一个 tar 文件中,并将 .debug 目录打包到另一个 tar 文件中。如果我想稍后添加调试信息,我只需提取调试 tar 文件,瞧,我就有了符号调试信息。

这是 bash 脚本:

#!/bin/bash

scriptdir=`dirname ${0}`
scriptdir=`(cd ${scriptdir}; pwd)`
scriptname=`basename ${0}`

set -e

function errorexit()
{
errorcode=${1}
shift
echo $@
exit ${errorcode}
}

function usage()
{
echo "USAGE ${scriptname} <tostrip>"
}

tostripdir=`dirname "$1"`
tostripfile=`basename "$1"`


if [ -z ${tostripfile} ] ; then
usage
errorexit 0 "tostrip must be specified"
fi

cd "${tostripdir}"

debugdir=.debug
debugfile="${tostripfile}.debug"

if [ ! -d "${debugdir}" ] ; then
echo "creating dir ${tostripdir}/${debugdir}"
mkdir -p "${debugdir}"
fi
echo "stripping ${tostripfile}, putting debug info into ${debugfile}"
objcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"
strip --strip-debug --strip-unneeded "${tostripfile}"
objcopy --add-gnu-debuglink="${debugdir}/${debugfile}" "${tostripfile}"
chmod -x "${debugdir}/${debugfile}"

关于gcc - 如何在构建目标之外生成 gcc 调试符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com