C源代码中使用dll的效率
我有一个 dll,我想在 c
程序中使用,
您认为拥有一个 dll(许多常用函数)然后创建一个最终将使用的程序是否有效他们,或者有所有的源代码?
要包含 dll,必须遵循什么语法?
I have a dll which I'd like to use in a c
program,
Do you think is efficient to have a dll (lots of common functions) and then create a program that will eventually use them, or have all the source code?
To include the dll, What syntax must be followed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于内存和磁盘空间,使用共享库(DLL是共享库的Windows实现)更有效,假设至少有两个程序使用该组件。如果只有一个程序使用该组件,则无法节省内存或磁盘空间。
共享库可能比静态链接代码稍慢;然而,这可能是非常小的,并且共享库具有许多好处,使其非常值得(例如动态加载和处理符号的能力,这允许类似插件的架构)。也就是说,也有一些缺点(如果您不小心 DLL 的位置、版本控制方式以及谁可以更新它们,那么您可以进入 DLL 地狱)。
这取决于。有两种方法可以使用共享库。第一种方式,你告诉链接器引用共享库,共享库将在程序启动时自动加载,你基本上会像平常一样引用代码(包括各种头文件,并且在启动时只使用符号的名称)你想引用它)。第二种方法是动态加载共享库(在 Windows 上,这是通过 LoadLibrary,在 UNIX 上使用 dlopen< /a>)。第二种方法可以根据共享库中符号的存在或不存在来更改程序的行为,并检查可用的符号集。对于第二种方式,您可以使用 GetProcAddress (Windows) 或 dlsym (UNIX) 获取指向函数在库中定义,并且您将传递函数指针来引用已加载的函数。
For memory and disk space, it is more efficient to use a shared library (a DLL is the Windows implementation of shared libraries), assuming that at least two programs use this component. If only one program will ever use this component, then there is no memory or disk space savings to be had.
Shared libraries can be slightly slower than statically linking the code; however, this is likely to be incredibly minor, and shared libraries carry a number of benefits that make it more than worthwhile (such as the ability to load and handle symbols dynamically, which allows for plugin-like architectures). That said, there are also some disadvantages (if you are not careful about where your DLLs live, how they are versioned, and who can update them, then you can get into DLL hell).
This depends. There are two ways that shared libraries can be used. In the first way, you tell the linker to reference the shared library, and the shared library will automatically be loaded on program startup, and you would basically reference the code like normal (include the various headers and just use the name of the symbol when you want to reference it). The second way is to dynamically load the shared library (on Windows this is done via LoadLibrary while it is done on UNIX with dlopen). This second way makes it possible to change the behavior of the program based on the presence or absence of symbols in the shared library and to inspect the available set of symbols. For the second way, you would use GetProcAddress (Windows) or dlsym (UNIX) to obtain a pointer to a function defined in the library, and you would pass around function pointers to reference the functions that were loaded.
您可以将函数放入静态库(.lib)中,该库会在编译时合并到应用程序中,与将 .c 文件放入项目中基本相同。
或者您可以使用在运行时包含函数的 dll。 dll 的优点是两个使用相同功能的程序可以使用相同的 dll(节省磁盘空间),并且您可以升级 dll 而无需更改程序 - 这些对您来说可能都不重要。
当您的程序运行时,该 dll 会自动加载,您无需执行任何特殊操作即可包含它(您可以在代码中专门加载 dll - 有时有特殊原因这样做)
编辑 - 如果您需要创建存根库对于现有的 dll,请参阅 http://support.microsoft.com/kb/131313
You can put your functions into either a static library ( a .lib) which is merged into your application at compile time and is basically the same as putting the .c files in the project.
Or you can use a dll where the functions are included at run time. the advantage of a dll is that two programs which use the same functions can use the same dll (saving disk space) and you can upgrade the dll without changing the program - neither of these probably matters for you.
The dll is automatically loaded when your program runs there is nothing special you need to do to include it ( you can load a dll specifically in your code - there are sometimes special reasons to do this)
Edit - if you need to create a stub lib for an existing dll see http://support.microsoft.com/kb/131313