So, I made a dll and it compiles great.
Then I referenced this dll I made in another project and received this error message:
error C1083: Cannot open include file: 'openssl\ssl.h': No such file or directory
this .h file is used inside the dll, I would think that by referencing the dll I should not have to include this file directly...
Shouldn't a dll have all the files needed for it's purpose "inside it"?
Shouldn't a dll have all the files needed for it's purpose "inside it"?
No. A DLL contains machine code.
The main difference between .c and .h files is that .c files contain Code and .h files contain Headers (i.e. that's what they're supposed to, although they're not bound to). You need those header files in order for the compiler to know what to look for in the DLL. After your program has been compiled and linked, the header files are not required anymore.
That's why the authors of libraries written in C or C++ which are not open source usually provide precompiled binaries as well as header files.
A file format containing machine code and headers would be possible, but to my knowledge, no such format exists, and it would be really bad if it did, because for a lot of programs that would mean huge executable files.
No, because:
A .dll is a compiled, binary file that can be dynamically loaded at runtime by .exe programs.
A .h (or .hpp) file contains source code definitions of function prototypes or data structures for your C/C++ program, which are used during compilation.
To compile your source code, you'd need to:
#include the header file(s) so that the rest of your code knows what the data structures and function signatures stored in the DLL look like.
Link with the .lib or .a file equivalent of the .dll file.
If all goes well, then the .exe file generated by the compilation process will be able to dynamically load in and use the (already compiled) functions stored in the .dll file.
Related
How are we supposed to create a DLL, included with header files?
For example, a project using the raylib game library requires raylib.dll to be present with the output file.
The raylib.dll is included with the header files of the raylib game library.
Is that how are supposed to create a DLL and include header files into it?
I am using Dev C++ with a GCC compiler.
There's only a limited relation between the header files and the creation of the DLL. In particular, you use the word "linked", and header files are not linked. Header files are included, libraries are linked. Including happens before a compile, linking afterwards.
Header files can provide declarations for functions defined in the DLL. That tells the compiler that those functions actually exist (somewhere), so the compiler will ask the linker to find those functions. The linker in turn will find them in the library.
I have several functions in a .cpp file and I want several of my projects use the same file.
So instead of copy them to different directories just add them (from original location) to project file in visual studio and use them.
The problem is #include "stdafx.h" seems to refer to the same directory as the file exist and this causes compiler problems.
Is there a way or workaround or am I doing something conceptually wrong?
(I think making it as a static library is a bit overkill. compiling and building new project and making sure others are using correct DEBUG/RELEASE .lib. Hmmmm. A bit hard for a lazy coder!)
I think what you should do is to include that ".cpp" file in a library (.DLL). Then you can statically load it in your project files by including that library (.LIB) and the header file of the library that exports the functions (.H) into the projects that are using the library. That way you will statically load your (.DLL) into memory using the (.LIB) file and the functions will be available through the (.H) file that you will create when making your library.
I know saying this was is not that obvious, especially if you haven't done it in the past. If you want, you can upload your project files onto some location and give the link. And I can give you a better description of what you need to do.
Be warned I am new to C++.
When I compile my program I notice that there is an .obj created for every class that I have. When I looked at other programs in my program files, I realized that these programs barely had any .obj's so I guess that I am doing something wrong.
Also if I deleted the obj files from the release directory and tried to run the exe again and it still worked exactly as before, I know these files can't be pointless but...
What is their use? Should it make them for every class file? - if not how do I resolve this?
How do I put them in one directory like a folder called obj when I compile?
.obj files (.o files on Linux/Unix) are compiled source files, there will indeed be one for each .cpp file, or more formally "compilation unit". They are produced by the compilation phase of building a project.
These .obj files are then combined by linker either to an application, usually .exe file on Windows, or a library file, which on windows can be a .dll for dynamic library, or .lib for static library (which is basically a collection of .obj files in one packed into one file, see below). On Unix-like platform application usually has no extension, and dynamic library has .so extension, and static library has .a extension.
You do not see .obj files or static .lib files with programs, because they are not needed at runtime, they are used only by linker. When you run the linking phase of building a project, linker combines all the needed .obj files into .exe and .dll files (or equivalent), which are used at runtime.
Obj files are generated after compiling. The compiler generates them with many information. Then the linker generates an executable with other files, thus those OBJ files are not necessary anymore.
A very extended answer can be found in any C++ book.
There is no problem. But if you delete them you will force your compiler to compile some files that had no changes but have no OBJ file anymore. Be aware of that.
Just forget about them if you are still working in your code.
Object files are generated by compiling your code. They take your code and convert it to machine code so that the computer can understand and implement your solutions. Once the object files have been generated (a object file is generated for every .cpp file), all the relevant object files are used by the compiler to build a executable file. The executable can then be run independant of the object files, and the object files may be deleted. If another executable were to be created, object files for the relevant code would be necessary again.
Hope it helps!
I was thinking: when I produce a DLL with Visual Studio (C++) it generates
a .dll file
a .lib file
And I have a .h file
So, why not developing directly a static .lib library?
For example, why Office doesn't have .lib files?
And, in the future, if I change the DLL, will I have to send to all the machines also the new .lib file and .h file?
The .h and .lib is only for developers. Whoever writes a program to use the DLL.
Those who just execute the application need only the .DLL.
So if you release a new version, you send DLL to users and the triplet to developers. Unless you changed the public interface (the exports), the old clients will be happy to use the updated DLL without any work.
If you instead build a static .lib, every client must rebuild his binaries.
The .lib file and the header file is the static part of your dynamic library.
You need the .lib and header file in order to compile and link a program so that it uses your library.
So why not use a static library?
There are multiple reasons. One would be that if you use a static library every change of your library causes a recompile of your program. Another that the size of your program will increase. And some more.
So for your second question the lib file are useless for a user of your program. In case of office. As long as you don't have the sources and a compiler the lib files will not help.
For your last question. The answer is also simple. No you don't need to distribute the .lib files. You can replace the dll files with new versions as long as the interface stays the same.
This is a SHORT version that tries to answer your specific questions. The subject of "how DLL's and shared libraries work" is quite a few pages long, and I'm just not going to write that all as an answer.
To start with the parts the compiler generats, a .lib and a .dll file, are used as follows: The .lib file contains "stubs" that go into .exe file using the .dll, and contain "where to find the different functions" in the .dll file. So this is used only when building the .exe file. Similarly, the .h file is used when compiling the .dll and .exe file, and aren't used once you have the final binaries (.exe and .dll files) that make up the product.
The purpose of using DLL's over static linking is most important when there are multiple executables that use the same .dll file. If there is only one "user" of a DLL then the the benefits are reduces, but there are still some benefits (such as compartmentalisation, ability to supply updates to only that part of the code, plugins, etc).
Assuming your .dll is part of a program that contains only binaries, no source code, then you only need to distribute the new .dll [as long as the functions haven't changed in such a way that the .exe or other .dll's that use this .dll has to change, of course].
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.