S
S
sddvxd2017-10-15 05:41:25
C++ / C#
sddvxd, 2017-10-15 05:41:25

Why are prototypes needed in C++?

Good afternoon. I started learning C ++ after PHP and some questions arose:
1) Header files: do they contain prototypes of library functions? (and that's all?)
2) How does the compiler find the necessary built-in functions by prototypes if we do not include the library file in the cpp file, but only include the header file with #include?
3) As I understand it, prototypes in C ++ are needed in order to compile faster?
4) Header files are already compiled code? (object)
5) The same question as in 4, only about libraries
Don't ask me why I need to know, I'll just sleep better I'm
learning from a book, pretty good (Stephen Prata) so please do not send me to smoke manuals or go on coding in php and html, this book does not answer my questions

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2017-10-15
@sddvxd

1) Header files: do they contain prototypes of library functions? (And that's all?)

The content of the header file is simply substituted in the place where #include is written. So you can put anything in there. Traditionally, libraries put class, function, and global variable declarations and macro definitions there.
The compiler does not find them. It's not his job. It simply places calls to the object code that refer to external symbols. When linking object files into an executable, the linker finds all called functions in the libraries that were passed to it for linking.
Prototypes are needed to compile at all. You cannot call a function about which nothing is known at all.
No, these are ordinary text files with source code. The content of the header file is simply substituted in the place where #include is written. Open one for interest and read.
Yes. A static library is an archive of object files. A dynamic library is a collection of object files put together by a linker.

M
Maxim Moseychuk, 2017-10-15
@fshp

1) In the header file, data types, function prototypes, the function body itself, templates can be declared. All this can be declared in cpp.
2) This is what the linker does.
3) This is one of the reasons. The second is the resolution of visibility problems.
For example, try writing mutually recursive functions without prototypes.
4) No
5) No

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question