静态链接库 Lnk2001

周晓荣 2011-09-23 08:12:02
VS2008写的静态库,在VS2008下能用调用,但在VC6.0下无法调用

听说静态链接库也得使用__declspec(dllexport)来导出才能适用于不同编译器,但我试了依然不行。


#pragma once
#include <WinSock.h>

namespace MyLibrary
{
#define DllExport __declspec(dllexport)
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*MsgRecvCallBack)(char *p_recv_buf);
//common
DllExport void InitAsyncSocket(int time_out = 5000);
DllExport void UnInitAsyncSocket(void);
DllExport void SetRecvBufSize(int buf_size);
DllExport void SetMsgRecvFunc(SOCKET &recv_socket, MsgRecvCallBack msg_recv_call_back);
DllExport int SendTCPMsg(SOCKET &send_socket, const char *p_send_buf);
DllExport int SendUDPMsg(SOCKET &send_socket, const char *p_ip, unsigned short port, const char *p_send_buf);

//server side
DllExport SOCKET ListenConnect(unsigned short port/*监听端口*/, int protocal, int max_connect/*最大连接数*/ );
//client side
DllExport SOCKET ConnectToAddr(const char *p_ip, unsigned short port, int protocal/*SOCK_STREAM=1(it's tcp), SOCK_DGRAM=2(it's udp)*/);

#ifdef __cplusplus
}
#endif

}
...全文
81 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
另外 :


无论扩展dll ,还是win32 dll,

我都是用 导出符 _declspce(dllexport) ,从来不用 扩展mfc中的导出宏


对于扩展dll ,会不会有什么问题??



[Quote=引用 12 楼 caddor2011 的回复:]

这位 想必是大牛 , 乘着这个机会 ,请教个dll问题

扩展的dll

写了很多扩展dll ,但是没有深究



static AFX_EXTENSION_MODULE AaDLL = { NULL, NULL };

红色的是什么意思?


extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD ……
[/Quote]
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
这位 想必是大牛 , 乘着这个机会 ,请教个dll问题

扩展的dll

写了很多扩展dll ,但是没有深究



static AFX_EXTENSION_MODULE AaDLL = { NULL, NULL };

红色的是什么意思?


extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);

if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("AA.DLL Initializing!\n");

// Extension DLL one-time initialization
if (!AfxInitExtensionModule(AaDLL, hInstance))
return 0;

// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.

new CDynLinkLibrary(AaDLL);
为什么要new个类??


}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("AA.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(AaDLL);
}
return 1; // ok
}




期盼大牛的回复




[Quote=引用 11 楼 fox000002 的回复:]

分别给出各个编译器版本的文件

如果只是用 VC 的话,可以考虑动态库
[/Quote]
fox000002 2011-09-23
  • 打赏
  • 举报
回复
分别给出各个编译器版本的文件

如果只是用 VC 的话,可以考虑动态库
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
dll
周晓荣 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fox000002 的回复:]

引用 5 楼 et214721856 的回复:
1L说静态库无法跨编译器...如果真是这样的话,那静态库也太不灵活了。


静态库本来就不是那么灵活的

静态库就是简单把 object 文件打个包
[/Quote]
那请问,如果平时自己想封装一些常用库,在不同编译器都能使用,用什么方式好?
难道直接把所有文件直接加进工程项目中?
fox000002 2011-09-23
  • 打赏
  • 举报
回复
一般软件提供静态库文件的话,都会分别给出各个编译器版本的文件
fox000002 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 et214721856 的回复:]
1L说静态库无法跨编译器...如果真是这样的话,那静态库也太不灵活了。
[/Quote]

静态库本来就不是那么灵活的

静态库就是简单把 object 文件打个包
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
1l的话 或许对, 或许不对



我曾用vc6的静态lib ,放到2010测试程序下,无法运行程序,


当然也可能是本身代码有问题。。。

后来 我用了vs2010下的静态lib版本 ,才成功运行。。。



[Quote=引用 5 楼 et214721856 的回复:]

引用 4 楼 caddor2011 的回复:

1L说静态库无法跨编译器...如果真是这样的话,那静态库也太不灵活了。
[/Quote]
周晓荣 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caddor2011 的回复:]
[/Quote]
1L说静态库无法跨编译器...如果真是这样的话,那静态库也太不灵活了。
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
这个我不知道 ,


但是 我可以肯定的说 静态lib 和dll产生的 lib 是不一样的 ,后者需要 导出符

前者不要



extern "C" 我从来不加, 对于 dll,我从来不加






[Quote=引用 3 楼 et214721856 的回复:]

引用 2 楼 caddor2011 的回复:

静态库 需要导出符号吗?


不需要好吧 ,我前天就编译过 开源的 类库,用静态库的方式, ,我好想没有看到有到处符号?


另外你的代

#ifdef __cplusplus
}
#endif码

解释一下,我是菜鸟, 学习一样,

为什么 是C语言的时候, 就需要加这个 }

以c++方式编译函数的话,会发生……
[/Quote]
周晓荣 2011-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caddor2011 的回复:]

静态库 需要导出符号吗?


不需要好吧 ,我前天就编译过 开源的 类库,用静态库的方式, ,我好想没有看到有到处符号?


另外你的代

#ifdef __cplusplus
}
#endif码

解释一下,我是菜鸟, 学习一样,

为什么 是C语言的时候, 就需要加这个 }
[/Quote]
以c++方式编译函数的话,会发生名字改编,在c编译器中将会找不到该函数
extern "C"规定以c方式编译,不会发生改编,可以在c文件中调用

我理解得不是很透彻,只知道是大概,你百度一下extern “C"吧
caddor2011 2011-09-23
  • 打赏
  • 举报
回复
静态库 需要导出符号吗?


不需要好吧 ,我前天就编译过 开源的 类库,用静态库的方式, ,我好想没有看到有到处符号?


另外你的代

#ifdef __cplusplus
}
#endif码

解释一下,我是菜鸟, 学习一样,

为什么 是C语言的时候, 就需要加这个 }

fox000002 2011-09-23
  • 打赏
  • 举报
回复
静态库一般无法跨编译器

静态库用 __declspec(dllexport) 是瞎说

15,471

社区成员

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

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