首页 > 技术文章 > dll导入导出宏定义,出现“不允许 dllimport 函数 的定义”的问题分析

xingzhensun 2016-04-25 10:02 原文

建立dll项目后,在头文件中,定义API宏

#ifndef API_S_H
#define API_S_H
......

#ifndef DLL_S_20160424
#define API _declspec(dllimport)
#else
#define API _declspec(dllexport)
#endif

......
int API apiFunction();
#endif

 

在头文件导出函数前添加 API 类型说明

int API apiFunction();

关键的是.cpp中的宏定义

#include "stdafx.h"

#define DLL_S_20160424

#include "api.h"

注意:DLL_S_20160424 的定义一定要在头文件“api.h”之前,否则会出错

error C2491: “apiFunction”: 不允许 dllimport 函数 的定义

推荐阅读