0

I wonder whether #include also means "use". If not, would you please tell me what the compiler will do with the extra files, included functions? If yes, does this mean they have their memories allocated in the output PE?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marc
  • 53
  • 1
  • 3
  • 2
    Did you find a good book? Or should we recommend you some? – osgx Aug 13 '11 at 10:16
  • 2
    I didn't understand why people downvoted it? How else should one clear his doubts if asking questions recieve downvotes here? – Nawaz Aug 13 '11 at 10:20
  • @Nawaz: [read a good book.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Yakov Galka Aug 13 '11 at 10:21
  • @ybungalobill: That is good. But does it justify downvoting? Many of the questions here can be answered by reading books from that list. – Nawaz Aug 13 '11 at 10:23
  • @Nawaz: I didn't downvote, Yet, I expect a beginner to start by reading a book, not asking everything here. On the other hand, I don't expect someone who already knows C++ to search for a concrete answer among 1000+ pages of TC++PL. – Yakov Galka Aug 13 '11 at 10:31
  • @ybungalobill: I didn't ask the question either. Also, he asked the question, that doesn't mean that he is not reading books. People who read books still ask questions here. If some questions are elementary, doesn't mean it should receive downvotes. Also, many good books do not cover such details which he asked here. Many others do cover, but then the language is not so easy. – Nawaz Aug 13 '11 at 10:35
  • +1 This is a perfectly reasonable and answerable question that meets all of the [guidelines](http://stackoverflow.com/faq). Sure, it might be a little silly or simple for C++ experts, but the point of this site is to join experts with beginners to answer questions. – Mankarse Aug 13 '11 at 11:30
  • 1
    Before I can tell you whether `#include` means "use", I must know what "use" means. I assume you have some formalized, specific meaning in mind because of the quotation marks.... – Karl Knechtel Aug 13 '11 at 14:03
  • Thanks Karl, I understand, I guess all files will be merged into one before entering compiler's toasting machine. what is needed will be used and others are discarded. – Marc Aug 13 '11 at 15:18

3 Answers3

7

#include "file.h" tells the preprocessor to open file.h and merge the content of this file with the current file in which you write #include "file.h".

That is, if you have two files as:

//file.h
extern int x;

//file.cpp
#include "file.h"

int x;
void f()
{
    x = 10;
}

Then preprocessor copies the content of file.h to file.cpp as:

extern int x; //came from file.h

int x;
void f()
{
    x = 10;
}
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

Include means open the file, which name is parameter of include and (virtually) put its text in the current file. Compiler will work in same way as if all files were combined into single one.

So, At most cases, included files are header files. They are used to declare functions, macros, classes, extern variables; so you can include a header file (e.g.file.h) in the several source files (e.g. src1.c, src2.c) and in both sources you will have the same set of functions/classes/extern functions predefined.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Thanks osgx, that means the final file feeding the compiler will be only one ? – Marc Aug 13 '11 at 15:15
  • you will start compiler with single file (like `gcc src1.c`). This command will run: preprocessor(which will combine all included files into single temporary file - translation unit), compiler (which will compile to assembler lang. temporary file with all headers already included), assembler (to translate src1.S file into binary src1.o) and linker (to link src1.o with standard C library). – osgx Aug 13 '11 at 15:25
0

Include just copies the contents of the included file as the first stage of compilation. (The pre-processor). This is usually to add header files but can also be used to include any other sort of file. So it is often used to add files with inline code. Sometimes while developing code you may want to include another file with code in.

#include <header.h>
#include <inlines.inl>
#include "testcode.cpp"
QuentinUK
  • 2,997
  • 21
  • 20