Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 520
user366312 Avatar asked Aug 01 '15 16:08

user366312


People also ask

How will you create a static library from object files?

Static libraries are created using some type of archiving software, such as ar. ar takes one or more object files (that end in .o), zips them up, and generates an archive file (ends in . a) — This is our “static library”. Now that we have the object file(s), we can archive them and make a static library using ar.

How do I add an OBJ file to Visual Studio?

Go to project properties then from "Property Page" select the node "C/C++" their you will get "Additional Include Directories" add the name of your object file. Keep your obj file in the directory where your source code is or you can add the directory from: Tools->Options->Projects and Solutions->VC++Directories .

What command is used to create a static library from object files in C?

In other words, the “ar” command can be used to create a static library, to add and to modify object files already included in the library. The object files included in the static library can then be listed via the following command line: “ ar -t libstatic_library.


2 Answers

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

like image 56
Niall Avatar answered Sep 23 '22 15:09

Niall


Modern C++ compilers will embed information about the libraries they need. For visual studio, a .obj file includes a reference to the C++ libraries it relies on (/MT /MD /MTd /MDd) these libraries have slightly different implementations, and are not compatible with them. The only choice is to have source code, or multiple .obj files for each supported build mode

like image 33
mksteve Avatar answered Sep 22 '22 15:09

mksteve