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
2101
votes
18 answers

What is the effect of extern "C" in C++?

What exactly does putting extern "C" into C++ code do? For example: extern "C" { void foo(); }
Litherum
  • 22,564
  • 3
  • 23
  • 27
150
votes
11 answers

Why do we need extern "C"{ #include } in C++?

Why do we need to use: extern "C" { #include } Specifically: When should we use it? What is happening at the compiler/linker level that requires us to use it? How in terms of compilation/linking does this solve the problems which…
Landon
  • 15,166
  • 12
  • 37
  • 30
69
votes
10 answers

Determining C executable name

When we are compiling a C program the output is stored in a.out. How can we redirect the compiled output to another file?
arun
52
votes
3 answers

Static functions declared in "C" header files

For me it's a rule to define and declare static functions inside source files, I mean .c files. However in very rare situations I saw people declaring it in the header file. Since static functions have internal linkage we need to define it in every…
miguel azevedo
  • 537
  • 1
  • 4
  • 5
50
votes
8 answers

About inconsistent dll linkage

How can I remove this link warning? You can see code segment that causes this warning. static AFX_EXTENSION_MODULE GuiCtrlsDLL = { NULL, NULL }; //bla bla // Exported DLL initialization is run in context of running application extern "C" void…
baris.aydinoz
  • 1,902
  • 2
  • 18
  • 28
44
votes
1 answer

What are ld-linux.so.2 and linux-gate.so.1?

When I run ldd program I get an output of the form linux-gate.so.1 => (0xb77ae000) libstdc++.so.6 => /lib/libstdc++.so.6 (0xb76bc000) libm.so.6 => /lib/libm.so.6 (0xb7691000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7674000) …
e271p314
  • 3,841
  • 7
  • 36
  • 61
40
votes
7 answers

What does mean for a name or type to have a certain language linkage?

According to (c) ANSI ISO/IEC 14882:2003, page 127: Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur…
artyom.stv
  • 2,097
  • 1
  • 24
  • 42
39
votes
3 answers

Difference between constexpr and static constexpr global variable

In the C++11 standard, what is the difference between constexpr and static constexpr global variables when defined in a header? More specifically, when multiple translation units include the same header, which declaration (if any) is guaranteed to…
Danra
  • 9,546
  • 5
  • 59
  • 117
30
votes
6 answers

Struct vs. Function Definitions in Scope

So, as far as I know, this is legal in C: foo.c struct foo { int a; }; bar.c struct foo { char a; }; But the same thing with functions is illegal: foo.c int foo() { return 1; } bar.c int foo() { return 0; } and will result in…
V0ldek
  • 9,623
  • 1
  • 26
  • 57
29
votes
4 answers

c & c++ default global variable linkage, multiple declaration & definition problem

For example: code1.c / .cpp int a; // ... and so on code2.c / .cpp int a; int main(void) { return 0; } go to compile: $gcc code1.c code2.c # this is fine $ $g++ code1.cpp code2.cpp # this is dead /tmp/ccLY66HQ.o:(.bss+0x0): multiple…
Bossliaw
  • 698
  • 1
  • 10
  • 23
29
votes
6 answers

Why can't templates be within extern "C" blocks?

This is a follow-up question to an answer to Is it possible to typedef a pointer-to-extern-“C”-function type within a template? This code fails to compile with g++, Visual C/C++, and Comeau C/C++ with basically the same error message: #include…
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
27
votes
3 answers

Clarification on difference in ODR rules for structs in C and C++

I am aware of how ODR, linkage, static, and extern "C" work with functions. But I am not sure about visibility of types since they cannot be declared static and there are no anonymous namespaces in C. In particular, I would like to know the validity…
Quimby
  • 17,735
  • 4
  • 35
  • 55
26
votes
2 answers

Dependency Property dependent on another

How can one register a dependency property whose value is calculated using the value of another dependency property? Because the .NET property wrappers are bypassed by WPF at run-time, one should not include logic in the getters and setters. The…
Gregyski
  • 1,707
  • 3
  • 18
  • 26
26
votes
5 answers

Program statically linked to a library but still needs dll to run

There are things that I don't understand when it comes to linking... I'm writing a program using a 3rd party library (the GEOS library). This program has a dependency to geos.lib but still needs geos.dll to run. I read this question, I think I…
undu
  • 2,411
  • 3
  • 21
  • 28
24
votes
2 answers

Doesn't std::piecewise_construct cause a ODR violation?

std::piecewise_construct, defined in , has internal linkage since it is declared constexpr. I wonder if use of std::piecewise_construct in a header can violate ODR. For example: a.hpp #include #include struct point { …
iorate
  • 693
  • 5
  • 6
1
2 3
42 43