Questions tagged [linkage]

Linkage describes how names can or can not refer to the same entity throughout the entire program or a single unit. Linkage is particularly useful in C++.

The static keyword is used in to restrict the visibility of a function or variable to its translation unit. This is also valid in .

A name's linkage is related to, but distinct from, its . The scope of a name is the part of a translation unit where it is visible. For instance, a name with global scope (which is the same as file-scope in C and the same as the global namespace-scope in C++) is visible in any part of the file. Its scope will end at the end of the translation unit, whether or not that name has been given external or internal linkage.

If the name has external linkage, the that name denotes may be referred to from another translation unit using a for that same name, and from other scopes within the same translation unit using distinct declarations. Were the name given internal linkage, such a declaration would denote a distinct entity, although using the same name, but its entity could be referred to by distinct declarations within the same translation unit. A name that has no linkage at all cannot be referred to from declarations in different scopes, not even from within the same translation unit. Examples of such names are parameters of functions and local variables. The details differ between C (where only objects and functions - but not types have linkage) and C++ and between this simplified overview.

Static Linking

In static linking, the compiled functions are stored into the executable or dynamic library (if you're creating one).

Dynamic Linking

In dynamic linking, the compiled function is stored in a separated library (DLL in Windows or shared object in Linux). A small piece if code is added to the executable to load that shared library at runtime and map the public functions and variables within it.

639 questions
23
votes
4 answers

Can a variable be declared both static and extern?

Why the following doesn't compile? ... extern int i; static int i; ... but if you reverse the order, it compiles fine. ... static int i; extern int i; ... What is going on here?
user1252446
22
votes
5 answers

static vs extern "C"/"C++"

What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
22
votes
1 answer

constexpr global constants in a header file and odr

Unfortunately, I am somewhat confused about constexpr, global constants declared in header files, and the odr. In short: Can we conclude from here https://isocpp.org/files/papers/n4147.pdf that constexpr MyClass const MyClassObj () { return MyClass…
JohnB
  • 13,315
  • 4
  • 38
  • 65
20
votes
2 answers

C++ standard: do namespace-scoped constexpr variables have internal linkage?

Imagine we have a header foo.h containing the following: #ifndef FOO_H_ #define FOO_H_ namespace foo { constexpr std::string_view kSomeString = "blah"; } #endif // FOO_H_ Is foo::kSomeString guaranteed to have internal linkage in any translation…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
19
votes
2 answers

What happens when non-static function declaration follows static function declaration?

The following compiles: static int foo() { return 1; } int foo(); But, will it always compile? Is the behavior in this case well defined? And what does it means when a non-static prototype follows a static declaration?
user4209701
19
votes
3 answers

Is it legal C++ to declare main as extern "C"?

Being a low-level programmer, I often work with the module startup code for executables, so I understand pretty well how code like "crt0" work. When writing C++ code, I've generally declared main as extern "C" to match what the C startup code is…
Myria
  • 3,372
  • 1
  • 24
  • 42
18
votes
2 answers

Why do inline functions have external linkage by default?

The standard says that given a declaration of inline void foo(); that foo is an inline function with external linkage (because by default all function declarations have external linkage). This strikes me as odd. because the one definition rule…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
18
votes
3 answers

External, internal and no linkage or why this does not work?

According to C standard: In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. …
user6898756
  • 183
  • 6
18
votes
3 answers

Difference between internal and no linkage

Please refer to the following code that is in the same translation unit: static int global_var; // file scope in C and global namespace scope in C++ // internal linkage void f(void) { static int local_var; // block scope…
zephon
  • 310
  • 2
  • 8
18
votes
1 answer

What exactly do I lose when using extern "C" in C++?

I'm trying to develop a dynamic library in C++ to be called by an existing program written in IDL (Interactive Data Language). I know that I need to use extern "C" to disable name mangling so that IDL can call the functions it needs (the rest of the…
user1871183
  • 439
  • 5
  • 11
17
votes
4 answers

Interface to C++ objects through extern "C" functions

Can an extern "C" function accept or return C++-specific data types, such as references, pointers-to-members, or non-POD classes (by value)? I cannot find anything in the C++ standard that forbids this. Logically, I would expect the standard to say…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
17
votes
3 answers

Linkage of symbols within anonymous namespace within a regular namespace

In C++, putting a function or a variable in an anonymous namespace makes its linkage internal, i. e. the same as declaring it static on a file-level, but idiomatic C++. What about an anonymous namespace within a normal namespace? Does it still…
Alex B
  • 82,554
  • 44
  • 203
  • 280
16
votes
1 answer

Can a lambda have extern "C" linkage?

This seems to work on the platforms I have tried: #include // extern "C" linkage extern "C" void foo(void (*fn_ptr)(int)); namespace { struct bar { static void f(int); }; } int main() { // Usually works on most platforms, not…
Flexo
  • 87,323
  • 22
  • 191
  • 272
16
votes
1 answer

static keyword in h file and internal linkage

Yet another static question. I have read the following: What are static variables? file scope and static floats http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx And I still fail to understand the following behavior: I have one h file: //…
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
16
votes
4 answers

Why does the extern “C” group C++ class (not header) here?

I was searching SVM libraries and encountered BudgetedSVM. In the source code, I found an unusual usage, just like this: #sample.h #ifndef SAMPLE_H #define SAMPLE_H //no header included or namespace declared here #ifdef __cplusplus extern "C"…
foo
  • 398
  • 1
  • 16
1
2
3
42 43