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

前端 未结 7 1528
天涯浪人
天涯浪人 2020-12-31 15:07

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?<

相关标签:
7条回答
  • 2020-12-31 15:27

    for "Debug" mode try this:

    1. Go to Project\[Projectname] Properties...
    2. Open Configuration Properties
    3. Open C/C++
    4. Open Code Generation
    5. For Runtime Library Select Multi-threaded Debug (/MTd) instead of Multi-threaded Debug DLL (/MDd)

    for "Release" mode, do same steps except that selecting Multi-threaded (/MT) in the last step.

    This causes any C Runtime function that used in your program to be statically-linked to your binary file.

    0 讨论(0)
  • 2020-12-31 15:27

    Some Windows libraries depend on the C runtime (ODBC32.DLL, for example) so I think you are on a hiding to nothing here. Why would you want to do this, anyway?

    0 讨论(0)
  • 2020-12-31 15:35

    Compile it with static microsoft lib.

    0 讨论(0)
  • 2020-12-31 15:37

    You may have more dependencies on the CRT than you think. It tears down resources like thread local storage, and global class initializers are run by the CRT before main().

    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.

    Oh, and instead of reworking memcpy, consider using the super-fast compiler intrinsic for it. You can turn on intrinsics with /Oi.

    0 讨论(0)
  • 2020-12-31 15:40

    The /NODEFAULTLIB linker flag is not actually the proper flag. It will ignore all default libs, including others like uuid.lib.

    What you want is the /Zl compiler option, "omit default library name in .OBJ".

    0 讨论(0)
  • Use the /NODEFAULTLIB linker option and (of course) make sure you have no actual dependencies on the runtime. 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).

    Matt Pietrek's article from way back when on LIBCTINY will probably have all the information you need:

    • http://msdn.microsoft.com/en-us/library/bb985746.aspx
    0 讨论(0)
提交回复
热议问题