检查C ++头文件上的操作系统时出错

时间:2013-04-15 12:17:12

标签: c++ gcc header hashmap c-preprocessor

我收到了一些C ++代码,我试图使用提供的makefile在Linux 64位下编译。

在该代码中有一个string_hash.h文件,其中包含代码:

#ifndef STRING_HASH
#define STRING_HASH

#include <string>
#include "universal_hash_map.h"

#ifdef WINDOWS
typedef stdext::hash_compare<std::string> String_Hasher;
typedef stdext::hash_map< std::string, std::string, stdext::hash_compare<std::string> > String_Hash;
#else
#ifdef LINUX
class String_Hasher: public stdext::hash< std::string > {
private:
    stdext::hash<const char*> hasher;
public:
    String_Hasher() {}
    size_t operator() (const std::string& p) const {
        return hasher(p.c_str());
    }
};
typedef stdext::hash_map< std::string, std::string, String_Hasher > String_Hash;
#endif
#endif

#endif

使用universal_hash_map.h:

#include "define_os.h"

#if defined(__MINGW32__) || defined(LINUX)
#include<ext/hash_map>
#define stdext __gnu_cxx;
#else
#if defined(_MSC_VER)
#include<hash_map>
#endif
#endif

和define_os.h:

#if defined(WINDOWS) || defined(LINUX)
#else

#if defined(_MSC_VER) || defined(WIN32)  || defined(_WIN32) || defined(__WIN32__) \
|| defined(WIN64)     || defined(_WIN64) || defined(__WIN64__)
#define WINDOWS
#elif defined(sun)       || defined(__sun)      || defined(linux)       || defined(__linux) \
 || defined(__linux__)   || defined(__CYGWIN__) || defined(BSD)         || defined(__FreeBSD__) \
 || defined(__OPENBSD__) || defined(__MACOSX__) || defined(__APPLE__)   || defined(sgi) \
 || defined(__sgi)
#define LINUX
#endif

#endif

如果我这样编译项目我得到错误告诉我编译找不到stdext :: hash

Compiling string_functions.o from /home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_functions.cpp.
In file included from /home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_functions.h:5:0,
                 from /home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_functions.cpp:1:
/home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_hash.h:12:29: error: expected class-name before ‘;’ token
/home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_hash.h:12:29: error: expected ‘{’ before ‘;’ token
/home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_hash.h:12:35: error: ‘hash’ in namespace ‘::’ does not name a type
/home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_hash.h:21:9: error: ‘__gnu_cxx’ does not name a type
/home/adria/Code/JensAdria/trunk/thirdparty/pechin/Cpp_Libraries/mylibrary/codes/string_hash.h:21:15: error: ‘hash_map’ in namespace ‘::’ does not name a type
make: *** [string_functions.o] Error 1

但令我感到困惑的是,如果我在universal_hash_map.h中注释第一行所以它不包含define_os.h,那么代码编译得很好!

所以将universal_hash_map.h更改为:

//#include "define_os.h"

#if defined(__MINGW32__) || defined(LINUX)
#include<ext/hash_map>
#define stdext __gnu_cxx;
#else
#if defined(_MSC_VER)
#include<hash_map>
#endif
#endif

或只是

//#include "define_os.h"

//#if defined(__MINGW32__) || defined(LINUX)
#include<ext/hash_map>
#define stdext __gnu_cxx;
//#else
//#if defined(_MSC_VER)
//#include<hash_map>
//#endif
//#endif

编译好。

但是在最后一个例子中,如果我取消注释* #include“define_os.h”*它不能编译:s

所以问题是,为什么尝试定义操作系统会使代码无法编译?

另外,检查gcc预定义符号似乎没有任何错误:

$ gcc -E -dM - </dev/null | grep linux
#define __linux 1
#define __linux__ 1
#define __gnu_linux__ 1
#define linux 1

编辑:有关命名空间的更多信息

在/usr/include/c++/4.7/ext/hash_map(没有文件扩展名)我有:

#ifndef _BACKWARD_HASH_MAP
#define _BACKWARD_HASH_MAP 1

#ifndef _GLIBCXX_PERMIT_BACKWARD_HASH
#include "backward_warning.h"
#endif

#include <bits/c++config.h>
#include <backward/hashtable.h>
#include <bits/concept_check.h>

namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  using std::equal_to;
  using std::allocator;
  using std::pair;
  using std::_Select1st;

  /**
   *  This is an SGI extension.
   *  @ingroup SGIextensions
   *  @doctodo
   */
  template<class _Key, class _Tp, class _HashFn = hash<_Key>,
       class _EqualKey = equal_to<_Key>, class _Alloc = allocator<_Tp> >
    class hash_map
    {
    private:
      typedef hashtable<pair<const _Key, _Tp>,_Key, _HashFn,
            _Select1st<pair<const _Key, _Tp> >,
            _EqualKey, _Alloc> _Ht;

      _Ht _M_ht;

    public:
      typedef typename _Ht::key_type key_type;

我似乎无法找到我正在运行的代码的4.7.3版本,但here是4.7.2,看起来与我完全相同。

编辑: 在string_functions.cpp上运行gcc -E后的文件:

with error (with include)

without Error (without include)

diff

1 个答案:

答案 0 :(得分:1)

问题是您使用的是非标准类型,因此无法保证您的代码具有可移植性。

如果可以(因为您的标准库支持它),您应该切换到std::unordered_map。它主要是非标准stdext::hash_map的标准替代品,并且在没有#ifdef的情况下将独立于平台。

要测试它,您可以执行以下操作:

#define stdext std
#define hash_map unordered_map

如果有效,那么我建议您快速搜索并替换您的文件。

另一个解决方案是,搜索你的hash_map头并找出它所在的命名空间。它似乎不在__gnu_cxx中。它可能在stdext中,可以由预处理器替换为__gnu_cxx。