2016-06-07 563 views
0

我在Visual Studio 2013上創建DLL時出現問題。此代碼可用於Code :: Blocks。錯誤是definition of dllimport function not allowed" on line void DLL_EXPORT prim(map<string, vector<int>> nodes, map<pair<string, string>, pair<int, string>> edges)。如何解決它?錯誤C2491:不允許使用dllimport函數的定義

main.h: 
#ifndef __MAIN_H__ 
#define __MAIN_H__ 

#include <windows.h> 
#include <iostream> 
#include <vector> 
#include <map> 

using namespace std; 

#ifdef BUILD_DLL 
    #define DLL_EXPORT __declspec(dllexport) 
#else 
    #define DLL_EXPORT __declspec(dllimport) 
#endif 


#ifdef __cplusplus 
extern "C" 
{ 
#endif 

void DLL_EXPORT prim(map<string,vector<int>> nodes, map<pair<string,string>,pair<int,string>> edges); 

#ifdef __cplusplus 
} 
#endif 

#endif // __MAIN_H__ 

第二個文件:

main.cpp: 
#include "main.h" 
//some other includes 

// a sample exported function 

extern "C" 
{ 
    void DLL_EXPORT prim(map<string, vector<int>> nodes, map<pair<string, string>, pair<int, string>> edges) 
    { 
     //some code 
    } 
} 

我試圖修復它,但我沒有更多的想法。當我將第二個文件中的prim函數從定義更改爲聲明時,dll編譯時沒有錯誤,但沒有代碼負責執行算法。

感謝您的回覆。

編輯:

我添加臨時的#define BUILD_DLL到main.h後來在CMake和我的作品。感謝您的回覆。

回答

2

main.hmain.cpp將用於您正在創建的DLL項目。

只有main.h將用於客戶端可執行程序/ DLL正在訪問您創建的DLL。

因此,DLL項目的main.h需要__declspec(dllexport)。這樣可以從DLL中導出函數。因此,定義BUILD_DLLDLL Project's Properties -> C/C++ -> 'Preprocessor definitions'

客戶main.h可執行文件要求__declspec(dllimport)。這樣可以從DLL中導入函數。所以無需定義BUILD_DLLExecutable Project's Properties -> C/C++ -> 'Preprocessor definitions'

0

您應該只是定義BUILD_DLL是您的一些標題,或者在項目屬性 - > C/C++ - >'預處理器定義'中。 所以DLL_EXPORT將是__declspec(dllexport)這就是你想要什麼時,你建立你的DLL。如果要從其他dll導入功能,則需要使用__declspec(dllimport)。這個錯誤意味着你不能重新定義導入的函數,因爲它在你導入它的dll中定義。

0

我想你只需要在main.cpp中刪除DLL_EXPORT。該錯誤表示它在定義中不被允許。由於它有一個機構{...},這是一個定義。

相關問題