How to build static and dynamic libraries from .obj files for Visual C++?

user366312 picture user366312 · Aug 1, 2015 · Viewed 14.4k times · Source

I have Visual Studio 2008, Windows7 64 bit.

I am using WinBGIm Graphics Library.

This library is supplied with some .obj files. There are no .lib or .dll files.

I want to convert them into static .lib and dynamic .dll files.

I have copied all .obj files in the directory:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64

But, the following command is not working:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64>lib.exe /out:bgiout.lib *.obj
Microsoft (R) Library Manager Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

LINK : fatal error LNK1104: cannot open file 'bgiout.lib'

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64>

How to do that?

Answer

Niall picture Niall · Aug 5, 2015

Yes you can do that, pretty much just as you have it.

C:\Code\bgi\obj>lib /out:libbgi.lib *.obj

LIB (lib.exe) is used to create static libraries. LINK (link.exe /DLL) is used to create dynamic libraries (it creates the .dll and an import library .lib).

C:\Code\bgi\obj>link /DLL /out:bgi.dll *.obj [additional libs]

When using the link /DLL command, additional Win32 and C++ standard runtime libraries will be required (such as MSVCRT.lib and User32.lib etc. and MFC libraries).

In this case; this seems to be the correct linker arguments;

C:\Code\bgi\obj>link /DLL /out:bgi.dll *.obj MSVCRTD.lib User32.lib Gdi32.lib ole32.lib Comdlg32.lib OleAut32.lib

Note: the object files built are debug versions, hence MSVCRTD.lib (note the D) is the one to use here. With the commands above, I've been able to successfully link both a .dll and a static .lib.

Additional include and library paths;

When distributing these outputs for other builds additional header and library paths may need to be included into the target build. To add additional locations to the include and library search paths, the environment variables (INCLUDE and LIB) can be added to (either per user or system wide), but these can also be specified on the command line, via /I and /LIBPATH as follows;

cl /IC:\Code\include [additional options] main.cpp
link /LIBPATH:C:\Code\lib [additional options] xyz.lib

Guidelines;

  • Start a "Visual Studio" command prompt, given 2008, there should be a link in the start menu "Visual Studio 2008 Command Prompt". This batch file will setup the correct environment for a C++ build. Make sure to match the correct toolchain for the x86 or x64 targets.
  • Navigate to the directory that contains the object files.
  • Run the command(s) you have (as above).

Your error LNK1104

I suspect the error you have, LNK1104, is most likely because your user does not have sufficient permission to be writing files within the "Program Files" directory. Else, it may be an error with using the incorrect toolchain for your target (x86 vs. x64).

It is generally better to do this in a directory of your own; e.g.: "C:\Code\bgi".