C++ Header and CPP includes

2019-02-24 22:29发布

问题:

quick question.

I am trying to get C++ nailed down, and today I spent hours with a double definition linker error("this has already been defined!") and I finally realised it's because I had the layout as such:

  • main.cpp

    #include Dog.cpp
    
  • Dog.cpp

    #include Dog.h
    
  • Dog.h

    // (Dog class and prototype of test function)
    

And now that I've cleared that up by including the Dog.h instead of the Dog.cpp in the main.cpp.

By including the .h file, does the .cpp file with the identical prefix get compiled with the program?

I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp. I spent ages Googling but no answers really helped me understand what was going on.

Edit: I forgot to add that I prototyped in the .h, and defined the function for the class in the .cpp and that's what gave me the "already defined" error.

回答1:

By including the .h file, does the .cpp file with the identical prefix get compiled with the program? I was astounded when the program ran with only the .h included and no references whatsoever to Dog.cpp.

No.

Your program is built in phases.

  • For the compilation phase, only declarations are needed in each translation unit (roughly equivalent to a single .cpp file with #includes resolved). The reason that declarations even exist in the first place is as a kind of "promise" that the full function definition will be found later.

    g++ -c Dog.cpp               # produces `Dog.o`
    g++ -c main.cpp              # produces `main.o`
    
  • For the linking phase, symbols are resolved between translation units. You must be linking together the result of compiling Dog.cpp and of compiling main.cpp (perhaps your IDE is doing this for you?), and this link process finds all the correct function definitions between them to produce the final executable.

    g++ Dog.o main.o -o program  # produces executable `program`
    

    (Either that, or you actually haven't got to the link phase yet, and merely have an object file (Dog.o); you can't execute it, partially because it doesn't have all the function definitions in.)

The two phases can be done at the same time, with the "shorthand":

g++ Dog.cpp main.cpp -o program  # compiles, links and produces executable


回答2:

No, the .cpp file does NOT automatically get compiled. You can either do that manually, create a makefile, or use an IDE that has both of them in the same project.



回答3:

You don't specify how you are compiling it. If you are using an IDE and have a new .h and .cpp to the project automatically then it will all be compiled and linked automatically.

There are 2 stages to making an executable to run: compiling and linking. Compiling is where the code gets interpretted and translated into lower level code. Linking is where all of the functions that you used get resolved. This is where you got the duplicate function error.



回答4:

Inclusion does not automatically cause compilation, no.

In fact, the actual compiler never sees the #include statement at all. It's removed by an earlier step (called the preprocessor).

I'm not sure how it could build if you never compiled the Dog.cpp file. Did you reference any objects with code defined in that file?



标签: c++ header main