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?
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.
I am using the QtQuickCompiler to compile my QML files, with qmake project file.
I'd like to check what the C++ looks like but in the build-directory there is only the usual files (.cpp for the resources, the executable, and the object files .o).
Where are generated the .cpp files from the QML before being compiled as object files ?
They are in a hidden directory inside the build-directory named .qtquickcompiler/.
I have a "standard " project in eclipse with source and header files included. Although all the source files within the project are compiled, despite of the fact that these source files are not included by the main.c where is the main routine.
What I want is only one compilation unit (one source file to be compiled) and the other source files to be included #include "some-folder/some-file.c"directly. Not to be compiled separately as it is now.
I tried to remove the path to those files from the Build variables but they are still compiled individually.
Please guide me how to achieve what I want.
I'm not familiar with the C tools and, like others, don't know why you would do this but can you put the other source files in a separate folder that isn't marked as a source folder? The source files in the "non source" folder shouldn't be compiled automatically.
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.
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!