When to use extern in C++

2018-12-31 12:50发布

I'm reading "Think in C++" and it just introduced the extern declaration. For example:

extern int x;
extern float y;

I think I understand the meaning (declaration without definition), but I wonder when it proves useful.

Can someone provide an example?

5条回答
伤终究还是伤i
2楼-- · 2018-12-31 13:08

This is useful when you want to have a global variable. You define the global variables in some source file, and declare them extern in a header file so that any file that includes that header file will then see the same global variable.

查看更多
不流泪的眼
3楼-- · 2018-12-31 13:16

It is useful when you share a variable between a few modules. You define it in one module, and use extern in the others.

For example:

in file1.cpp:

int global_int = 1;

in file2.cpp:

extern int global_int;
//in some function
cout << "global_int = " << global_int;
查看更多
十年一品温如言
4楼-- · 2018-12-31 13:17

It's all about the linkage.

The previous answers provided good explainations about extern.

But I want to add an important point.

You ask about extern in C++ not in C and I don't know why there is no answer mentioning about the case when extern comes with const in C++.

In C++, a const variable has internal linkage by default (not like C).

So this scenario will lead to linking error:

Source 1 :

const int global = 255; //wrong way to make a definition of global const variable in C++

Source 2 :

extern const int global; //declaration

It need to be like this:

Source 1 :

extern const int global = 255; //a definition of global const variable in C++

Source 2 :

extern const int global; //declaration
查看更多
骚的不知所云
5楼-- · 2018-12-31 13:20

This comes in useful when you have global variables. You declare the existence of global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source files.

To clarify, using extern int x; tells the compiler that an object of type int called x exists somewhere. It's not the compilers job to know where it exists, it just needs to know the type and name so it knows how to use it. Once all of the source files have been compiled, the linker will resolve all of the references of x to the one definition that it finds in one of the compiled source files. For it to work, the definition of the x variable needs to have what's called “external linkage”, which basically means that it needs to be declared outside of a function (at what's usually called “the file scope”) and without the static keyword.

header:

#ifndef HEADER_H
#define HEADER_H

// any source file that includes this will be able to use "global_x"
extern int global_x;

void print_global_x();

#endif

source 1:

#include "header.h"

// it needs to be defined somewhere
int global_x;

int main()
{
    //set global_x here:
    global_x = 5;

    print_global_x();
}

source 2:

#include <iostream>
#include "header.h"

void print_global_x()
{
    //print global_x here:
    std::cout << global_x << std::endl;
}
查看更多
何处买醉
6楼-- · 2018-12-31 13:20

When you have global variables you have to declare them extern in any translation unit they're not defined in or you'll get multiple definitions. This is of course to be avoided since globals are generally not good.

When you're writing in C and want to allow C++ users to use your library you declare your stuff in an extern "C" {} block. Since C sucks you shouldn't need this either.

And then there's extern templates, which aren't part of the standard anymore.

Finally, there's declaring a template instantiation that occurs somewhere else and you want to link to it rather than making a new one. You declare those extern also. This has occasional use...maybe...I never have.

I think I can count the amount of times I've needed "extern" in C++ on one hand since I tend to avoid all constructs in which it's needed.

查看更多
登录 后发表回答