简体   繁体   English

如何在不依赖于C运行时的情况下创建Win32 DLL

[英]How do I create a Win32 DLL without a dependency on the C runtime

Using Visual Studio 2008 and its C/C++ compiler, how do I create a Win32 DLL that is dependent only on other Windows DLLs, and has no dependency on the Microsoft C runtime? 使用Visual Studio 2008及其C / C ++编译器,如何创建仅依赖于其他Windows DLL的Win32 DLL,并且不依赖于Microsoft C运行时?

I have some C code I want to place in a DLL that is entirely computation, and makes almost no use of C library functions. 我有一些C代码,我想放在一个完全计算的DLL中,几乎不使用C库函数。

For those it does use (eg memcpy), I'm happy to rework the code to use the Win32 API equivalents (eg CopyMemory). 对于它确实使用的那些(例如memcpy),我很乐意重新编写代码以使用Win32 API等价物(例如CopyMemory)。

Use the /NODEFAULTLIB linker option and (of course) make sure you have no actual dependencies on the runtime. 使用/ NODEFAULTLIB链接器选项(当然)确保您没有对运行时的实际依赖性。 You'll also have to specify & define your own entry point for the DLL using the /ENTRY linker option or alternatively have your own entry point function that matches the name expected by the compiler/linker (for a dll, that's _DllMainCRTStartup ). 您还必须使用/ ENTRY链接器选项指定和定义自己的DLL入口点,或者使用您自己的入口点函数来匹配编译器/链接器所需的名称(对于dll,即_DllMainCRTStartup )。

Matt Pietrek's article from way back when on LIBCTINY will probably have all the information you need: 在LIBCTINY上发表的Matt Pietrek的文章可能会提供您需要的所有信息:

You may have more dependencies on the CRT than you think. 您可能比您想象的更多依赖CRT。 It tears down resources like thread local storage, and global class initializers are run by the CRT before main(). 它消除了线程本地存储等资源,全局类初始化器由main()之前的CRT运行。

Consider linking with a static CRT, as someone said, and if you really really don't want to, use /NODEFAULTLIB and /ENTRY as someone else said. 正如有人所说,考虑使用静态CRT进行链接,如果你真的不想这样做,请像其他人所说的那样使用/ NODEFAULTLIB和/ ENTRY。

Oh, and instead of reworking memcpy, consider using the super-fast compiler intrinsic for it. 哦,而不是重新编写 memcpy,考虑使用内在的超快编译器 You can turn on intrinsics with /Oi. 您可以使用/ Oi打开内在函数。

for "Debug" mode try this: 对于“调试”模式试试这个:

  1. Go to Project\\[Projectname] Properties... 转到Project \\ [Projectname]属性...
  2. Open Configuration Properties 打开配置属性
  3. Open C/C++ 打开C / C ++
  4. Open Code Generation 开放代码生成
  5. For Runtime Library Select Multi-threaded Debug (/MTd) instead of Multi-threaded Debug DLL (/MDd) 对于运行时库选择多线程调试(/ MTd)而不是多线程调试DLL(/ MDd)

for "Release" mode, do same steps except that selecting Multi-threaded (/MT) in the last step. 对于“释放”模式,除了在最后一步中选择多线程(/ MT)之外,执行相同的步骤。

This causes any C Runtime function that used in your program to be statically-linked to your binary file. 这会导致程序中使用的任何C运行时函数静态链接到二进制文件。

The /NODEFAULTLIB linker flag is not actually the proper flag. /NODEFAULTLIB链接器标志实际上不是正确的标志。 It will ignore all default libs, including others like uuid.lib . 它将忽略所有默认库,包括uuid.lib等其他uuid.lib

What you want is the /Zl compiler option, "omit default library name in .OBJ". 你想要的是/Zl编译器选项,“省略.OBJ中的默认库名称”。

You'd have to make sure none of the Win32 DLLs you use need the C runtime as well or else you back at square one. 您必须确保您使用的Win32 DLL都不需要C运行时,否则您需要返回第一个。 Compiling your DLL statically won't matter if one of the Win32 DLLs depends on the C runtime. 如果其中一个Win32 DLL依赖于C运行时,静态编译DLL将无关紧要。

The only way I can see this working is to statically link ALL your dependent DLLs (if that is even feasible) into your DLL. 我能看到这个工作的唯一方法是将所有依赖DLL(如果可行的话)静态链接到DLL中。 This of course means you would have to recompile to take advantage of any DLL updates. 这当然意味着您必须重新编译才能利用任何DLL更新。

Some Windows libraries depend on the C runtime (ODBC32.DLL, for example) so I think you are on a hiding to nothing here. 有些Windows库依赖于C运行时(例如ODBC32.DLL),所以我认为你在这里隐藏着什么。 Why would you want to do this, anyway? 无论如何,你为什么要这样做?

用静态microsoft lib编译它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM