Use .dll files in a Visual Studio project without headers [duplicate] - c++

This question already has answers here:
Copying a DLL's dependencies in Visual Studio
(9 answers)
Closed 8 years ago.
I downloaded a source code form a website and it contains the main.cpp and a folder with some .dll files. In the main.cpp file there are includes to headers with similar names to the .dll files. Is there any way I can use the source code in a VS project with only the .dll files even if I have not the headers?

To answer your question, yes you can do that. Just load a DLL using the LoadLibrary() function and resolve functions from it with GetProcAddress(), aka "explicit loading". However, this requires knowledge of the interface that the DLL exports, which is equivalent to the information provided by the headers that you don't have. If you had the information, you could also write the headers, if you don't, you can't load the DLL. Note that to some extent, tools like dependencywalker can determine the DLL's interface, but that may or may not be enough to use it. In summary, no, you can't do that, you need to know the interface.

Related

How to use a dynamic library in c++ project using only the .dll file [duplicate]

This question already has answers here:
How to use DLL without header in C++
(4 answers)
Closed 1 year ago.
I have a dynamic library already generated with cmake in c++. How can I use it within another c++ project without having the .h or the .cpp file for the library. I only have the .dll file.
There are at least 2 ways:
Generate .lib/.def (or other files) from your .dll file and use them into host application. You can see utilities like implib, mkexp etc.
Use API: LoadLibrary for loading and GetProcAddress for getting function pointer. There is c++ wrapper for this: see boost::dll::import_alias.
As you mention dll I'm assuming you are on a Windows platform. You can use tools like dumpbin /exports someLib.dll to list the functions exported by the dll and after that you can use GetProcAddress to create a function pointers to those functions.
If you need something more complex, like using a C++ type this might get more complicated or even impossible as you need to link with the exact version of the standard library, compiler version and compiler flags.
For example if you have a function void myFunction(const std::string&) in the dll you must be sure the std::string from your executable is binary compatible with the one in the dll. If they are not binary compatible you'll have very difficult to find and solve run time errors. This applies even if you have the headers and this is the reason for libraries like QT to deliver multiple versions of the same dll

Why specify particular library in Visual Studio if the path to lib is known? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
TL/DR: Question start with steps that I've made and that I think are important to mention (let me know if it is not important and I will delete it). The question itself is in the bottom of page. I've posted 3 questions that has the same steps that I've issued before asking the question, however questions are different. Thanks
I've successfully compiled libharu library with cmake so I can use it in my own c++ project to generate PDF files. It was compiled with Visual Studio 2013 as Debug/Win32. cmake-gui was set following way:
Sources:
c:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0
Build:
C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32
My current project (created from scratch) that I'm using libharu in has following structure (again Visual Studio 2013):
./Debug
./libharu_example
./libharu_example/Debug
./libharu_example/Debug/libharu_example.tlog
./libharu_example/libharu
./libharu_example/libharu/include
./libharu_example/libharu/lib
./libharu_example/libharu/src
./libharu_example/libharu/src/CMakeFiles
libharu/include contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\include
libharu/src contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-RELEASE_2_3_0\src
libharu/lib contains files from C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug
Visual studio project settings are following:
# C/C++ -> General -> Additional Include Directories:
$(ProjectDir)libharu\include
# C/C++ -> Preprocessor -> Preprocessor Definitions:
_CRT_SECURE_NO_WARNINGS
# Linker -> General -> Additional Library Directories
$(ProjectDir)libharu\lib
# Linker -> Input -> Additional Dependencies
libhpdfd.lib
Inside main file I'm including libharu this way:
#include "libharu/include/hpdf.h"
Finally to the question:
In the C:\Users\wakatana\Downloads\__c++_pdf__generation\libharu-build32\src\Debug directory there are also those files:
libhpdfd.dll
libhpdfd.exp
libhpdfd.ilk
libhpdfd.lib
libhpdfd.pdb
libhpdfsd.lib
I've tried to set Linker -> Input -> Additional Dependencies also to libhpdfsd.lib and libhpdfd.dll but the only one that worked was libhpdfd.lib. What is purpose of other above mentioned files and how do I know which *.lib *.dll should I use? Also why I need to specify this to Visual Studio? Isn't it smart enough that it can load it automatically? It has already specified $(ProjectDir)libharu\lib where all those libs were stored, why not just pick the best one automatically?
libhpdfsd.lib - this is a static library. Static libraries are linked at build time by the linker.
libhpdfd.dll - this is a dynamically linked library. In contrast to a static library, it is not linked at build time. Instead, it is loaded into process memory explicitly at runtime with LoadLibrary and addresses of its exported functions and variables are obtained with GetProcAddress. This requires writing some boilerplate code. To avoid doing that, there's often a corresponding static library, called import library, that automagically does this for you. This is what libhpdfd.lib is.
libhpdfd.pdb - this is a program database file. It is used by a debugger.
libhpdfd.exp - this is an export file. It is useful when you have cyclic dependencies.
libhpdfd.ilk - this file is for incremental linking. Incremental linking speeds up linking phase which is useful when during debugging you make small changes in your code and rebuild all project.
You need to specify explicitly a library you use because you may have different libraries or different versions of the same library, which export symbols with the same name. In that case a linker can't know from which library your symbol should be imported and you'd get a linker error. I believe the error you got when you added libhpdfsd.lib to additional dependencies was because of this.
Whether to use the static library or the dll is up to you. Your default choice is to use the static library, use dll when you have to.
In visual studio there are two types of lib files, first one is static library and second lib contains only symbols for mapping with a DLL. If you choose to build you project using dynamic libraries, VS will expect that the second type of lib is specified as it cannot distinguish the two from the filename and vice versa for building statically linked projects.

What exactly are development headers? [duplicate]

This question already has answers here:
What are *-devel packages?
(2 answers)
Closed 6 years ago.
I'm confused about the use for "*-devel" packages, and so I looked it up. According to an answer in this post, What are *-devel packages?:
"For running an application using the library libfoo only the actualy
shared library file (.so., for example libfoo.so.1.0) are needed
(plus possibly some data files and some version-specific symlinks).
When you actually want to compile a C application that uses that
library you'll need the header files (.h, for example foo.h) that
describe the interface of that application as well as a version-less
symlink to the shared library (.so, for example libfoo.so ->
libfoo.so.1.0). Those are usually bundled in the *-devel packages."
This seems redundant to me. To me it sounds like this: "To use the library you need just the libfoo. But if you want to use the library, you need the header files and hence libfoo-devel"
I can't quite find the answer to the importance of the header files.
After you built an application using libfoo, you obviously do not need any of libfoo's headers installed any more. The application is already compiled. You don't need the header files to run something that's already compiled. The only thing you need is libfoo itself, because your application is linked to it.
And that's your libfoo: just the library itself. Maybe an occasional configuration file, or something else, that's needed at runtime.
On the other hand, if you need to build and compile the source code that uses libfoo, you need the header files, and other supporting files. Whatever is needed to build the source code that uses libfoo.
And those bits are the ones that go into the libfoo-devel package. That's the stuff that's needed to build the source code that uses libfoo.

Linking dependency to static library? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I have a small console application that makes use of the MySql C/C++ connector interface.
I'd like to make a static library out of the existing code and am wondering how (or if it is possible) to link the MySql C/C++ interface with my static library seeing as there is no "Link" tab in the properties dialog of the project.
I'd basically like to link my static library to a new project and have everything work as it did in my old console application.
Is there a way of doing this (without simply just linking the MySql C/C++ interface to the new project) or am I looking at this from the wrong angle? If so let me know what might be a better alternative.
Static library projects (i.e. that output a .lib file on a win32 environment) haven't a link tab because they're not meant to be linked when generating the output file.
Instead they concur, alongside with other .lib files, to resolve link-time dependencies in a project (e.g. executable) that requires linking.
To do what you want you just need to include the necessary headers and make sure both your code and the C/C++ interface (which is also a lib file) are provided at link-time for the project that will use them and will be ultimately linked

About C++ Libraries

When I install an IDE like VB for example, it has C++ libraries.
The question is, how to know the contents of a library (methods or manipulators) the way I am intended to do.
Where should a beginner find the contents formally? I found them in Wikipedia, but I want to know the original source (if we suppose that no internet connection is available).
MSDN would be a good source. And C++ Reference too.
You can donwload the current image of cppreference.com wiki at this address. It is updated daily.
Simple question here, when i install an IDE like vb for example, it has c++ libraries.
What libraries are you referring to?
where should a beginner find the contents formally?
Libraries that are meant for public consumption are described in their respective documentation. Where that documentation is found differs greatly. Most documentations for big public libraries are found online, on the official websites of these libraries, though.
Since the question explicitly mentioned C++, the C++ standard libraries are described at cplusplus.com. There is another large collection of C++ libraries, called Boost which is described on their homepage.
The libraries that ship with VB (which VB, though? VB.NET or VB6?) are Microsoft’s, and are therefore described on their developer network homepage, msdn.microsoft.com. An offline copy of this documentation is installed with Visual Studio; however, the software used to navigate it is barely usable (especially when accessed via Visual Studio).
The original source are the header files (.h files). In these files you will find the functions declarations and class definitions. In some cases, like template code, you will also see the implementationl. In others, the implementation will be precompiled into a .dll or .lib file and you can't see it. But all available things are in the header file. To use its contents you must #include the header file and link against the implementation. For standard library, the linking is done for you by the IDE
Everything that comes with Visual Studio and its subset installations is documented on MSDN. Do you have more specific questions re the C++ libraries? Do you need info on Win32, C runtime (CRT), C++ standard library?
Every lib usually has a coupled header (.h) file that describes the signatures of methods and types available with that library. The library is linked (or dynamically loaded and linked) with the executable while the header is used during compilation to the purpose of static checking the source.
You should just look for an include folder and check its contents.
The contents of a library is contained in the headers which ship with your IDE.
The .h files contain just the function declarations. The source containing the definitions is already compiled, so when you include a header, the linker looks for the function definitions in the libraries that you have linked. Most of the libraries are open source, some of them are just specifications that anyone can implement for example OpenGL, and the standard library is a specification, too. The same goes for the C++ language. Then programmers use the specifications to write implementations (GCC, VC++).
Here you can download the source code of STL C++ https://www.sgi.com/tech/stl/download.html