来自 yaml-cpp 的编译器错误

Posted

技术标签:

【中文标题】来自 yaml-cpp 的编译器错误【英文标题】:Compiler error coming out of yaml-cpp 【发布时间】:2010-06-27 19:03:17 【问题描述】:

我正在尝试将 yaml-cpp 集成到项目中,但我发现 GCC 出现了一些意外错误。例如:

g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/commands-tz.cpp -o obj/commands-tz.o
In file included from /usr/local/include/yaml-cpp/conversion.h:9,
                 from /usr/local/include/yaml-cpp/node.h:8,
                 from /usr/local/include/yaml-cpp/parser.h:8,
                 from /usr/local/include/yaml-cpp/yaml.h:8,
                 from src/note.h:26,
                 from src/commands-tz.cpp:297:
/usr/local/include/yaml-cpp/traits.h:26: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:26: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:26: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:26: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:31: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:31: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:31: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:31: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:34: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:34: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:34: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:34: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:37: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:37: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:37: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:37: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:42: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:42: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:42: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:42: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/traits.h:45: error: expected identifier before ‘(’ token
/usr/local/include/yaml-cpp/traits.h:45: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:45: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/traits.h:45: error: expected unqualified-id before ‘)’ token

另一个例子:

In file included from /usr/local/include/yaml-cpp/nodeimpl.h:8,
                 from /usr/local/include/yaml-cpp/node.h:139,
                 from /usr/local/include/yaml-cpp/parser.h:8,
                 from /usr/local/include/yaml-cpp/yaml.h:8,
                 from src/note.h:26,
                 from src/commands-tz.cpp:297:
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:9: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:14: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected nested-name-specifier before ‘(’ token
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected ‘)’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected ‘>’ before numeric constant
/usr/local/include/yaml-cpp/nodeutil.h:19: error: expected unqualified-id before ‘)’ token
/usr/local/include/yaml-cpp/nodeutil.h:24: error: ‘is_index_type_with_check’ is not a template
/usr/local/include/yaml-cpp/nodeutil.h:24: error: explicit specialization of non-template ‘YAML::is_index_type_with_check’

我的平台是 Fedora(2.6.32 内核)、GCC 4.4.1 和 yaml-cpp 0.2.5。还有很多很多其他的错误。肉眼看来,这似乎是 yaml-cpp 中的一个问题,但经验告诉我,我可能是错误的人。有什么想法吗?

更新

文件 traits.h 包含以下内容:

namespace YAML

        template <typename>
        struct is_numeric  enum  value = false ; ;

        template <> struct is_numeric <char>  enum  value = true ; ;
        template <> struct is_numeric <unsigned char>  enum  value = true ; ;
        template <> struct is_numeric <int>  enum  value = true ; ;
        template <> struct is_numeric <unsigned int>  enum  value = true ; ;
        template <> struct is_numeric <long int>  enum  value = true ; ;
        template <> struct is_numeric <unsigned long int>  enum  value = true ; ;
        template <> struct is_numeric <short int>  enum  value = true ; ;
        template <> struct is_numeric <unsigned short int>  enum  value = true ; ;
        template <> struct is_numeric <long long>  enum  value = true ; ;
        template <> struct is_numeric <unsigned long long>  enum  value = true ; ;
        template <> struct is_numeric <float>  enum  value = true ; ;
        template <> struct is_numeric <double>  enum  value = true ; ;
        template <> struct is_numeric <long double>  enum  value = true ; ;

        template <bool, class T = void>
        struct enable_if_c 
          typedef T type;
        ;

        template <class T>
        struct enable_if_c<false, T> ;

        template <class Cond, class T = void>
        struct enable_if : public enable_if_c<Cond::value, T> ;

        template <bool, class T = void>
        struct disable_if_c 
          typedef T type;
        ;

        template <class T>
        struct disable_if_c<true, T> ;

        template <class Cond, class T = void>
        struct disable_if : public disable_if_c<Cond::value, T> ;

并且 nodeutil.h 包含:

namespace YAML

        template <typename T, typename U>
        struct is_same_type 
                enum  value = false ;
        ;

        template <typename T>
        struct is_same_type<T, T> 
                enum  value = true ;
        ;

        template <typename T, bool check>
        struct is_index_type_with_check 
                enum  value = false ;
        ;

        template <> struct is_index_type_with_check<std::size_t, false>  enum  value = true ; ;

#define MAKE_INDEX_TYPE(Type) \
        template <> struct is_index_type_with_check<Type, is_same_type<Type, std::size_t>::value>  enum  value = true ; 

        MAKE_INDEX_TYPE(int);
        MAKE_INDEX_TYPE(unsigned);
        MAKE_INDEX_TYPE(short);
        MAKE_INDEX_TYPE(unsigned short);
        MAKE_INDEX_TYPE(long);
        MAKE_INDEX_TYPE(unsigned long);

#undef MAKE_INDEX_TYPE

        template <typename T>
        struct is_index_type: public is_index_type_with_check<T, false> ;

        // messing around with template stuff to get the right overload for operator [] for a sequence
        template <typename T, bool b>
        struct _FindFromNodeAtIndex 
                const Node *pRet;
                _FindFromNodeAtIndex(const Node&, const T&): pRet(0) 
        ;

        template <typename T>
        struct _FindFromNodeAtIndex<T, true> 
                const Node *pRet;
                _FindFromNodeAtIndex(const Node& node, const T& key): pRet(node.FindAtIndex(static_cast<std::size_t>(key))) 
        ;

        template <typename T>
        inline const Node *FindFromNodeAtIndex(const Node& node, const T& key) 
                return _FindFromNodeAtIndex<T, is_index_type<T>::value>(node, key).pRet;
        

【问题讨论】:

导致错误的行包含哪些代码? (大约在 traits.h 中的第 26 行和 nodeutil.h 中的第 9 行) 你好像在地狱里。您发布的 traits.h 文件包含 no '(',但 gcc 抱怨 '(' 之前的内容。这可能意味着您包含的东西具有与某物同名的宏在这个文件中。尝试重新排序项目中的包含,看看是否有任何不同。 @Gianni:这包括地狱好吧。我不确定这是如何工作的,但文件“note.h”包含 。在一个实现文件 [src/commands-tz.cpp] 我 #include "note.h" 并且 GCC 死了。如果我添加(before that include)#include ,那么一切正常。看不懂,谢谢指教! (重新发布作为答案,以便我可以标记它?) 【参考方案1】:

你似乎在地狱里。您发布的 traits.h 文件不包含“(”,但 gcc 抱怨“(”之前的内容。这可能意味着您包含的内容与此文件中的内容具有相同名称的宏。尝试重新排序包含在您的项目中,看看它是否有任何不同。

【讨论】:

【参考方案2】:

我建议您尝试删除“使用命名空间 X;” - 特别是在 X == YAML 的情况下。这样,您的错误消息可能会变得更有洞察力 - 因为看起来您遇到了名称冲突。另一个技巧是查看预处理器输出的内容 - 使用 G++,您可以使用 -E(而不是 -c)运行编译行并将输出通过管道传输到文件。然后你可以看到编译器得到了什么(而不是原始源代码,或者预处理器得到了什么)。

【讨论】:

以上是关于来自 yaml-cpp 的编译器错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试构建库时出现 yaml-cpp 编译器错误

RAD Studio 2010 中的错误 yaml-cpp 编译

yaml-cpp 编译错误:“未定义对 YAML::LoadFile 的引用”

未定义的参考 yaml-cpp

使用 yaml-cpp 0.5 DLL 时的链接器错误

ROS交叉编译——protobuf/yaml-cpp/opencv