首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免“警告:在gperf输出文件中声明UserSuppliedStruct不声明任何东西”

避免“警告:在gperf输出文件中声明UserSuppliedStruct不声明任何东西”
EN

Stack Overflow用户
提问于 2014-03-11 09:52:58
回答 1查看 643关注 0票数 0

给定这样一个带有用户提供的结构的gperf文件:

代码语言:javascript
复制
%define class-name Table
%define lookup-function-name m

%struct-type
%language=C++

%{
#include <cstring>
#include <cstdio>
// defination of LookupTableElement is put to a header file in the real project and included in
namespace bar {
  struct LookupTableElement {
    const char *name;
    void (*func) ();
  };
}

// a handler
void ack() {
  puts("you said hello");
}

// namespace bar {
%}
struct bar::LookupTableElement;//gperf needs the declaration
%%
######
hello,     ack
######
%%
// } // end namespace bar
int main() {
  auto p = Table::m("hello", sizeof("hello") - 1);
  if (!p) return 1;
  p->func();
  return 0;
}

汇编:

代码语言:javascript
复制
$ gperf foo.gperf > foo.cc && g++ -std=c++0x foo.cc

让g++ (gcc 4.7.3和4.8.2测试)警告:

代码语言:javascript
复制
foo.gperf:26:13: warning: declaration ‘struct bar::LookupTableElement’ does not declare anything [enabled by default]
struct bar::LookupTableElement;//declare look up table's element
            ^

如果namespace bar被删除,将不再有任何警告。

避免警告的最好方法是什么?

  1. 我是否应该在每个gperf文件中定义bar::LookupTableElement (有多个使用结构的gperf )?
  2. 或者使用类似的东西(在GCC手册中还没有找到关闭开关)?
  3. 取消// namespace bar {// } // end namespace bar,并将struct bar::LookupTableElement更改为struct LookupTableElement。但是以这种方式,我们将把很多东西拖到名称空间中(看看生成的foo.cc,您就知道了)。
  4. 还有别的主意吗?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-02 08:24:35

gperf有一种选择:

代码语言:javascript
复制
   -T, --omit-struct-type
          Prevents  the  transfer  of  the type declaration to the output file. Use
          this option if the type is already defined elsewhere.

所以,没有任何命名空间技巧,

代码语言:javascript
复制
struct bar::LookupTableElement;

这个选项会生成完全可以接受的代码(例如gperf -T foo.gperf > foo.gperf.cpp)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22322100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档