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

2

16

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?

Illumine answered 1/8, 2015 at 16:5 Comment(6)
Create a project. Add the files to the project. Command line is much better though.Livvy
That's because you've not setup the env variables properly.Livvy
To build a .lib, use lib.exe.Unexacting
Please edit your question and place your information into the question and not as comments.Leninakan
Just FYI, the Microsoft compiler lets you link directly to .obj files as though they were lib files. You might not need them as libs.Busywork
@VoidStar, yeah I know.Illumine
W
29

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

Worth answered 5/8, 2015 at 10:43 Comment(7)
Yeah, you were right, that was about permission and the selection of tool. Before I award you the bounty, can you please tell me how to deploy lib files so that I don't need to include C++ headers like #include "graphics.h" ?? Instead I want to write #include <cgraphics>, or, at least, #include <graphics.h>.Illumine
@anonymous. I'm not sure what you mean to deploy the lib. The header file is needed at compile time during the build. The lib files are needed at link time in the build, not at runtime on the target machine. The dll would be needed at runtime. The header file would typically contain nothing more than the definitions required for the code in the lib or dll (essentially this would be the class and function declarations, not any code)...Worth
If you need to distribute the lib for other people to build against the dll, that import lib includes just enough code to link into the exe to load the dll and fix up the function pointers. The difference between "" and the <> form of include goes to how the preprocessor locates the header file. <> looks into the include path and the "" looks for the file locally - the name of the file doesn't really matter - it is just by convention. If distibuting the lib and header for building, then <> would probably be better...Worth
MSVC includes a #pragma(lib, "mylib.lib") directive that assists in the linking, but it equivalent to adding the lib as an linker argument. This is all quite different to the C# assemblies etc. that essentially have no direct equivalent to the lib files.Worth
@anonymous. I know that was a little long, but I hope the helps you with some of the aspects of the C++ build?Worth
I mean , I just want that graphics.lib and graphics.h behaves like any other standard c++ library in terms of path and #include directive.Illumine
Let us continue this discussion in chat.Worth
M
1

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

Machinist answered 10/8, 2015 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.