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
5 answers

Linker says undefined function reference

Can Someone Explain this to me? The file a is as follows: file a.c #include #include int fun1(); int main() { fun1(); return 0; } File b is written as: file b.c static int fun1(); int fun1(){ printf("fron…
Duggs
  • 169
  • 1
  • 12
0
votes
0 answers

Forcing linkage to compilation unit

I am working on a simple C++ reflection system to use in my own library. I went for a manual registration by using static member per each class that has side effects in the constructor - it actually registers the class in the system. Everything…
0
votes
1 answer

OpenGL methods not recognized from glew64.lib using Visual Studio 2012 on Windows 8

I coded a simple SDL/OpenGL program which displays a simple triangle in rotation. I first compiled and executed my application as a Win32 application and it works perfectly. But with a x64 configuration (using the configuration manager of Visual)…
0
votes
1 answer

LNK2019 Unresolved Symbol Even When The Linkage Exists

PLATFORM Visual Studio 2010 Proffesional MY PROBLEM I have a solution called solution A. I have projects 'a' (static library containing internal and external header and source files) and 'b' (executable to see if 'a' functions can be used). I…
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
0
votes
1 answer

enum type and its linkage in C

why and how does the following code work? both a and b have external linkage, but can they be declared by types of no linkage? if not, why isn't there conflict between MyEnum_t in a.c and MyEnum_t in b.c? a.h #ifndef _A_H_ #define _A_H_ void…
user1252446
0
votes
0 answers

C++ internal linkage with anonymous namespace: is this really as good as it gets?

In my class Foo, I need to construct an object using a helper function, and - as an arbitrary second function that helps force the structure of code - set a handler. Exposing the definition due to internal linkage seems like the only way to do this,…
Sean
  • 9,888
  • 4
  • 40
  • 43
0
votes
1 answer

why this complicated structure constants is internal linkage

as you know, constants defaults to internal linkage. const int Buf = 1000; // defaults to internal linkage Buf can be defined in a header file, it's visible only within the files where it is defined and cannot be seen at link time by other…
0
votes
1 answer

C2894 without any 'extern'

I have a quite simple code in C++, here it is: namespace Phoenix { template struct Ref { private: T* _instance; public: inline Ref(T* instance) { ... } public: inline Ref(const Ref &reference) { ... } …
Hemel
  • 313
  • 3
  • 13
0
votes
3 answers

Why is use of an array defined in File1 working in File2 (only declared there),even without "extern"?

Here I have two files externdemo1.c and externdemo2.c.In the first file,I have declared and initialized a character array arr at file scope.But I have declared it in the second file externdemo2.c without the extern keyword and made use of it there…
0
votes
1 answer

static library linkage issue using eclipse

I have two projects, one that creates static library and the other one that is using it. The first one, create a file called liboutputdevice.a. When I build it, everything goes OK. Then I have my second project that uses the library above, and…
fgfjhgrjr erjhm
  • 325
  • 1
  • 10
  • 24
0
votes
1 answer

Linkage convention

When a compiler compiles a procedure, I understand that there's a 'Linkage convention' for ensuring that the caller and callee's environments are sorted out fine in this situation. How can a compiler know if different linkages are 'safe'. I would…
Awoken
  • 79
  • 6
0
votes
1 answer

gnuwin32 libxml2 installation : setting several lib and include directories

I am now compiling libxml2 on windows 8 using mingw32. I have downloaded from here ftp://xmlsoft.org/libxml2/ and follow the instruction found in the README file. They instruct to configure make using a javascript called configure.js. We can run the…
Vince
  • 3,979
  • 10
  • 41
  • 69
0
votes
1 answer

Adding an image to stage using actionscript

I am new to adobe flash cs6 and actionscript and am having trouble doing what should be a simple task. I have created a new project and document class that looks like this: package { import flash.display.MovieClip; public class MyClass…
Cooper Cripps
  • 205
  • 2
  • 6
  • 14
0
votes
2 answers

Linking error while using static library linkage

This might have been asked previously, however, I found it only in context of Classes, and this is not the case. Utils.h #ifndef _UTILS_H_ #define _UTILS_H_ #include //is 'x' prime? bool isPrime(long long int x); //find the number of…
BegemoD
  • 39
  • 3
0
votes
3 answers

error LNK2019: unresolved external symbol in a multiple projects solution

I have a visual studio solution with multiple projects. One of them, "MyProject" is a static library (.lib). The project, among many other classes has two classes "A" and "B". A.h: #pragma once class A { public: void foo(); }; A.cpp: #include…
Sanich
  • 1,739
  • 6
  • 25
  • 43