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

Is it possible to export a C++ member method with C linkage in a DLL?

Consider this in a cpp file: struct someStruct{ public: extern "C" __declspec(dllexport) int sumup(); } someStruct; extern "C" __declspec(dllexport) int someStruct::sumup() { return 0; } This does not compile: error: expected…
Juergen
  • 3,489
  • 6
  • 35
  • 59
-2
votes
1 answer

File not found AS3

I am writing a program in AS3. It's exact contents don't seem to matter here. I basically have 4 classes, split in 4 files. Thing is, it can't find one of these files, and I can't understand why. I have checked it several times and even had my…
MKII
  • 892
  • 11
  • 36
-2
votes
2 answers

c extern undefined reference

I'm sorry for what I'm sure is a simple mistake. But after a few hours I can't figure out what I'm doing wrong. I understand that extern needs to be declared outside a function and defined within a function. But I can't get it to work. Here is my…
Akim
  • 147
  • 1
  • 8
-2
votes
1 answer

How to define a "global" struct?

Let say I've decleared this within MyTools.h #ifndef _MYTOOLS_ #define _MYTOOLS_ typedef struct { // const double LN20; double LN40; // methods double NoteToFrequency(int noteNumber); } Tool; extern const Tool tool; #endif //…
markzzz
  • 47,390
  • 120
  • 299
  • 507
-2
votes
1 answer

Visual Studio C++ links unused globals

I have mylib.lib: //mylib.h: int foo(); //mylib.cpp: #include "mylib.h" const int arr[] = {1, 2}; int foo() { return arr[0]; } And proxylib.lib: //proxylib.h: int bla1(); int bla2(); //proxylib.cpp: #include "../mylib/mylib.h" #include…
-2
votes
1 answer

C Linkage Terminology Differences

There is static linkage, dynamic linkage. What is type where it imports compiled functions from a library and compiles it into the binary?
B. Nadolson
  • 2,988
  • 2
  • 20
  • 27
-3
votes
0 answers

Can't link against some Windows SDK functions (e.g. DCompositionCreateDevice)

I get a linkage error 'LINK2019: unresolved external symbol' of the DCompositionCreateDevice function from dcomp.h header, which I believe is a part of Windows SDK and must be reachable out-of-box. Just trivial using of this function leads to the…
-3
votes
1 answer

why making unit static makes internal linkage

Can anyone explain why making function static in C++ makes it internally linked only. Is that just from plane standard or some language trick?
-3
votes
2 answers

Two files using each other funtions - how to solve it?

I have two headers with source files, lets say file1.h, file1.cc and file2.h, file2.cc. They use each others functions, for example: file1.h: void test1(); file2.h: void test2(); file1.cc: #include "file1.h" #include "file2.h" void test1() { …
Bluerain
  • 9
  • 4
1 2 3
42
43