C++ 预处理器意外编译错误

3
请查看以下文件:(这是一个完整的文件)
#ifndef TEES_ALGORITHM_LIBRARY_WRAPPER_H
#define TEES_ALGORITHM_LIBRARY_WRAPPER_H

#ifdef _TEES_COMPILE_AS_LIB
#include <dfa\Includes\DFC_algorithms.hpp>
#include <DFA\FuzzyClassifier\FuzzyAlgorithmIntialization\InitFuzzyAlgorithm.hpp>
typedef teesalgorithm::tees_fuzzy_algorithms algorithms_switchyard_class;
#else
#include <DFA\Includes\commercial_algorithms.hpp>
//An incomplete class to hide implementation
class algorithms_switchyard_class;
#endif

class AlgorithmLibraryWrapper {
algorithms_switchyard_class * algorithmPtr_;

typedef teesalgorithm::tees_paramObj paramObj_type;
typedef teesalgorithm::tees_errorObj errorObj_type;  
typedef teesalgorithm::tees_statusObj statusObj_type;
typedef teesalgorithm::tees_dataObj dataObj_type;
typedef teesalgorithm::tees_outputObj outputObj_type;

public:

AlgorithmLibraryWrapper(const std::string& sAlgName, paramObj_type& paramObj,     errorObj_type& errObj, statusObj_type& statusObj, const char* sFilePath);
static bool dataReader(const std::string& sFileName, dataObj_type& dataObj,     errorObj_type& errObj, statusObj_type& statusObj);
bool runalgorithm(const dataObj_type& dataObj, outputObj_type& outObj, errorObj_type& errObj, statusObj_type& statusObj); 
~AlgorithmLibraryWrapper();

};


#ifdef _TEES_USE_COMPILED_ALGORITHM_LIB
#   ifdef _MSC_VER
    #if _MSC_VER < 1400  // If VC 2003
        #ifdef _DEBUG
            #error No AlgorithmLibWrapper libraries compiled for this version of VC
        #else
            #error No AlgorithmLibWrapper libraries compiled for this version of VC
        #endif
    #elif defined(UNDER_CE)  // Win CE
        #ifdef _DEBUG
            #pragma comment( lib, "AlgorithmLibWrapperCEd" )
        #else
            #pragma comment( lib, "AlgorithmLibWrapperCE" )
        #endif
    #else  // If VC 2005
        #ifdef _DEBUG
            #pragma comment( lib, "AlgorithmLibWrapperd" )
        #else
            #pragma comment( lib, "AlgorithmLibWrapper" )
        #endif
    #endif
#endif 
#endif


#endif //TEES_ALGORITHM_LIBRARY_WRAPPER_H

我遇到了以下错误,但我不知道为什么。我已经手动计算了预处理器指令。

AlgorithmLibraryWrapper.hpp:10:1: 未终止的 #ifdef
AlgorithmLibraryWrapper.hpp:7:1: 未终止的 #ifndef

我正在使用较差的vxWorks gcc编译器。请告诉我是我的问题还是编译器的问题。
6个回答

4

可能问题出在包含的文件中(如果确实存在不平衡的#if/#endif)。

我建议尝试使用另一个编译器进行预处理。您可以使用gcc进行预处理,无论它是否能够编译都没有关系。只需获取gcc(如果您使用的是Windows,则为MinGW)并运行以下命令:

cpp -Iinclude_direcories your_file

或者,如果你不喜欢gcc,可以使用MSVC Express版。同样,你可以预处理甚至无法编译的代码,所以不存在非工作库的问题。
大多数编译器都有一个选项,可以给你从预处理器中得到的输出,这样你就可以检查它正在做什么了。例如,
gcc -E file.c >file.preproc

它将为您提供预处理源代码,以便您可以检查#if与#endif的平衡。


我不认为有过多的endif。它们在我的看来是匹配的。大块的缩进不是我喜欢的,但仍然匹配。如果你指的是最后一个endif,那就是在顶部开始的包含保护。 - paxdiablo
不,我指的不是最后一个,而是前面的那个。不过,你说得对,它是正确的。 - jpalecek
@jpalecek,“gcc -E”将生成预处理器输出到标准输出,您可以检查其平衡性。我已经添加了这个,希望您不介意。 - paxdiablo

3
猜测您正在#include此文件中的其中一个文件具有不匹配的#ifdef / #endif对。 您需要查看所有文件(与预处理器一样),而不仅仅是这个文件。

确实如此。在此运行cpp会生成一个干净的文件(当然不包括头文件)。 - greyfade
抱歉,zabzonk,我不得不取消我的点赞,因为你删除了有用的补充说明。我认为提到如何做,而不仅仅是可以做,很重要。但这是你的答案,不是我的。 - paxdiablo

1

正如其他人所指出的那样,这很可能是由于不匹配的包含保护导致的。

如果您要包含的文件在您的控制范围内(即不属于第三方闭源库),则可以考虑将#ifndef等保护(用于防止多次包含)替换为#pragma once。这将消除具有不匹配的预处理器指令的可能性。

其中一个注意事项是pragma once是非标准的,因此只有在编译器支持它时才能使用。


0

我已经尝试使用VS 6.0编译您的源代码,但没有遇到您所提到的错误。正如其他人所说,可能是由于包含的头文件导致的错误。为了让您的代码编译成功,我需要注释掉上述头文件。

请检查一下上述头文件。


0

我会通过逐个注释部分并尝试确定哪个部分导致错误来调试此问题。

可能是您的编译器不喜欢嵌套的 #ifdefs 或无法正确解释语法。也许它不喜欢 #pragmas。


0
这可能有点冒险,但在您的源文件中,您有以下行:
#   ifdef _MSC_VER

在 '#' 字符和指令名称 (ifdef) 之间有空格时,这在 C / C++ 中是有效的。但是,它并不常见,所以如果偶尔出现编译器崩溃,我也不会感到非常惊讶。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接