首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DEV-C++中的编译器错误

DEV-C++中的编译器错误
EN

Stack Overflow用户
提问于 2012-07-25 19:01:08
回答 1查看 3.6K关注 0票数 0

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

下面是我的示例代码,取自MSDN站点:

代码语言:javascript
复制
#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"));
}

大量错误中的一个错误样本。

代码语言:javascript
复制
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文件的一部分:

代码语言:javascript
复制
//
// 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-26 01:30:34

__nullterminated,__out_opt,__in,__in_opt和其他类似的词都是微软特有的关键词。使用gcc时,您可以将它们定义为空宏,如下所示

代码语言:javascript
复制
#define __in
#define __deref_out_range(A,B)
... and so on

在includes之前插入这些定义。

或者正如评论所建议的那样,避免使用<strsafe.h>

编辑:

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

代码语言:javascript
复制
gcc -c Test.cpp -o Test.exe -lstdc++
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11648246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档