【问题标题】:VC++ Compile error on DLL projectDLL项目上的VC ++编译错误
【发布时间】:2012-07-09 12:47:38
【问题描述】:

这是我的代码:

#include "stdafx.h"
#include <Windows.h>

extern "C" int __stdcall myfunction ();

BOOL WINAPI DllMain ( HINSTANCE hin, DWORD reason, LPVOID lpvReserved );

int __stdcall myfunction ()
{
      MessageBoxW(NULL,L"Question",L"Title",MB_OK);
      return 0;
}

BOOL WINAPI DllMain ( HINSTANCE hin, DWORD reason, LPVOID lpvReserved )
{
    return TRUE;
}

当我编译时显示这些错误:

错误 LNK2028:引用 simbol (令牌)未解析(0A000027)“extern“C”int stdcall MessageBoxW(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) 在函数中 "extern "C" int __stdcall myfunction(void)" (?myfunction@@$$J10YGHXZ)

错误 LNK2019:外部符号 "extern "C" int stdcall MessageBoxW(struct HWND *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) 未解决用于 函数 "extern "C" int __stdcall myfunction(void)" (?myfunction@@$$J10YGHXZ)

我不明白错误在哪里及其原因。 如果有人可以帮我解决它,我会非常感谢:)

【问题讨论】:

    标签: c++ dll compiler-errors


    【解决方案1】:

    extern "C" 需要在函数上,而不是定义上:

    int __stdcall myfunction ();
    
    extern "C" int __stdcall myfunction ()
    {
          MessageBoxW(NULL, L"Question", L"Title", MB_OK);
          return 0;
    }
    

    但是,您可以将其包装在预解析器条件中,而不是附加 extern "C"

    int __stdcall myfunction ();
    
    #ifdef __cplusplus
        extern "C" {
    #endif
    
    int __stdcall myfunction ()
    {
          MessageBoxW(NULL, L"Question", L"Title", MB_OK);
          return 0;
    }
    
    #ifdef __cplusplus
        }
    #endif
    

    【讨论】:

    • 此代码返回error C2732: linkage specification contradicts earlier specification for 'myfunction'
    【解决方案2】:

    谢谢大家,但问题出在 user32.lib 上。

    #include "stdafx.h"
    #include <Windows.h>
    
    #pragma comment(lib,"user32.lib"); //Missing lib (No compile errors)
    
    BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
        return  TRUE;
    }
    
    extern "C" __declspec(dllexport) void __stdcall SomeFunction() {
        MessageBoxA(NULL, "Hello", "HELLO", 0x000000L); //0x000000L = MB_OK
    }
    

    我希望这对像我这样的菜鸟有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 2017-12-21
      相关资源
      最近更新 更多