How do I create a Win32 DLL without a dependency o

2019-02-09 10:39发布

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?

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.

For those it does use (eg memcpy), I'm happy to rework the code to use the Win32 API equivalents (eg CopyMemory).

标签: c++ c winapi dll
7条回答
Fickle 薄情
2楼-- · 2019-02-09 11:07

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?

查看更多
爷、活的狠高调
3楼-- · 2019-02-09 11:10

Compile it with static microsoft lib.

查看更多
Juvenile、少年°
4楼-- · 2019-02-09 11:11

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".

查看更多
贼婆χ
5楼-- · 2019-02-09 11:15

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.

查看更多
女痞
6楼-- · 2019-02-09 11:20

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. Compiling your DLL statically won't matter if one of the Win32 DLLs depends on the C runtime.

The only way I can see this working is to statically link ALL your dependent DLLs (if that is even feasible) into your DLL. This of course means you would have to recompile to take advantage of any DLL updates.

查看更多
迷人小祖宗
7楼-- · 2019-02-09 11:23

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.

查看更多
登录 后发表回答