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!
Related
I use netbeans and notice that in addition to creating the .o files, it also creates .o.d files. What are these files (.o.d) for?
As described by Netbeans: Dependency files.
Build systems try to make it so that if you recompile, you save effort by not recompiling translation units whose generated object file will be identical. So it only attempts to recompile files that have changed, or files that maybe haven't changed but a file they depend on has changed. The .o.d files track which files the .o depends on so that it can quickly determine what needs to be recompiled if you build your project again.
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.
I have a small library project that uses OpenGL (glfw and glew). Now, the project compiles fine, but when I create a new project and statically link the library project, VS starts to throw errors in the library project. Why is that?
More specifically, I get this error:
error C1083: Cannot open include file 'GL/glew.h': No such file or directory (file: trenums3d.h)
The project setup is like this: There's the library project 'Foo', which is compiled into a static library ('Foo.lib'). The application project 'Bar' links 'Foo' (I added the folder where Foo.lib resides to Bar's 'Additional Library Directories', as well as the source folder of 'Foo' to Bar's 'Additional Include Directories'). If I compile only the library project, everything works just fine, but compiling the whole solution give me the aforementioned error.
This isn't a proper answer to your question, but just an explanation of the steps required for building an application in a compiled language.
Building a project containing multiple files is a three-step process:
Creation and editing of source and header files
Compilation of the source files (this step contains many sub-steps). This step creates object files of all translation units
Linking of all object files and libraries to form the final executable
Error like the one shown in your question is emitted in the second step. Linking with libraries happens in a completely different step, and is usually done by a different program than the compiler.
To answer your question, if linking with a static library also requires linking with the other libraries that the static library depend on, then the answer is normally yes. Static libraries only contain the function in the actual libraries, you can look at a static library more as a collection or archive of object files. Static libraries does not contain any information about other libraries they depend on.
And as for your problem, with the pre-processor error, it's because you include a header file from your static library, and that header file in turn includes some header files. But the pre-processor doesn't have the secondary included header files in its default search path, so you need to add it.
This still have nothing to do with linking any library, this is a pure pre-processor issue, and is handled in step two in my list above.
I suspect the header files of your static library look like somewhat this:
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "GL/glew.h"
// ...
#endif
If you include this header file from another library or application, the compiler will open this file and will see that it needs to open GL/glew.h as well in order to be able to "understand" the definition of your class.
This means you need to supply at least the header files of glew. The only way to get rid of this is if you manage to only reference glew files from your .cpp files but not from your .h files. In some cases, forward declarations can be used, but not sure if this will work for glew.
Concerning the linker settings: In case your glew library is built statically as well, you may or may not have to supply that library file and link to it from your project. This depends on how you setup your linker for your own static library. If you have troubles in this step, create a new question.
I am new to Linux environment, and I just started using gcc. We have small project that has 7 to 8 cpp files. When I try to compile my files, I wonder why there is .o file for every .cpp file. There is only one main.
This is the way compilation works. Each translation unit gets turned into object code ( the .o files) then the object files get linked together and with other libraries into the final executable file. Normally the build system hides object files somewhere, but you're probably using a very simple build system or script which doesn't bother.
.o is an object file created during compilation of each cpp.
for more info Why Compile to an Object File First?
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.