Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

source file and header in c++

I have a question about source file and header in c++.

I have a header which is used to declare the functions. And I implement these functions in C++.

Now I need to use these functions in other files. Should I include both source file and header file in order to use these functions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The basic compilation model used by (C and) C++ is thus:

  1. You put declarations that are to be shared between source files into header files. The source files that need to access those declarations then include these header files.
  2. When compiling, the preprocessor will (recursively) replace #include directives with the file(s) included. The result is called a compilation unit.
  3. The compiler then compiles these compilation units, one at a time, into object files. (Note: The compiler always only ever sees a single translation unit. It has no access to whatever is declared and defined in other translation units.) Basically, object files are readily compiled code, but all the references to symbols (functions, variables) outside of their compilation unit are still symbolic.
  4. The linker is then passed all object files and links references to symbols across those object files to the symbols' definitions, spitting out an executable if all goes well.

In C++, this is often a bit more complicated in practice (especially due to inlining and templates, but also with features like link-time code generation), but that's the basic principles.

The implications of that are:

  • Source files usually only include header files.
    Since the whole preprocessor magic is just a simple text replacement engine without any knowledge of (C or) C++ and about its purpose in the process described above, it can be bend and and abused to do other things. In this, including source files has been done sometimes to achieve some goal. But it's rare.
  • Symbols can be declared as often as wanted, but must be defined exactly once.
    If a linker can't find a symbol that has been declared (and thus references to it have been accepted by the compiler), it will spit a nasty error message into your face. If it finds multiple definitions it will do likewise.
  • The compiler doesn't care whether a declaration is coming from a header included by the preprocessor or has been written directly into the source file.
    However, if you write declarations directly into source files, the compiler, not being able to "look" into other translation units, can't warn you that they are out of date. If you put declarations into header files instead, it is much easier to keep them in sync with their corresponding definitions, and often the compiler can even diagnose if they don't match.
  • Any project with more than one source file can only be built by several compilation runs (one for each source file) with the linker linking the resulting object files. Often though, IDEs hide that behind their project management.
    If you change a header that is, directly or indirectly, used in many source files, you will have to re-compile most of your project. If you change a source file, you will only have to re-compile that one source files (and re-link the executable, of course).

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...