在jemalloc.h之前包含std :: mutex时编译错误

时间:2015-02-26 01:04:28

标签: c++ c++11 include std

//CSocket.h
#ifndef __SERVER_CSOCKET_H__
#define __SERVER_CSOCKET_H__

#include "winsock2.h"
#include "ws2tcpip.h"

#include <thread>
#include <stdio.h>
#include <string>

(cpp仅包含标题)

//CSocket.cpp
#include "CSocket.h"

在c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ ratio

中生成以下错误消息
ratio(122): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(133): See reference to the instance of the just compiled class-template "std::ratio<_Nx,_Dx>".
ratio(124): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(44): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(217): See reference to the instance of the just compiled class-template "std::_Safe_mult<0x01,0x01>".
ratio(36): error C2338: integer arithmetic overflow
ratio(44): See reference to the instance of the just compiled class-template "std::_Safe_multX<0x01,0x01,false>".
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x01,false>'
ratio(44): error C2065: 'value': undeclared identifier
ratio(44): error C2057: Expected constant expression
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x0989680,false>'
ratio(219): error C2975: "_Nx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(116): See declaration of '_Nx'
ratio(219): error C2975: "_Dx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(117): See declaration of '_Dx'
CSocket.cpp

在.cpp中包含std :: thread而不是在头文件中解决所有错误,但我不知道为什么它在标题中不起作用。

//CSocket.cpp
#include "CSocket.h"
#include <thread>

我使用的唯一库是jemalloc。 可能错误来自于在互斥体之前包含jemalloc.h而不是来自线程本身吗?

4 个答案:

答案 0 :(得分:1)

我必须在#include <mutex>之前#include "jemalloc.h"而不是之后。 现在工作正常,但奇怪的错误。

答案 1 :(得分:1)

我遇到了同样的错误,但是包含的顺序对我没用。我认为这与其他包含chrono和thread的包含有关,所以你可以查看它。

您使用的是Visual Studio吗?似乎更多人遇到了同样的错误:https://connect.microsoft.com/VisualStudio/feedback/details/800726/compiler-error

答案 2 :(得分:1)

我在VS2013更新3中遇到了同样的错误。问题似乎是INTMAX_MAX未定义,但它在ratio.h中使用。

我的解决方案是添加

#define INTMAX_MAX   INT64_MAX

在您的文件中#include <ratio>之前(如果您没有该行,则可以添加该行)。

要包含的行可以在stdint.h中找到 - 在您的情况下,右侧可以是不同的。

PS另一个解决方案是#include <stdint.h>并定义__STDC_LIMIT_MACROS。在这种情况下,您可能会收到有关重复宏的警告。

答案 3 :(得分:0)

当我使用CxImage作为第三方库并使用C ++ 11中的线程池时,它会出现在项目中。另外,它们都可以,但在同一个项目中合并时,会发生错误。

solution是将_STDC_LIMIT_MACROS的预编译选项添加到 属性页 - &gt;配置属性 - &gt; C / C ++ - &gt;预处理器 - &gt;项目的预处理器定义。

PS:我的环境是:MFC / VS2015&amp;&amp; windows7 64bit

可能对某人有帮助:))