- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 159字
- 2021-06-10 18:28:03
Specifying linkage
In the previous example, the function is defined in the same source file, so there is internal linkage. If the function is defined in another file, the prototype will have external linkage and so the prototype will have to be defined like this:
extern int mult(int, int); // defined in another file
The extern keyword is one of many specifiers that you can add to a function declaration. For example, the static specifier can be used on a prototype to indicate that the function has internal linkage and the name can only be used in the current source file. In the preceding example, it is appropriate to mark the function as static in the prototype.
static int mult(int, int); // defined in this file
You can also declare a function as extern "C", which affects how the name of the function is stored in the object file. This is important for libraries, and will be covered shortly.