How to determine default libraries for an .obj object file in Visual C++ (143072)



The information in this article applies to:

  • Microsoft Visual C++ 2.0
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 2.2
  • Microsoft Visual C++ 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q143072
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SUMMARY

In Visual C++, default libraries can be linked to automatically. This is done for the C run-time libraries. The compiler writes a default C run-time library name in the object (.obj) files it creates. The library name depends on the compiler options you use.

You can also specify default libraries by the using the #pragma comment() compiler directive. The Microsoft Foundation Class (MFC) header files do this.

Writing a default library name in an object file in this manner saves you from having to specify the library on the linker command line. However, sometimes it is necessary to determine which libraries an object file links to by default.

MORE INFORMATION

To see which libraries an object file will use by default, you can use the Dumpbin.exe utility in a console session. For example, to find out which libraries Mfcapp.obj will cause the linker to search automatically, you would use the following code (this example pipes the output to a file):
   DUMPBIN /RAWDATA /SECTION:.drectve MFCAPP.OBJ > OUTPUT.TXT
				
In the column to the right side of the output, you'll see the list of default libraries; the following is an excerpt from that column:
-default|lib:mfc4
0.lib -d|efaultli
b:mfcs40|.lib -de
faultlib|:msvcrt.
lib -def|aultlib:
kernel32|.lib -de
faultlib|:user32.
lib
				
From this excerpt, you can determine this object file will cause the linker to automatically search Mfc40.lib, Msvcrt.lib, Kernel32.lib, and User32.lib when building an executable file. As you can see, each library name is preceded with "-defaultlib:". You can use this procedure for multiple object files, and compare the output of each file.

One reason you may need to determine which default library names are in an object file is to troubleshoot the LNK2005 error, "symbol multiply defined."

Modification Type:MajorLast Reviewed:1/9/2006
Keywords:kbDebug kbhowto KB143072 kbAudDeveloper