Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are C++ OBJ files important?

Tags:

c++

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?

like image 946
DanielCollier Avatar asked Jan 15 '14 14:01

DanielCollier


People also ask

What are OBJ files in C?

An Object file is the compiled file itself. There is no difference between the two. An executable file is formed by linking the Object files. Object file contains low level instructions which can be understood by the CPU. That is why it is also called machine code.

What is the purpose of OBJ?

An OBJ file (. obj) contains information about the geometry of 3D objects. The files are used for exchanging information, CAD, and 3D printing. OBJ files can support unlimited colors, and one file can define multiple objects.

What are .OBJ files C++?

C++ Object File (*. obj, *.o) Go Up to File Types Index. A C++ object file is an intermediate file produced by a C++ compiler from a C++ implementation file and the C++ header files that the implementation file includes. The C++ linker produces the output executable or library of your project from your C++ object files ...

Why are objects used in files?

A file object allows us to use, access and manipulate all the user accessible files. One can read and write any such files.


3 Answers

.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.

like image 85
hyde Avatar answered Oct 31 '22 02:10

hyde


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.

like image 21
Darkgaze Avatar answered Oct 31 '22 00:10

Darkgaze


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!

like image 22
Cornel Verster Avatar answered Oct 31 '22 02:10

Cornel Verster