【问题标题】:Is there a way to tell gcc not to substitute typedef names and default parameters when reporting errors?有没有办法告诉 gcc 在报告错误时不要替换 typedef 名称和默认参数?
【发布时间】:2014-10-25 02:51:26
【问题描述】:

例如这段代码:

#include <vector>

template<typename T>
void useVector(T);

using thing = std::vector<int>;

int main()
{
    std::vector<thing> vec;
    useVector(vec);
    return 0;
}

产生过于冗长的消息:

/home/martin/Projects/TestGrounds/main.cpp:11: error: undefined reference to 
`void useVector<std::vector<std::vector<int, std::allocator<int> >, 
std::allocator<std::vector<int, std::allocator<int> > > > 
>(std::vector<std::vector<int, std::allocator<int> >, 
std::allocator<std::vector<int, std::allocator<int> > > >)'

有没有办法阻止编译器替换所有类型定义和默认参数并产生类似的东西:

/home/martin/Projects/TestGrounds/main.cpp:11: error: undefined reference to 
void useVector(std::vector<thing>);

?

【问题讨论】:

  • 试试 STLFilt [bdsoft.com/tools/stlfilt.html] 或 gccfilter [mixtion.org/gccfilter/]。 (有一段时间没有使用过,所以不确定它们处于什么状态,因此这是一条评论)。
  • 此错误消息来自链接器,而不是编译器。这使得它更加更加困难。

标签: c++ gcc compiler-errors


【解决方案1】:

按照 Eugen Constantin Dinca 的建议,我尝试了 gccfilter

输出误差减少为:

/tmp/ccEixtdl.o: In function `main':
test.cpp:(.text+0x30): undefined reference to `void useVector<>(std::vector<>)'
collect2: error: ld returned 1 exit status

这是一个巨大的改进。


首先您需要下载gccfilter perl script 并将其放在您的$PATH 中。然后,您必须安装提到的 Perl 模块。在 Ubuntu 下我刚刚做了:

cpan App::cpanminus
cpanm Term::ANSIColor
cpanm Getopt::ArgvFile
cpanm Getopt::Long
cpanm Regexp::Common

您可以通过将原始问题的源代码保存为test.cpp 来测试脚本并运行:

gccfilter --colorize --remove-template-args g++ -std=c++11 test.cpp

如果您使用的是链接器,则必须使用以下命令运行链接器:

gccfilter --colorize --remove-template-args g++

也是。如果您使用的是qmake,您可以添加:

QMAKE_CXX="gccfilter -c -a g++"
QMAKE_LINK="gccfilter -c -a g++"

到您的.pro 文件。对于 QtCreator,这将破坏 Issues 选项卡中错误消息的查看,但您可以在 Compile Output 选项卡中查看格式化和简化的错误消息。

【讨论】:

    猜你喜欢
    • 2012-03-17
    • 2011-07-31
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2018-12-26
    • 2012-07-16
    相关资源
    最近更新 更多