paw*_*112 4 c++ dllimport dllexport msvc12
我在 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)。如何解决?
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__
Run Code Online (Sandbox Code Playgroud)
第二个文件:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我试图修复它,但我没有更多的想法。当我将第二个文件中的 prim 函数从定义更改为声明时,dll 编译没有错误,但没有负责算法实现的代码。
感谢所有回复。
编辑:
我将临时 #define BUILD_DLL 添加到 main.h 和稍后在 Cmake 中,我工作。感谢您的回复。
main.h并将main.cpp在您正在创建的 DLL 项目中使用。
只会main.h在访问您创建的 DLL 的客户端可执行文件/DLL 中使用。
因此,main.hDLL 项目需要__declspec(dllexport). 这样就可以从DLL中导出函数。因此,定义BUILD_DLL在DLL Project's Properties -> C/C++ -> 'Preprocessor definitions'
main.h客户端可执行文件需要__declspec(dllimport). 这样就可以从DLL中导入函数。所以不需要在BUILD_DLL中定义Executable Project's Properties -> C/C++ -> 'Preprocessor definitions'