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
16
votes
2 answers

Contradictory results between GCC and clang related to [basic.link]/7 in the C++ Standard

This snippet compiles in clang, namespace A { void f() { void g(); g(); } } void A::g() { } but GCC only accepts the code if g is defined inside the namespace A as follows: namespace A { void f() { void g(); …
Leon
  • 868
  • 4
  • 12
16
votes
1 answer

Are methods of templated classes implied inline linkage?

Are methods of templated classes implied inline linkage (not talking about the inline optimization), or is it just templated methods which are? // A.h template class A { public: void func1(); // #1 virtual…
David
  • 27,652
  • 18
  • 89
  • 138
15
votes
2 answers

Can you have two classes with the same name and the same member function in different translation units?

Suppose I have two translation units: //A.cpp class X { }; //B.cpp class X { int i; }; Is the above program well-formed? If not, no further questions. If the answer is yes, the program is well-formed (ignore the absence of main), then the…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
15
votes
4 answers

const variables in header file and static initialization fiasco

After reading a lot of the questions regarding initialization of static variables I am still not sure how this applies to const variables at namespace level. I have kind of the following code in a header file config.h generated by the build…
Hannah S.
  • 3,081
  • 3
  • 20
  • 27
14
votes
6 answers

Do classes have external linkage?

I have 2 files A.cpp and B.cpp which look something like A.cpp ---------- class w { public: w(); }; B.cpp ----------- class w { public: w(); }; Now I read somewhere (https://en.cppreference.com/w/cpp/language/static) that classes have…
Vivek
  • 143
  • 1
  • 4
13
votes
1 answer

Declare a C++ function that has C calling convention but internal linkage

I'm trying to interface with a C library, which expects me to provide a pointer to a callback function. As I understand it, according to the standard the callback must have C language linkage, due to possibly different calling convention. I can…
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
13
votes
2 answers

C++ Warning: anonymous type with no linkage used to declare variable

I see this warning message when compiling (gcc 4.6.3, ubuntu) the example: struct { } a; int main() { } warning: anonymous type with no linkage used to declare variable ‘ a’ with linkage [enabled by default]. GCC does not give…
ROTOGG
  • 1,106
  • 1
  • 7
  • 17
12
votes
2 answers

Will a function declared inside main() have external linkage or none linkage?

See the following code: /* first file */ int i; /* definition */ int main () { void f_in_other_place (void); /* declaration */ i = 0 return 0; } /* end of first file */ /* start of second file */ extern int i; /* declaration */ void…
LocalHost
  • 910
  • 3
  • 8
  • 24
12
votes
1 answer

What does the standard say about char arrays as template arguments?

During my research for an answer for this question I found (I did not know that before) that gcc and clang allow char arrays to be template arguments if they are declared static. E.g. this code compiles with gcc and clang: #include…
sebrockm
  • 5,733
  • 2
  • 16
  • 39
12
votes
1 answer

Are all constexpr variable implicitly inline?

I was playing around with auto template parameters and I was surprised that this code didn't compiled: constexpr auto bar = 2; template struct Foo { auto operator()() const { return T; } }; int main() { Foo b; …
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
12
votes
1 answer

How do I set the WebKit version used by a WebView...?

I'm using WebKit in an OS X app via the JUCE WebBrowserComponent, a lightweight wrapper around Apple's WebView Objective-C class. I'm compiling on OS X 10.12 with a deployment target of 10.7. The issue I'm having is that on OS X 10.8, the version…
j b
  • 5,147
  • 5
  • 41
  • 60
12
votes
2 answers

Error in calling a static function in a namespace

I am getting the following error: x.h:3:13: warning: ‘int X::foo()’ used but never defined /tmp/ccK9qSnq.o: In function `main': main.cpp:(.text+0x7): undefined reference to `X::foo()' collect2: error: ld returned 1 exit status while building the…
Deepak
  • 1,038
  • 5
  • 17
  • 44
11
votes
2 answers

No linkage at block scope?

Do all variables declared in a block have 'no linkage'? For example: 1: If I declare a static variable: void foo() { static int i; } Would it have an internal linkage or no linkage? If no linkage, then why make it static? 2: What happens if I…
user103214
  • 3,478
  • 6
  • 26
  • 37
11
votes
1 answer

undefined reference issue with latest gcc

I have link-time error when trying to compile following code with gcc 12.1.0. With clang, msvc and older gcc it compiles as expected. template void def() {} template> void bar() { …
11
votes
1 answer

C++/CLI->C# error C2526: C linkage function cannot return C++ class

I have a simple .NET dll built with VS2010 C# that exposes 2 static members of a class public class Polygon { public static void Test(int test) {} public static void Test(List test) {} } I then created a Console app from VS2010 C++ and…
PeskyGnat
  • 2,454
  • 19
  • 22
1 2
3
42 43