2012-01-24 40 views
3

GNU GCC正在修改我導入的函數名稱,儘管我在聲明中使用了extern「C」。如何阻止GNU GCC從mangling dll導入函數名稱

我剛剛開始使用Code :: Blocks和GNU GCC將現有項目從Borland C++ Builder 6.0 Pro遷移出來,並開發了一些未來的項目。長期來看,我會1)製作我想要交付的DLL,以便終止使用各種平臺的開發人員(即,.dlll不會嚴格保留在內部),並且2) Code ::使用這些.dlls的塊。

當我嘗試遷移在Borland C++ Builder下工作的第一個項目時,即使我將它們聲明爲'extern「C」,它也會損壞導入的函數名稱。

#include <windows.h> 

    #include <stdio.h> 

    #include <cstdlib> 

    #include "dsound.h" 

    //#include "CGG_Cpp_DInterface.h" 
    //The following are relevent items taken from CGG_Cpp_DInterface.h 
    struct CrimsonReply1{ 
    unsigned int Echo; 
    unsigned int Result; 
    unsigned int ReplyType; 
    unsigned int Reserved1; 
    unsigned int Reserved2; 
    char* OutputString; 
    double Reply[32]; 
    }; 

    extern "C" __declspec(dllimport) int CrimsonCommandProc(/*I'm not going to list all the arguments here*/); 
    extern "C" __declspec(dllimport) int TranslateCrimsonReply1(int, CrimsonReply1*, int); 
    #define CGG_SETUP    0x20000001 
    #define CGG_SHUTDOWN   0x20000005 
    #define CGG_WINDOWED   0x0000002F 
    #define CGG_RECTANGLE   0x00000024 
    #define CGG_STRETCHTOCLIENT  0x00000028 
    #define CGG_DUMPALLREPLIES  0 
    //End of items taken from CGG_Cpp_DInterface.h 
    extern "C" LRESULT CALLBACK WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam); 

    char szProgName[] = "Age Games: Animal Reader"; 
    char message[] = ""; 
    extern "C"{ 

    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow) 
    { //Win32 entry-point routine 
    MSG WinEvent;//long pointer to an event message sent by Windows to the application 
    WNDCLASSEX WinClass;//A Windows Class Struct 
    RECT WindowRect;//A structure to describe the position and size of the window. 
    HWND WinHand; 

    /*Other Variables-------------------------------------------------------------*/ 
    CrimsonReply1 CrimsonReply; 


    /*------------------------------------------------------------------------------ 
    Set up a window, register it, and create it. 
    ------------------------------------------------------------------------------*/ 

    /*set up window class and register it*/ 
    /*All the standard window initialization is in here. It's not relevent to the problem.*/ 
    /*------------------------------------------------------------------------------ 
    begin the message loop 
    ------------------------------------------------------------------------------*/ 

    LPDIRECTSOUND* DirectSoundObject; 
    HRESULT Result = DirectSoundCreate(NULL, DirectSoundObject, NULL); 
    if (Result == DS_OK && *DirectSoundObject) Result = (*DirectSoundObject)->SetCooperativeLevel(WinHand, DSSCL_NORMAL); 
    if (Result != DS_OK) return(0); 
    int ReplyPointer = CrimsonCommandProc(CGG_SETUP,NULL,(double)(int)WinHand,NULL,CGG_WINDOWED,NULL, 
           CGG_RECTANGLE,NULL,800,NULL,600,NULL, 
           CGG_STRETCHTOCLIENT); 
    if (ReplyPointer) ReplyPointer = TranslateCrimsonReply1(ReplyPointer, &CrimsonReply, CGG_DUMPALLREPLIES); 

    while(GetMessage(&WinEvent, NULL, 0, 0)) 
    { 
     DispatchMessage(&WinEvent); 
    } 
    /*------------------------------------------------------------------------------ 
    Shutdown. 
    ------------------------------------------------------------------------------*/ 

    ReplyPointer = CrimsonCommandProc(CGG_SHUTDOWN); 
    if (ReplyPointer) ReplyPointer = TranslateCrimsonReply1(ReplyPointer, &CrimsonReply, CGG_DUMPALLREPLIES); 

    return(WinEvent.wParam); 
    } //end of WinMain() 

    } 

此代碼產生以下錯誤::

C:\Development\AnimalReader\Animal Reader.cpp|91|undefined reference to `[email protected]'| 
    C:\Development\AnimalReader\Animal Reader.cpp|96|undefined reference to `_imp__CrimsonCommandProc'| 
    C:\Development\AnimalReader\Animal Reader.cpp|97|undefined reference to `_imp__TranslateCrimsonReply1'| 
    C:\Development\AnimalReader\Animal Reader.cpp|107|undefined reference to `_imp__CrimsonCommandProc'| 
    C:\Development\AnimalReader\Animal Reader.cpp|108|undefined reference to `_imp__TranslateCrimsonReply1'| 

因此,它的重整的DirectSoundCreate函數名

例如,編譯器開始連接後的以下的(降低的)碼產生的錯誤與@ 12,即使我使用微軟的dsound.h文件(它把它放在一個'extern「C''塊),它是搗毀CrimsonCommandProc與_ imp __即使我將所有內容都放在「extern」C「範圍內」。我如何獲得GNU GCC編譯器來停止修改名稱? (無論是進口dll函數,並最終創建dll時)

或者,我沒有結婚到GNU GCC。如果還有另一個編譯器是A)Free,B)跨平臺,並且C)與DirectX .lib文件一起使用(最好是在新版本出現時支持的東西),我可以使用它。

回答

3

DirectSoundCreate被聲明爲WINAPI,爲__stdcall宏:

extern HRESULT WINAPI DirectSoundCreate(...); 

這使得它在@ 12裝飾。您可能正在獲取鏈接器錯誤,因爲您沒有鏈接dsound.lib。或者你的任何版本都缺少導出,這在SDK文件的mingw版本中並不罕見。

您正在獲取__imp前綴,因爲您聲明瞭函數__declspec(dllimport)。只要刪除它。

+0

dsound.h幾乎包住該文件的所有內部 的#ifdef __cplusplus的extern 「C」 { #ENDIF // __cplusplus 的#ifdef __cplusplus }; #endif // __cplusplus 此外,我還將dsound.lib添加到項目中。我應該用它做點什麼嗎? (BCB足夠了,我是新來的code :: blocks) – user1167758

+0

我去了Build Options ...-> Linker Settings-> Add - >(Browse)並添加了dsound.lib和CGGWin.lib。現在我得到「ld.exe ||找不到-ldsound.lib |」和CGGWin一樣。 – user1167758

+0

啊。當你在GNU GCC中使用__stdcall時,即使你指定了'extern'C'',它也會添加@N。這在BCB中不會發生。此外,dsound.h文件使用#define WINAPI __stdcall隱藏了__stdcall。現在我只需要弄清楚爲什麼Code :: Blocks在瀏覽並添加library.lib時試圖找到-llibrary.lib。 – user1167758