为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

如何在构建目标之外生成gcc调试符号?我知道我可以使用-g选项生成调试符号。但是,符号将嵌入目标文件中。gcc可以在结果可执行文件/库之外生成调试符号吗?像Windows VC ++编译器的.pdb文件一样。
查看完整描述

3 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

您需要使用objcopy来分隔调试信息:

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

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

这是bash脚本:

#!/bin/bashscriptdir=`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}"


查看完整回答
反对 回复 2019-07-31
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

查看strip命令的“--only-keep-debug”选项。

从链接:

目的是将此选项与--add-gnu-debuglink结合使用以创建两部分可执行文件。一个剥离的二进制文件占用RAM和分布中较少的空间,第二个是调试信息文件,仅在需要调试功能时才需要。


查看完整回答
反对 回复 2019-07-31
  • 3 回答
  • 0 关注
  • 638 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信