How to import .lib into .dll? - c++

I'm trying to code a dll as a plugin for some system using C++. This plugin will make use of another library which is a .lib file and should output only one .dll file as a result. So I want this other .lib file included in my dll. There should be only one dll file and lib file should be included in it, so that I could include this dll file into the system as a one-file plugin. Is this possible and how?
I'm using Visual C++ 2010 Express.
Thank you.

Unfortunately, the VS linker doesn't have an option equivalent to ld's --whole-archive which can be used to include an entire library.
Your best bet is to unpack the library and link in the resulting object files. You can use the librarian (lib tool) for that. To list all members of the library, use lib /LIST. Object files have to be extracted one by one, using lib /EXTRACT:member.

Related

Dynamic linking in visual studio C++

I want to ask whether the library.lib file generated when creating a dynamic library contains some kind of memory location reference to the function of the dynamic library?
How exactly does the executable find and resolve the functions either at load time or runtime?
In visual studio, the dynamic library .dll and static library .lib are created by creating a library application, followed by generating the header file and .cpp file representative of the library to be included into other source codes and finally compilation.
The library is then used by another program source code by using the include directive on the header file, then followed by declaring the location to header files of the library under additional include directories. Then settings additional dependencies to the static .lib file generated previously also called DLL import library. A post build event is then set in order to copy the .dll file into the local working directory of the executable.
DLL import library, what exactly is contained in this static library which i guess provides instructions to resolve functions in the dynamic library?
Thanks

How do I include my C++ DLL in a separate project?

This question might seem obvious but I am having a lot of trouble with this, and I have ended up having to post here after a lot of searching.
I currently have two windows of Visual Studio open. One is a Win32 Console->DLL project which exports a class, and in the output directory I have:
.dll file
.exp file
.pdb file
.lib file
I have dropped the DLL file into the my other project's output directory, as I do with all DLLs, and that works fine usually. Then, I added the directory into the Linker's library directories.
But unlike most libraries I use, I think I have done something wrong or I misunderstand how this works, I have no .h[pp] files, and so I have no idea how I am supposed to include the functions into my code. I'd rather not have Windows-only hacks (I want to confine that to the DLL project only, so that it can be ported easily).
Can anyone enlighten me as to what I am doing wrong?
There is nothing 'hacky' or 'windows' specific about having .h files available to the other projects. Your .lib file will provide the necessary information to complete the build. See: How do I use a third party dll in Visual Studio C++?
Did you add the .lib file corresponding to the .dll into the other project's directory?
It is the .lib file that is consumed by the linker, not the DLL (which is consumed by the loader at run-time).
A .dll is a shared library, as opposed to a static library (.lib on Windows).
Static library must always be linked when you compile your project, and you can easily call their functions using header (.h/.hpp) files, whereas you have two options for the shared library:
static linking (at compile-time, but the way to do it is different than for a static library)
dynamic linking (at run-time)
I would advise you to read this in-depth article: http://www.codeproject.com/Articles/85391/Microsoft-Visual-C-Static-and-Dynamic-Libraries
See also the wikipedia article: https://en.wikipedia.org/wiki/Dynamic-link_library

Working with DLL in a C++ application

I'm planning to use id3lib in my application. I'm looking for a way to use the library as a DLL. I've noticed that they released the library in various form: one of which is windows binary. My target platform in Windows, and I'll use Qt 4.8. After I extract the files in windows binary, I found the following files in Release folder:
id3lib.dll
id3lib.exp
id3lib.lib
I know how to use a DLL in Qt given the DLL, one or more header files where function prototypes resides, and with or without the *.lib file. This package doesn't come with a header file.
How am I supposed to use this package without any header file? What's the purpose of the *.lib and *.exp files here? As far as I know *.lib files are used for static linking with functions which I don't want in my program.
You've just missed the header. It is available under the include subfolder (see here), also the .lib file is still needed for linking, even if you'll be using the DLL.
The usual course is to use a header file #included in the C++ file, the .lib file to link to and the .dll is required at run time.
The header file should/may be in another package as the same header is probably used for different kinds of linking strategies.
At worst you should be able to use a tool such as depends.exe to view the exported symbols and create your own h file to match - but it would be better to find a .h file issued with the release.

How link .obj files in Visual Studio?

My problem is:
I have compiled LuaJit with VisualStudio and its genereated a .Obj file, a dll and .lib file.
i just want to link the .obj file in my win32 application to generate a single EXE file.
If its possible,i still need to add the Lua headers?
Thanks anyway.
Use the header file to define LuaJit symbols for your code that deals with Lua.
To link you only need the lib and dll. Add the lib file to your link dependencies under Project Properties > Linker > Input. Put the DLL next to your EXE when you run.
You need to make the project produce a .lib file containing the code (instead of in addition to the dll) and link that. It could be that you can by switching the project setting (look for one labelled lib, or static). Otherwise, you'll have to modify the project settings. (set output type to lib rather than dll).
I never tried linking a .obj file directly, it might be possible. Lib files are more standard.

Simple Way to Use a DLL

I followed the MSDN walkthrough on creating and using a DLL in Visual C++ Studio, but it requires the user to add the DLL project to the same solution as the project they're working on.
Is there a simple way to include a DLL? Ideally, I'd like to just distribute my .dll (and the .lib, I suppose) to my friends so they can use it in their own projects.
I realize there are other walkthroughs out there (some of them on SO), but they all require editing the PATH environment variable, etc. Is that really the simplest way?
At a minimum, you need to do the following:
Include the .lib file in the project
Tell the linker where you put the .lib file (library search path)
Make the .dll file available at runtime (easiest is to put it in the same directory as the .exe)
To distribute the compiled .dll to your friends, you will need to include:
the .h file(s) for the compiler
the .lib file for the linker
the .dll file for runtime