Reputation: 765
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?
Upvotes: 21
Views: 27270
Reputation: 2573
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.
Upvotes: 11
Reputation: 1781
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!
Upvotes: 8
Reputation: 62817
.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.
Upvotes: 55