VS2017编译c dll 有三种方法
(1).h和.c文件函数增加 __declspec(dllexport)
(2)仅在.h函数添加__declspec(dllexport)
(3)使用.def文件,不用在.h和.c文件的函数前增加__declspec(dllexport)
如果是将已有的c文件编译成dll,推荐使用(3),减少对原始c文件的干预
如果要生成纯粹c dll,使用控制台程序,dll,空项目
解决error C1083: 无法打开包括文件: “sys/time.h”的问题_Valars的博客-CSDN博客_sys/time.h
无法打开源文件<sys/time.h>,但是用time.h编译就会出错,缺少gettimeofday()_L888666Q的博客-CSDN博客_sys/time.h
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
Error LNK2001 无法解析的外部符号 的几种情况及解决办法_小驴的博客-CSDN博客_errorlnk2001无法解析的外部符号
查看DLL 及LIB 库导出函数方法_张十三的博客的博客-CSDN博客_查看lib导出函数
下面是采用def文件生成dll和lib文件,没有问题
D:\OceanMultiBeamProc\bin>dumpbin /exports oncrpc.dll
Microsoft (R) COFF/PE Dumper Version 14.10.25017.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file oncrpc.dll
File Type: DLL
Section contains the following exports for oncrpc.dll
00000000 characteristics
63D870D6 time date stamp Tue Jan 31 09:37:26 2023
0.00 version
1 ordinal base
89 number of functions
89 number of names
ordinal hint RVA name
1 0 000734C0 _null_auth = __null_auth
2 1 000024F5 authnone_create = @ILT+5360(_authnone_create)
3 2 00001AB9 authunix_create = @ILT+2740(_authunix_create)
4 3 00003C42 authunix_create_default = @ILT+11325(_authunix_create_default)
5 4 000035A3 bcmp = @ILT+9630(_bcmp)
6 5 00002441 bcopy = @ILT+5180(_bcopy)
7 6 00002BC1 bzero = @ILT+7100(_bzero)
8 7 0000141A clnt_broadcast = @ILT+1045(_clnt_broadcast)
9 8 00003788 clnt_create = @ILT+10115(_clnt_create)
10 9 00001FAA clnt_pcreateerror = @ILT+4005(_clnt_pcreateerror)
11 A 0000376A clnt_perrno = @ILT+10085(_clnt_perrno)
12 B 00003774 clnt_perror = @ILT+10095(_clnt_perror)
13 C 00002F77 clnt_spcreateerror = @ILT+8050(_clnt_spcreateerror)
14 D 00002AF9 clnt_sperrno = @ILT+6900(_clnt_sperrno)
15 E 00002B12 clnt_sperror = @ILT+6925(_clnt_sperror)
16 F 00003EBD clntraw_create = @ILT+11960(_clntraw_create)
17 10 00003D69 clnttcp_create = @ILT+11620(_clnttcp_create)
18 11 00001AEB clntudp_bufcreate = @ILT+2790(_clntudp_bufcreate)
19 12 00002077 clntudp_create = @ILT+4210(_clntudp_create)
20 13 00003A26 get_myaddress = @ILT+10785(_get_myaddress)
21 14 0000204A getrpcbyname = @ILT+4165(_getrpcbyname)
22 15 00002D10 getrpcbynumber = @ILT+7435(_getrpcbynumber)
23 16 00002518 getrpcent = @ILT+5395(_getrpcent)
24 17 00001474 nt_rpc_report = @ILT+1135(_nt_rpc_report)
25 18 00002F86 pmap_getmaps = @ILT+8065(_pmap_getmaps)
26 19 000030A3 pmap_getport = @ILT+8350(_pmap_getport)
27 1A 0000303F pmap_set = @ILT+8250(_pmap_set)
28 1B 000018B1 pmap_unset = @ILT+2220(_pmap_unset)
29 1C 000734B0 rpc_createerr = _rpc_createerr
30 1D 00003E68 rpc_nt_exit = @ILT+11875(_rpc_nt_exit)
31 1E 00003E72 rpc_nt_init = @ILT+11885(_rpc_nt_init)
32 1F 00074AA0 svc_fdset = _svc_fdset
33 20 000012D0 svc_getreq = @ILT+715(_svc_getreq)
34 21 00001A69 svc_getreqset = @ILT+2660(_svc_getreqset)
35 22 00003887 svc_register = @ILT+10370(_svc_register)
36 23 000034DB svc_run = @ILT+9430(_svc_run)
37 24 00001FBE svc_sendreply = @ILT+4025(_svc_sendreply)
38 25 000020B3 svc_unregister = @ILT+4270(_svc_unregister)
39 26 000038B4 svcerr_auth = @ILT+10415(_svcerr_auth)
40 27 0000336E svcerr_decode = @ILT+9065(_svcerr_decode)
41 28 000032B0 svcerr_noproc = @ILT+8875(_svcerr_noproc)
42 29 000032C4 svcerr_noprog = @ILT+8895(_svcerr_noprog)
43 2A 00001898 svcerr_progvers = @ILT+2195(_svcerr_progvers)
44 2B 00002649 svcerr_systemerr = @ILT+5700(_svcerr_systemerr)
45 2C 00003D0A svcerr_weakauth = @ILT+11525(_svcerr_weakauth)
46 2D 0000332D svcraw_create = @ILT+9000(_svcraw_create)
47 2E 000028BA svctcp_create = @ILT+6325(_svctcp_create)
48 2F 0000245A svcudp_bufcreate = @ILT+5205(_svcudp_bufcreate)
49 30 00003B16 svcudp_create = @ILT+11025(_svcudp_create)
50 31 000016A4 xdr_array = @ILT+1695(_xdr_array)
51 32 000020F9 xdr_authunix_parms = @ILT+4340(_xdr_authunix_parms)
52 33 00002FBD xdr_bool = @ILT+8120(_xdr_bool)
53 34 00001663 xdr_bytes = @ILT+1630(_xdr_bytes)
54 35 00001465 xdr_callhdr = @ILT+1120(_xdr_callhdr)
55 36 00001433 xdr_callmsg = @ILT+1070(_xdr_callmsg)
56 37 00002FD6 xdr_char = @ILT+8145(_xdr_char)
57 38 00003C2E xdr_des_block = @ILT+11305(_xdr_des_block)
58 39 00001866 xdr_double = @ILT+2145(_xdr_double)
59 3A 00002365 xdr_enum = @ILT+4960(_xdr_enum)
60 3B 0000164F xdr_float = @ILT+1610(_xdr_float)
61 3C 00002FEF xdr_free = @ILT+8170(_xdr_free)
62 3D 00003512 xdr_int = @ILT+9485(_xdr_int)
63 3E 00002FD1 xdr_long = @ILT+8140(_xdr_long)
64 3F 000039A4 xdr_netobj = @ILT+10655(_xdr_netobj)
65 40 00001979 xdr_opaque = @ILT+2420(_xdr_opaque)
66 41 000031DE xdr_opaque_auth = @ILT+8665(_xdr_opaque_auth)
67 42 00002833 xdr_pmap = @ILT+6190(_xdr_pmap)
68 43 00003599 xdr_pmaplist = @ILT+9620(_xdr_pmaplist)
69 44 000022E8 xdr_pointer = @ILT+4835(_xdr_pointer)
70 45 00002F68 xdr_reference = @ILT+8035(_xdr_reference)
71 46 000035B7 xdr_replymsg = @ILT+9650(_xdr_replymsg)
72 47 00001D16 xdr_short = @ILT+3345(_xdr_short)
73 48 00002CC5 xdr_string = @ILT+7360(_xdr_string)
74 49 000019B5 xdr_u_char = @ILT+2480(_xdr_u_char)
75 4A 00001CE9 xdr_u_int = @ILT+3300(_xdr_u_int)
76 4B 00001992 xdr_u_long = @ILT+2445(_xdr_u_long)
77 4C 00002310 xdr_u_short = @ILT+4875(_xdr_u_short)
78 4D 00003DEB xdr_union = @ILT+11750(_xdr_union)
79 4E 000019AB xdr_vector = @ILT+2470(_xdr_vector)
80 4F 00002B8F xdr_void = @ILT+7050(_xdr_void)
81 50 00003EB8 xdr_wrapstring = @ILT+11955(_xdr_wrapstring)
82 51 0000241E xdrmem_create = @ILT+5145(_xdrmem_create)
83 52 00001D66 xdrrec_create = @ILT+3425(_xdrrec_create)
84 53 00001A46 xdrrec_endofrecord = @ILT+2625(_xdrrec_endofrecord)
85 54 00003A3A xdrrec_eof = @ILT+10805(_xdrrec_eof)
86 55 00003175 xdrrec_skiprecord = @ILT+8560(_xdrrec_skiprecord)
87 56 0000114F xdrstdio_create = @ILT+330(_xdrstdio_create)
88 57 000016E5 xprt_register = @ILT+1760(_xprt_register)
89 58 00002DB0 xprt_unregister = @ILT+7595(_xprt_unregister)
Summary
1000 .00cfg
3000 .data
1000 .idata
D000 .rdata
3000 .reloc
1000 .rsrc
64000 .text
如果想使用与源文件中定义的不一样的函数名,可以按照以下语法:entryname=internalname
其中,entryname是要导出的符号名,而internalname是DLL中将要导出的函数名。
D:\OceanMultiBeamProc\oncrpc\win32\bin>dumpbin /exports oncrpc.lib
Microsoft (R) COFF/PE Dumper Version 14.10.25017.0
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file oncrpc.lib
File Type: LIBRARY
Exports
ordinal name
__null_auth
_authnone_create
_authunix_create
_authunix_create_default
_bcmp
_bcopy
_bzero
_clnt_broadcast
_clnt_create
_clnt_pcreateerror
_clnt_perrno
_clnt_perror
_clnt_spcreateerror
_clnt_sperrno
_clnt_sperror
_clntraw_create
_clnttcp_create
_clntudp_bufcreate
_clntudp_create
_get_myaddress
_getrpcbyname
_getrpcbynumber
_getrpcent
_nt_rpc_report
_pmap_getmaps
_pmap_getport
_pmap_set
_pmap_unset
_rpc_createerr
_rpc_nt_exit
_rpc_nt_init
_svc_fdset
_svc_getreq
_svc_getreqset
_svc_register
_svc_run
_svc_sendreply
_svc_unregister
_svcerr_auth
_svcerr_decode
_svcerr_noproc
_svcerr_noprog
_svcerr_progvers
_svcerr_systemerr
_svcerr_weakauth
_svcraw_create
_svctcp_create
_svcudp_bufcreate
_svcudp_create
_xdr_array
_xdr_authunix_parms
_xdr_bool
_xdr_bytes
_xdr_callhdr
_xdr_callmsg
_xdr_char
_xdr_des_block
_xdr_double
_xdr_enum
_xdr_float
_xdr_free
_xdr_int
_xdr_long
_xdr_netobj
_xdr_opaque
_xdr_opaque_auth
_xdr_pmap
_xdr_pmaplist
_xdr_pointer
_xdr_reference
_xdr_replymsg
_xdr_short
_xdr_string
_xdr_u_char
_xdr_u_int
_xdr_u_long
_xdr_u_short
_xdr_union
_xdr_vector
_xdr_void
_xdr_wrapstring
_xdrmem_create
_xdrrec_create
_xdrrec_endofrecord
_xdrrec_eof
_xdrrec_skiprecord
_xdrstdio_create
_xprt_register
_xprt_unregister
Summary
C0 .debug$S
14 .idata$2
14 .idata$3
4 .idata$4
4 .idata$5
C .idata$6
调用dll两个方法,静态调用和动态调用
静态调用,不能解决动态调用的问题,但是由于与dll配套使用,可以解决,让exe文件变小
C/C++:Windows编程—调用DLL程序的2种方法_重庆李四的博客-CSDN博客_windows c调用dll
add.h
#pragma once
int add(int n1, int n2);
add.c
#include "add.h"
int add(int n1, int n2)
{
return n1 + n2;
}
add.def
LIBRARY
EXPORTS
add
testadd.c
#include <stdio.h>
下面这两行,可不写
#pragma comment(lib,"add.lib")
int add(int a, int b);
int main(char argc, char* argv[])
{
int a = add(1, 2);
printf("%d", a);
return 0;
}
但是在生成bsio.dll的过程种,总是报如下错误:
Error LNK2001 无法解析的外部符号,比如xdr_double
实际上oncrpc.lib和oncrpc.dll的函数名都有这个,实在是让人费解。
后来用Debug版生成bsio.dll,发现了一个错误,oncrpc.dll是32位平台生成的,于是用x64平台再重新生成一遍,再加载oncrpc模块,就没有问题,至此,bsio.dll模块就生成了。
参考文献
VS2017 编写 C++ DLL 给 易语言,C++,C#等第三方调用_YNStong的博客-CSDN博客_visual studio c++dll生成给易语言调用
创建和使用动态库 使用def文件_qianpeng4的博客-CSDN博客
使用模块定义文件(.def)文件生成dll_麻子来了的博客-CSDN博客
vs2017生成C++/C语言的DLL以及调用 极简讲解 秒懂_萌面刺客的博客-CSDN博客
VS2017——C语言dll编译并调用_baijian1989的博客-CSDN博客_c函数编译成dll并调用