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
11
votes
1 answer

Why same named extern local variables in different blocks get different linkages between compilers in c++?

While I was just checking which linkages are granted to extern local variables I found that some different behavior between compilers for instance if I tested below code as you see in the comments variable vars have different linkages // foo.cpp int…
hyuk myeong
  • 197
  • 1
  • 13
11
votes
2 answers

extern enum in c++

I have an enum I have declared in some .h file: typedef enum { NONE, ONE, TWO, THREE } MYENUM; in a seperate .cpp I cannot do this: extern enum MYENUM; //works extern MYENUM TWO; //makes sence, TWO is not an INSTANCE of…
Tom Fobear
  • 6,729
  • 7
  • 42
  • 74
11
votes
2 answers

Internal linkage with static keyword in C

I know static is an overloaded keyword in C. Here, I'm only interested in its use as a keyword to enforce internal linkage. If you have a global variable declared in a .c file, what is the difference between using static and not using static? …
Channel72
  • 145
  • 2
  • 5
11
votes
1 answer

Linkage of explicit class template instantiation

Are multiple instantiations of the same class template with the same type allowed in different compilation units? What about function templates? A sample code is as follow: test.hpp template class A { public: T…
nocte107
  • 273
  • 2
  • 10
11
votes
1 answer

Trying to understand §3.3.1/4

Apparently from §3.3.1/4, this snippet doesn't compile because it contains two different entities with the same name A in the global namespace, extern int A; and static int A = 101;. That is, one has external and the other has internal linkage. live…
Wake up Brazil
  • 3,421
  • 12
  • 19
11
votes
2 answers

template non type arguments

$14.3.2 - "... A template-argument for a non-type, non-template template-parameter shall be one of: ...a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a…
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
10
votes
3 answers

Non-extern function with C linkage

Is it possible to declare a function with C linkage without it having external linkage? When trying to compile extern "C" static void f() {} I get f.cc:1: error: invalid use of 'static' in linkage specification which makes sense, in a way. In…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
10
votes
1 answer

How -fvisibility-inlines-hidden differs from -fvisibility=hidden in gcc

According to http://gcc.gnu.org/wiki/Visibility, With -fvisibility=hidden, you are telling GCC that every declaration not explicitly marked with a visibility attribute has a hidden visibility. And -fvisibility-inlines-hidden causes all inlined…
flm8620
  • 1,411
  • 11
  • 15
10
votes
5 answers

Is it possible/safe/sane to pass around a function pointer to a static function?

Let’s say I only want to expose a function from one of my files by passing out a function pointer to that function. Is it safe to declare that function as static? Are compilers allowed to do any judo that would invalidate my function pointer, or…
ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
10
votes
4 answers

extern and extern "C" for variables

I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C". Consider the following code My header file is like this: #ifdef __cplusplus extern "C" int global; extern "C" int…
Thenewstockton
  • 443
  • 6
  • 18
10
votes
3 answers

Are all functions in the c++ standard library required have external linkage?

So I've got an app which compiles fine on windows, linux and a few variations of unix. I recently decided to port it to OSX when I ran into a snag. I have a template which looks like this: template int safe_ctype(unsigned char c) {…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
10
votes
2 answers

C/C++: What is the difference between a statically-linked library and an object file?

I understand that code included in an executable at compile-time can come from object files (.o files) and statically-linked libraries (.lib/.a files). What is fundamentally and conceptually the difference between these two? Why is there a…
user553702
  • 2,819
  • 5
  • 23
  • 27
10
votes
1 answer

variable has internal linkage but is not defined

I have this .h file: namespace{ class Invariant{ public: Invariant(z3::expr e,Instruction *i):Expr(e),I(i){ DenseMap FunMap = Invariants[F]; } private: //static map static DenseMap
Giacomo Tagliabue
  • 1,679
  • 2
  • 15
  • 31
9
votes
1 answer

Implicit internal linkage not the same as explicit internal linkage ("static")?

Today I encountered a pecularity which, although probably not really important, nevertheless puzzles me. Maybe I'm just not understanding C++ correctly, too. Some arrays inside a source file point to string literals, like so: const char* a[] = {…
Damon
  • 67,688
  • 20
  • 135
  • 185
9
votes
5 answers

java.lang.LinkageError: loader constraint violation in Grails project

I've built a Grails project with POI (include poi-3.7 and poi-ooxml-3.7). I've added these 2 external libraries to dependencies block in BuildConfig.groovy file of my project. There's nothing strange when I compiled it. But when I called the command…
Đinh Hồng Châu
  • 5,300
  • 15
  • 53
  • 90