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

How and where to call externally defined struct

file: element.h #ifndef ELEMENT_H #define ELEMENT_H typedef struct Elem { char * itag; char * cont; char * etag; struct Elem * previous; } Element; void printElement ( Element * ); #endif /* ELEMENT_H */ I declared and defined a…
Luis
  • 1,236
  • 5
  • 22
  • 31
-1
votes
1 answer

Is it possible to use a macro defined value in C/C++?

In the LLVM codebase, I see this lines: class LLVM_EXTERNAL_VISIBILITY Function : public GlobalObject, public ilist_node { My LSP (clangd) tells me that LLVM_EXTERNAL_VISIBILITY refers to ///…
-1
votes
1 answer

Difference between C global and local _static_ variables?

My C is quite rusty, please help me out. static int i = 42; int main() { } creates a variable i with global scope and internal linkage. Meaning anyone can refer to it but only entries within the translation unit(.c file) will not break the linker.…
Vorac
  • 8,726
  • 11
  • 58
  • 101
-1
votes
1 answer

does c++ allows hoisting or is it compiler specific ? even if

So i was looking through various questions to prepare for my interview that's up tomorrow and came across extern keyword, i understand that extern keyword specifies to allocate memory for a variable which is part of another program(dont know where…
anyman
  • 9
-1
votes
1 answer

Define variable in one file but it's undefined in another (declared in common header)

I have some functionality which is highlighted in a separate file functionality.c. The code in this file reads thresholds which are in functionality.h: unsigned int thresold1[2]; unsigned int thresold2[2]; void *watcher(void *); Also I have main.c…
NK-cell
  • 1,145
  • 6
  • 19
-1
votes
2 answers

What does this External Linkage directive mean?

Can someone explain this note in C++ primer 5th edition to me: Note The functions that C++ inherits from the C library are permitted to be defined as C functions but are not required to be C functions—it’s up to each C++ implementation to decide…
Itachi Uchiwa
  • 3,044
  • 12
  • 26
-1
votes
3 answers

extern linkage of function in C, is it okay to not be with "extern"?

Hello I'm viewing an example of external linkage of function and variable in C. This is an example that produces random variables. random.c #define SEED 20 #define MULT 3124 #define INC 2345 #define MOD 5436 unsigned int call_count = 0; static…
ssrr00
  • 13
  • 3
-1
votes
1 answer

using variable name with internal and external linkage in one program

B.cpp #include static int x=4; void print_x() { std::cout<< x; } A.cpp #include void print_x(); //forward declaration int x=3; int main () { std::cout<< x << " "; print_x(); } // output 3 4 i run this code…
Abdo Mostafa
  • 65
  • 1
  • 4
-1
votes
1 answer

I'm a student and I can't run program in C (visual studio), when I include a library like stdio.h

I'm in class where we're learning C, and a few weeks ago I noticed whenever I included any library with #include an error takes place. For this simple piece of code: #include #include int main() { printf("Hi");…
-1
votes
1 answer

C++: Using external linkage with an unnamed namespace to use a constant variable

My goal is that when I call myFunction from within main, I do not have to pass the SIZE constant. The function within myFunction.cpp should then run and simply output the contents of the array. I've read some notes on external linkage but I feel…
Cartino
  • 41
  • 1
  • 7
-1
votes
2 answers

C - "Error: Expected identifier or '(' before '[' token"

I am making a rogue-like game in C and I am having trouble with file linkage. I am making a custom header file where I declare an array of structures, but when I compile this code: #ifndef spells #define spells struct spells SpellList[55]; #endif…
octagonlord69
  • 75
  • 2
  • 2
  • 6
-1
votes
2 answers

Why does this not violate One definition rule?

I am new to c++ and trying to understand the one-definition-rule. Will including the below test.h file in multiple c++ files voilate the one definition rule (syspath and tags). If not why not ? #ifndef TEST_H #define TEST_H_ #include…
-1
votes
1 answer

scipy adding a custom linkage function

I am trying to add a custom linkage function to the hierarchical clustering in SciPy. I can't seem to access the implementation which redirects to _hierarchy.linkage(y, n, method_code) in .so compiled file. Any idea on how to go further in a clean…
-1
votes
4 answers

C Pointers to Global Variable: Different Pointers

I have a global variable/buffer defined in a header. I have two source files, a function library (lib.c) and a test bench (tb.c), both include the header. In the library, I fill the global variable buffer, print the pointer value and print some of…
E. Fisher
  • 35
  • 2
-1
votes
1 answer

How to declare identifier with internal-linkage in block-scope without prior declaration of that identifier with some linkage being visible?

Consider this: #include static int b; int main() { { int b; { extern int b; b = 2; } } printf("%d", b); } Here by definition the identifier 'b' which is assigned the value 2…
AnArrayOfFunctions
  • 3,452
  • 2
  • 29
  • 66
1 2 3
42
43