can you give me a few examples of how different cpp files are communicated in one main cpp, in c++?
In projects bigger than a simple Hello World more .cpp (technically called "translation units") are written, to separate logically the parts of the application and to decrease compilation times.
Notice that the different cpp files are not merged in a single main .cpp (as I suppose you think from your question), but each .cpp is compiled on its own; the compiler generates an object module (usually .o or .obj) for each .cpp, and then it's the linker is called to link together such modules to produce the final executable.
How to make this work? In little projects where you call the compiler by hand, you may just call the compiler specifying all the .cpps on the command line and let it recompile everything and call the linker by itself.
When the program grows bigger, you usually delegate the task of calling the compiler only on the modified files and then run the linker to some kind of utility: many people use Makefiles, many just use an IDE that manages all the files in a project and automagically invokes the compiler and the linker just pressing a button. Actually it's quite uncommon to manually invoke the compiler and the linker in separate steps.
To make the communication between the various modules possible, C++ allows to just declare functions/classes/variables with prototypes/extern declarations (which usually is done in header files) without actually defining them in the current translation unit. This lets the compiler check the syntax and emit the code for the procedure calls, and instructs the linker to look for these "missing pieces" in the other object modules.
What is usually done is to associate to each .cpp a .hpp (or .h if you are old-fashioned :) ) header which contains all the prototypes and declarations relative to its .cpp that should be accessible from the other modules. In this way, if A.cpp needs to call a function defined in B.cpp, it can simply include B.h.
A quick example may be:
A.hpp
#ifndef A_CPP_INCLUDED
#define A_CPP_INCLUDED
// ^^^ these are header guards, used to avoid multiple inclusions of the same header
// Declarations
// Notice that this does not define a global variable, it just says to the compiler "in some module there's a global named in this way, let me access it"
extern int AUselessGlobalVar;
int AddFive(int In);
#endif
A.cpp
#include "A.hpp" //it's useful that each .cpp includes its own .hpp, so it has all the prototypes already in place
// Actually define the var
int AUselessGlobalVar;
// Actually define the function
int AddFive(int In)
{
return In + 5;
}
Main.cpp
#include <iostream>
#include "A.h" // now it can access all the functions/vars declared in A.h
int main()
{
std::cin>>AUselessGlobalVar;
std::cout<<AddFive(AUselessGlobalVar);
return 0;
}
By the way, in this blog entry there's a nice description of the classical model for linking, which is quite related to what we're talking about.
Several .cpp files will share a common .h (header) file. It's in the header that everything goes that they need to share, e.g. function prototypes, macros, constants, ...
I hope that's what you were asking about.
Let's say you have your "main" CPP file, which will have your main()
function, a CPP file that does some stuff, and a header file that declares what that stuff is:
main.cpp:
#include "stuff.h"
#include <iostream>
int main() {
cout << do_something(5) << endl;
}
stuff.h:
int do_something(int);
stuff.cpp:
#include "stuff.h"
int do_something(int x) {
return x*x;
}
If you are using an IDE like QtCreator or Visual Studio, you shouldn't have to do anything special.
If you use the command line, you now need to let it know where these extra files are. For example, using GCC:
g++ main.cpp stuff.cpp -o the_program_name
(Assuming your header file is in the same directory as the CPP files)
Now, the program will write to standard output 25
, because it is calling the do_something
function from stuff.cpp
You mean how they share things?
Simply by including the same headers.
精彩评论