开发者

Simple question about header files in C++

Lets say I have 3 files: add.h, add.cpp and main.cpp. This is a common example on C++ training sites where add.h contains the prototype of a function called "add". Add.cpp contains the declaration of the prototype and main.cpp includes add.h and then calls the add() function to add two numbers (x and y) together.

My question is this: No matter what I name add.cpp, my program works just fine. Elephant.cpp works just as well as doctorWhoRules.cpp. Does the compiler search through all 开发者_JAVA百科the local .cpp files to find the given prototype in add.h? I don't declare the name of the .cpp file that contains it anywhere. I'm just confused as to how this works.

Thanks!


I think you are mixing Java with C++. In Java (editors like Eclipse) there is a general restriction that, your class name should same as the file name. i.e.

// Add.java
class Add
{
}

In C++ there are no such restrictions. Your file name can be any meaningful name. The only thing you have to take care is that your implementation files are with .cpp/.cxx/.C etc. extensions (I don't know if modern compilers support other than this also).

Even your header file can be any arbitrary extension of course other than .cpp and all; the more conventional extensions for header files are .h, .hpp etc.


The names are a convention. You could name it anything you like, including "weekly.newsletter". You don't have to use "add.cpp".

That said, it is wise to follow conventions unless you have a reason not to, and some tools will use the extension to infer the language used.

As an example, it's very confusing when people use "foo.cpp" files as if they were header files! Yet, this is allowed in C++. It is, however, against convention, and programming is difficult enough as it is.


Different possibilities;

1) You are not actually renaming the file, you are just saving the file under a new name -- and the old add.cpp is still there and still works.

2) There is an old add.obj hanging around and that is what is getting linked

3) The files are openeds as a project and visual-studio figures out that the files open are really all the file you want to compile into and exe


Yes, the compiler searches all the files. More precisely, it does one pass ("compilation") that reduces your code to machine instructions and adds to each file a list of symbols (function and variable names) it defines globally, and then a second pass ("linking", technically not part of the compiler at all) that matches those names to each other.

The point of the prototypes in the .h file is just to make available to the compiler, when it compiles main.cpp, the exact types and names that are actually defined in add.cpp (or whatever you call it). This makes it possible to do the compilation part without actually seeing the implementation file.

Templates break this "separate compilation" model, though, because the compiler needs to see the entire definition of a template during compilation, since it has to generate class-specific code depending on how the template is called. Generally, templates get put in the .h files and included everywhere.


See above answers for filenames. The linker collects the object files, matches up code etc. and builds your executable.


Lets say we have add.h add.cpp and main.cpp.

You need the prototype of add in both your main.cpp and add.cpp. main.cpp needs it because it wants to use the class add and add.cpp needs it because it wants to implement it, therefore needing its prototype. This is done as you know by including the header file. Doing #include "add.h" in both add.cpp and main.cpp.

Now the compiler comes into action creates two object files, namely the main.o and add.o. So still main only knows about add, the internal mechanics (implementation) are still not there. After that the linker combines main.o and add.o into an executable which has all the bells and whistles the run add from main.

After compiling every object file knows by name. After linking they actually know how to call them by name (where he (the implementation) lives.

Before the compiler we have another step which is called pre processing. One of the things which is done in this step is copying the contents of files which are after #include.

No matter how you call add.h it does not matter, but you have to include the correct file in main.cpp and add.cpp. No matter how you call add.cpp or main.cpp it does not matter only your compiler has to be instructed about how they are called. For example in visual studio this is done automatically, by renaming them in your project file. If using gcc you have to instruct your compiler and linker yourself. In this specific situation you do the following.

compile add.cpp into add.o:

gcc -c add.cpp

compile main.cpp into main.cpp:

gcc -c main.cpp

link add.o and main.o into myniceprogram:

gcc -o myniceprogram add.o main.o
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜