【问题标题】:Compiler Error in DEV-C++DEV-C++ 中的编译器错误
【发布时间】:2012-07-23 19:07:42
【问题描述】:

我编译了以下代码,在我的 C 代码中用 #include <strsafe.h> 声明,我在 DEV-C++ 中收到以下编译器错误。我想编译器中应该有一些选项来解决这个问题。有人可以帮忙解决这个问题吗?

以下是我的示例代码,取自 MSDN 网站:

#include <windows.h>
#include <strsafe.h>

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}

void main()
{
    // Generate an error

    if(!GetProcessId(NULL))
        ErrorExit(TEXT("GetProcessId"));
}

大量错误中的错误示例。

154 26  C:\Documents and Settings\mkumar@hubino\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h   
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'char'
155 26  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
156 1   C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__nullterminated'
156 33  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
157 26  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
158 26  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
164 38  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
165 38  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
166 38  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const'
173 10  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name 'STRSAFE_PCNZCH'
174 10  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__in_range'
175 15  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_out_range'
179 10  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name 'STRSAFE_PCNZWCH'
180 10  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__in_range'
181 15  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_out_range'
193 5   C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_in_opt_out'
194 17  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_out_range'
200 5   C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_in_opt_out'
201 17  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name '__deref_out_range'
207 30  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name 'STRSAFE_PCNZCH'
213 30  C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h  
[Error] unknown type name 'STRSAFE_LPCSTR'
continues.......

我收到上述错误的 strsafe.h 文件的一部分:

//
// These typedefs are used in places where the string is guaranteed to
// be null terminated.
//
typedef __nullterminated char* STRSAFE_LPSTR;
typedef __nullterminated const char* STRSAFE_LPCSTR;
typedef __nullterminated wchar_t* STRSAFE_LPWSTR;
typedef __nullterminated const wchar_t* STRSAFE_LPCWSTR;
typedef __nullterminated const wchar_t UNALIGNED* STRSAFE_LPCUWSTR;

//
// These typedefs are used in places where the string is NOT guaranteed to
// be null terminated.
//
typedef __possibly_notnullterminated const char* STRSAFE_PCNZCH;
typedef __possibly_notnullterminated const wchar_t* STRSAFE_PCNZWCH;
typedef __possibly_notnullterminated const wchar_t UNALIGNED* STRSAFE_PCUNZWCH;


// prototypes for the worker functions

STRSAFEWORKERAPI
StringLengthWorkerA(
    __in STRSAFE_PCNZCH psz,
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax,
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength);

STRSAFEWORKERAPI
StringLengthWorkerW(
    __in STRSAFE_PCNZWCH psz,
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax,
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength);

#ifdef ALIGNMENT_MACHINE
STRSAFEWORKERAPI
UnalignedStringLengthWorkerW(
    __in STRSAFE_PCUNZWCH psz,
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax,
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength);
#endif  // ALIGNMENT_MACHINE

STRSAFEWORKERAPI
StringExValidateSrcA(
    __deref_in_opt_out STRSAFE_LPCSTR* ppszSrc,
    __inout_opt __deref_out_range(<, cchMax) size_t* pcchToRead,
    __in const size_t cchMax,
    __in DWORD dwFlags);

【问题讨论】:

  • 如果您也发布有问题的代码可能会有所帮助。
  • 考虑编写SSCE,并编辑您的问题以包含代码。
  • 唯一的错误是 strsafe.h。
  • @user1317084 你忘记发帖strsafe.h了吗?没有它,您可能不会得到任何答案。
  • 谢谢,我已经从错误开始的地方发布了 strsafe.h 的一部分..

标签: c gcc compiler-errors gcc-warning dev-c++


【解决方案1】:

__nullterminated、__out_opt、__in、__in_opt 和其他此类词是 Microsoft 特定的关键字。当您使用 gcc 时,您可以将它们定义为空宏,例如

#define __in
#define __deref_out_range(A,B)
... and so on

在包含之前插入这些定义。

或者正如 cmets 建议的那样,避免使用 &lt;strsafe.h&gt;

编辑:

MinGW(x64 build from http://equation.com)只需一个简单的命令就可以安静地编译上面的代码(即使使用strsafe.h):

gcc -c Test.cpp -o Test.exe -lstdc++

【讨论】:

  • 你的意思是我要定义所有这些宏?没有其他方法可以避免这种情况,比如使用一些编译器选项?很抱歉在他们说要避免使用 strsafe.h 的 cmets 中问这个问题 .. 请建议..
  • 我只提出了对我有用的解决方案(我使用了带有 MinGW/GCC 的 DirectX 11 标头 - 有很多 __in_opt 之类的东西)。顺便说一句,我用 MinGW 检查了你的代码,它可以安静地编译。
  • 是的,谢谢。如果可能的话,我可以知道用于编译的命令吗?因为我的 devcon 在内部只调用了 Mingw。
  • 添加到我的答案中,我真的不认为这个命令会对你有多大帮助。您的标题设置有些奇怪。看起来您的 MinGW 以某种方式引用了 VC++ 标头(可能来自 Windows SDK?)
  • 是的,我指的是 SDK strsafe.h .. 但是有什么提示可以解决这个问题吗?因为我的计算机上没有 strsafe.h 文件,而不是 SDK。
猜你喜欢
  • 2011-01-10
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2012-05-14
  • 2013-02-21
相关资源
最近更新 更多