无法使用clang编译代码,但可以使用gcc

时间:2015-06-24 20:35:46

标签: c++ xcode clang++

我正在尝试修复一个开源C ++项目中的一些错误,原作者目前忙于他的学术生活来提供帮助。代码通过macports安装gcc-4.9编译得很好。我一直在命令行上使用lldb进行调试。但是,如果可能的话,我希望使用clang来编译代码,因为这样可以让我一起使用Xcode和lldb,并使bug更容易隔离。

当我尝试使用clang编译代码时,出现以下错误:

In file included from ./src/include/hash.h:25:
./src/include/hash_stream.h:18:11: error: call to function 'operator>>' that is neither visible in the template definition
  nor found by argument-dependent lookup
  iss >> table[key] ;
      ^
./src/common/tagger/implementations/collins/tagger.h:118:9: note: in instantiation of function template specialization
  'operator>><CWord, english::CTag>' requested here
  i >> (*m_TopTags);
    ^
./src/english/tags.h:29:23: note: 'operator>>' should be declared prior to the call site or in namespace 'english'
inline std::istream & operator >> (std::istream &is, english::CTag &tag) {
                  ^
1 error generated.
make: *** [obj/english.postagger.o] Error 1

this文件中出现此错误,该文件基本上是尝试读取密钥:,以及用空格分隔成自定义哈希映射的值。我已经阅读了clang兼容性指南中的相关section,但我无法弄清楚我需要更改什么来进行编译。

编辑:根据@ m-s,我修改了src/common/tagger/implementations/collins/tagger_include.h以将tags.h移到hash_stream.h之上,这似乎解决了这个错误。但是,我现在遇到了一个新的错误:

In file included from ./src/common/conparser/implementations/muhua/weight.cpp:13:
In file included from ./src/common/conparser/implementations/muhua/weight.h:13:
In file included from ./src/common/conparser/weight_base.h:13:
In file included from ./src/common/conparser/base_include.h:10:
In file included from ./src/english/tags.h:43:
In file included from ./src/english/pos/penn_morph.h:15:
In file included from ./src/include/knowledge/tagdict.h:15:
In file included from ./src/include/hash.h:25:
./src/include/hash_stream.h:15:11: error: call to function 'operator>>' that is neither visible in the template definition
      nor found by argument-dependent lookup
      iss >> key;
          ^
./src/include/learning/perceptron/hashmap_score_packed.h:282:7: note: in instantiation of function template specialization
      'operator>><std::__1::pair<unsigned long, unsigned long>, CPackedScore<double, 2048> >' requested here
   is >> static_cast< CHashMap< K, CPackedScore<SCORE_TYPE, PACKED_SIZE> > &>(score_map) ;
      ^
./src/common/conparser/implementations/muhua/weight.cpp:48:27: note: in instantiation of function template specialization
      'operator>><std::__1::pair<unsigned long, unsigned long>, double, 2048>' requested here
   iterate_templates(file >>,;);
                          ^
./src/common/conparser/implementations/muhua/weight.h:133:4: note: expanded from macro 'iterate_templates'
   left(m_mapS0cmN0tm)right\
   ^
./src/include/pair_stream.h:16:16: note: 'operator>>' should be declared prior to the call site
std::istream & operator >> (std::istream &is, std::pair<T1, T2> &p) {
               ^
1 error generated.
make: *** [obj/english.conparser/weight.o] Error 1

我尝试在pair_stream.h之前加入hash.h,但这似乎不起作用,我又难过了。任何帮助将非常感激。

编辑2 :我实际上已经考虑了事情并再次尝试了这一点,现在一切正常。感谢大家! StackOverflow真棒:)

3 个答案:

答案 0 :(得分:5)

如错误消息所述,std::istream & operator >> (std::istream &is, english::CTag &tag)中定义了src/english/tags.h。 但由于调用它的src/include/hash_stream.h中的模板是在包含该文件之前声明的,因此会发生错误。

文档已经提供了解决方案:

  

确保在可能调用它的模板之前声明要调用的函数。如果它的参数类型都不包含类,则这是唯一的选项。您可以通过移动模板定义,或移动函数定义,或在模板之前添加函数的前向声明来完成此操作。

因此要么确保此文件包含在之前包括src/include/hash_stream.h,要么应用文档提供的任何解决方案。

答案 1 :(得分:1)

operator>>是为istringstream定义的。在定义#include <sstream>的任何地方之前,尝试在您的来源中加入namespace english文件。

答案 2 :(得分:0)

在某些情况下,如果您不能仅仅改变声明的顺序来避免问题 - 您可以执行前向声明