Why do we need to include C or CPP declaration files at compile time and not default libraries like iostream?

If the program is C

or CPP

needs to be compiled with our own header files with declarations and cpp files with definitions, then we need to include the cpp files with the definitions in the compile command (see this answer ). However, when we write #include <iostream>

, we do not include iostream.cpp

in the compilation statement eg g++ main.cpp iostream.cpp -o main

.

If we write our custom declaration files, for example hello.hpp

with a class declaration and hello.cpp

with definitions, we need to compile it with g++ main.cpp hello.cpp -o main

after being included hello.hpp

in the header in the file main.cpp

, Why is this?

EDIT . Is it possible that we can mimic the behavior of the standard templating library for our custom header and cpp files, so that all we have to do is just include the header and the declarations get automatically compiled? If so, how? If not, why not?

+3


source to share


3 answers


The standard libraries are implicitly linked. So

g++ main.cpp -o main

      

really

g++ main.cpp -o main -lstdc++ -lc

      

where libstdc++

is the C ++ libc

standard library and is the c standard library. Other libraries must be explicitly linked to (for example libm

).



This becomes clearer if you separate the compilation and linking steps:

g++ -c main.cpp -o main.o
g++ -c other.cpp -o other.o
g++ main.o other.o /usr/lib/libstdc++.a /usr/lib/libc.a -o main

      

Here, we compile our function definition main()

and other definitions ( other.cpp

) into object files and combine them with the existing compiled function / class / variable definitions in the standard libraries.

See. TLDP page to Creating a shared library and Creating a static library to learn how to determine the files ( .c

and .cpp

) turn into the library.

+5


source


First of all, check the difference between including <filename>

and "filename"

in this post :

For #include "filename", the preprocessor searches in the same directory as the file containing the directive. This method is commonly used to include programmer-defined header files.

For #include <filename>, searches for the preprocessor in the implementation in a dependent manner, usually in search directories pre-designated by the compiler / IDE. This method is commonly used to include standard library header files

The fact that you include the file does not mean that you are compiling the file. In fact, by including, you decide the syntax references to the included file, such as class declarations (in C ++), shared variables, structs, enums, and funcion calls. This will avoid code to get errors in unreferenced objects, breaking the compilation process.

Compiling code with these references does not mean that the original referenced code has been compiled either. Example:

mycode.cpp

includes myclass.hpp

how:

#include "myclass.hpp"

      

If the links are correct, you can compile them using:

g++ mycode.cpp 

      



You will have compiled code for mycode.cpp

(named object file mycode.o

), but not compiled code before myclass

. mycode.cpp

compiled correctly because it has references to objects / functions myclass

/ etc., but that doesn't mean it is myclass

compiled (you don't have an object file yet myclass.o

).

If you link edit mycode.cpp

, there will be a few missing links as there is no myclass

compiled code.

If you choose:

g++ mycode.cpp myclass.cpp 

      

It will generate object files for mycode.o

and myclass.o

, which can later be linked to each other.

In the case of STL libraries, you just need a reference as they are already compiled and available, albeit in a so-called standard library (a set of files .o

already compiled for you). The friend will make sure to tie together then either automatically or if you tell him to do it correctly.

I suggest you go through the process compilation -> object files -> link editing -> executable file

to understand these steps, which only happen for compiled languages.

+1


source


You need to understand that the compiler reads the input files, then preprocesses them, and the preprocessor replaces all #include statements, so the compiler itself never sees them. As far as the real compiler is concerned, there are no #include statements. The compiler then converts the original language to machine code. There is one object file written by the compiler for each input file (c or cpp). Machine code in object files cannot be directly executed; it must be linked with other code.

Initially, programmers link their programs with other object files that are already compiled into object file libraries. Several object files have been linked statically to make an executable, and this can be done for many executables. Thus, in other words, each of the many object files can exist many times, once for each of the many executable files. And that means that when something changes, sometimes many programs using the same object files had to be linked again. Later programmers learned that it helps to dynamically link with object files, and it is a dynamic link library.

When you compile your program, the compiler includes linker instructions for linking to other libraries, mainly the "C Runtime" (CRT). The link can be either a static link or a dynamic link. In addition, the headings provide information about the CRT. Most of the standard libraries (the std namespace) are actually in headers such as "iostream" and are compiled with your program. Anything missing in the header is in the CRT library somewhere.

So the equivalent of iostream.cpp can either be in your own program, like main.cpp, or it's already compiled and is in the CRT library that is linked to your program.

+1


source







All Articles