如何在stdlib.h中启用__BEGIN_NAMESPACE_STD [英] how to enable __BEGIN_NAMESPACE_STD in stdlib.h

查看:231
本文介绍了如何在stdlib.h中启用__BEGIN_NAMESPACE_STD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在尝试使用cmake在linux中构建c ++库。如果我不启用-std = c ++ 0x选项,则总是会收到编译错误错误:对于以下代码,未在此范围内声明'div_t'

I am now trying to build a c++ library in linux with cmake. If I do not enable -std=c++0x option, I always get compilation errors error: 'div_t' was not declared in this scope for the following codes:

        int xPos;
        div_t divResult;
        divResult = div(xPos,8);

然后,如果我使用cmake启用-std-c ++ 0x选项: set(CMAKE_CXX_FLAGS $ {CMAKE_CXX_FLAGS} -std = c ++ 0x ,那么一切都很好。但是,在我的库中,我没有使用任何c ++ 0x功能,因此我不愿意设置std = c ++ 0x选项,因此我搜索了定义div_t的头文件,并在以下宏中找到了它在stdlib.h中定义的:

Then if I enable -std-c++0x options with cmake: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x", then everything is fine. However, in my library I did not use any c++0x features, so I am reluctant to set std=c++0x option. So I search the head file that defines div_t and find it is defined in stdlib.h within the following MACRO:

__BEGIN_NAMESPACE_STD


typedef struct
{ 
  int quot;
  int rem;
} div_t;

....
....
__END_NAMESPACE_STD

在我看来,如果可以启用这些宏,则可以在不启用c ++ 0x功能的情况下构建库。所以我的问题是在这种情况下我可以做什么。

It seems to me that if I can enable these macros I can build the library without enabling c++0x feature. So my question is what I can do in this situation.

顺便说一句,如果在Linux机器上只安装了g ++ 4.4,我可以很好地构建该库而无需启用c ++ 0x功能。当我还安装g ++ 4.6并将g ++ 4.6设置为默认g ++时,则编译错误开始发生。即使我将默认g ++更改为g ++ 4.4,如果不启用c ++ 0x功能,编译错误仍然存​​在。

By the way, I can build the library very well without enabling c++0x feature if only g++4.4 is installed in the linux machine. When I also install g++4.6 and make g++4.6 the default g++, then the compilation error began to occur. Even I changed the default g++ to g++4.4, the compilation error still exists if I do not enable c++0x feature.

推荐答案

宏扩展为命名空间std {} 如果代码是通过C ++标准库标头插入的。这使我相信您不是直接包含stdlib.h(这很好!)。

The macros expand to namespace std { and } respectively if the code is pulled in through a C++ standard library header. This leads me to believe that you're not #including stdlib.h directly (which is good!).

早期版本的libstdc ++将符号从C传统头文件中提取到即使这些标头的C ++版本(例如< cstdlib> 而不是< stdlib.h> )被使用;较新的将它们仅放置在命名空间std中。

Earlier versions of libstdc++ pulled symbols from C legacy headers into the global namespace even if the C++ versions of these headers (e.g. <cstdlib> instead of <stdlib.h>) were used; newer ones place them only in namespace std.

最简单的解决方法是

#include <cstdlib>

在出现问题的所有翻译单元中,并使用 std :: div 而不是 div 。如果您很懒,也可以

in all translation units where the problem occurs and to use std::div instead of div. If you're lazy, you can also

#include <stdlib.h>

在所有使用 div 的翻译单位中,但是混合使用C和C ++总是很麻烦。不过,在这种特殊情况下,并不是太累了。

in all translation units that use div, but mixing C and C++ is always icky. Not terribad in this particular case, though.

这篇关于如何在stdlib.h中启用__BEGIN_NAMESPACE_STD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆