2016-06-07 619 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。该错误表示它在定义中不被允许。由于它有一个机构{...},这是一个定义。

相关问题