DLL初学者问一个小问题:不能定义声明“dllimport”的函数

Longmenxieyi 2014-08-22 02:18:05
楼主用的是VS2010

/* 文件名:lib.h */
#ifndef _LIB_H_
#define _LIB_H_ //前面两行指示编译器只包含这个文件一次。

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
else
#define DECLDIR __declspec(dllimport)
#endif

extern "C" //extern "C"告诉编译器该部分可以在C/C++中使用。
{
DECLDIR int add(int x,int y);
DECLDIR int max(int x,int y);
DECLDIR void fun(void);
}

#endif



/* 文件名:lib.cpp */
#include<iostream>
using namespace std;

#include "lib.h"
#define DLL_EXPORT

extern "C"
{
DECLDIR int add(int x,int y)
{
return (x+y);
}

DECLDIR void fun(void)
{
cout<<"Dll called!"<<endl;
}
}
...全文
4451 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Longmenxieyi 2014-08-22
  • 打赏
  • 举报
回复
引用 1 楼 pcradio 的回复:
在lib.cpp中,将#define DLL_EXPORT放在#include "lib.h"前就可以了
此为正解,多谢pcradio!(另外发现了另一个小问题,else前面没有写#)
阿源是少年 2014-08-22
  • 打赏
  • 举报
回复
跟你你头文件的宏定义,DLL_EXPORT这个宏必须先于#if #else才会使得你的DECLDIR为__declspec(dllexport)
阿源是少年 2014-08-22
  • 打赏
  • 举报
回复 1
在lib.cpp中,将#define DLL_EXPORT放在#include "lib.h"前就可以了

15,474

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧