Questions tagged [linker]

The linker is part of the toolchain for producing executables from source code written in compiled programming languages. It takes compiled object code in multiple files and produces a single, "linked", executable file from them.

Separately-compiled programming languages such as C and C++ rely on a linker to turn one or more object files and libraries into an executable. The linker resolves undefined symbol references in object files by linking them to symbol definitions in other object files and libraries. The linker also arranges variables and functions to assign them addresses in the final executable,

On Unix-like operating systems the linker is typically named ld and is often invoked automatically by the compiler as the last step in the compilation process.

Books about linkers:

13907 questions
93
votes
8 answers

Replacing ld with gold - any experience?

Has anyone tried to use gold instead of ld? gold promises to be much faster than ld, so it may help speeding up test cycles for large C++ applications, but can it be used as drop-in replacement for ld? Can gcc/g++ directly call gold.? Are there any…
IanH
  • 3,968
  • 2
  • 23
  • 26
93
votes
4 answers

Easy check for unresolved symbols in shared libraries?

I am writing a fairly large C++ shared-object library, and have run into a small issue that makes debugging a pain: If I define a function/method in a header file, and forget to create a stub for it (during development), since I am building as a…
David Claridge
  • 6,159
  • 2
  • 27
  • 25
88
votes
7 answers

shared global variables in C

How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
86
votes
3 answers

Why is statically linking glibc discouraged?

Most of the sources online state that you can statically link glibc, but discourage from doing so; e.g. centos package repo: The glibc-static package contains the C library static libraries for -static linking. You don't need these, unless you…
pzelasko
  • 2,082
  • 1
  • 16
  • 24
86
votes
1 answer

What is the use of .exp and what is the difference between .lib and .dll?

During compilation and linking, what is use of .exp? What is the difference between .lib and .dll? I know that .lib will be used, while linking and .dll will be used when running the program. But what exactly is the difference between .lib and…
Vineel Kumar Reddy
  • 4,588
  • 9
  • 33
  • 37
83
votes
4 answers

Linking against an old version of libc to provide greater application coverage

Linux binaries are usually dynamically linked to the core system library (libc). This keeps the memory footprint of the binary quite small but binaries which are dependent on the latest libraries will not run on older systems. Conversely, binaries…
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86
81
votes
3 answers

Why does the order of '-l' option in gcc matter?

I am trying to compile a program which uses udis86 library. Actually I am using an example program given in the user-manual of the library. But while compiling, it gives error. The errors I get are: example.c:(.text+0x7): undefined reference to…
user1129237
80
votes
3 answers

Multiple definition of ... linker error

I defined a special file: config.h My project also has files: t.c, t.h pp.c, pp.h b.c b.h l.cpp and #includes: in t.c: #include "t.h" #include "b.h" #include "pp.h" #include "config.h" in b.c: #include "b.h" #include…
mazix
  • 2,540
  • 8
  • 39
  • 56
78
votes
9 answers

Override a function call in C

I want to override certain function calls to various APIs for the sake of logging the calls, but I also might want to manipulate data before it is sent to the actual function. For example, say I use a function called getObjectName thousands of times…
dreamlax
  • 93,976
  • 29
  • 161
  • 209
78
votes
4 answers

error LNK2005: xxx already defined in MSVCRT.lib(MSVCR100.dll) C:\something\LIBCMT.lib(setlocal.obj)

I'm using DCMTK library for reading Dicom files (Image format used in medical image processing.) I'm having a problem in compiling this DCMTK source code. DCMTK uses some additional external libraries (zlib, tiff, libpng, libxml2, libiconv). I know…
volpack
  • 835
  • 1
  • 8
  • 7
75
votes
2 answers

How to specify non-default shared-library path in GCC Linux? Getting "error while loading shared libraries" when running

There is a laptop on which I have no root privilege. onto the machine I have a library installed using configure --prefix=$HOME/.usr . after that, I got these files in ~/.usr/lib : libXX.so.16.0.0 libXX.so.16 libXX.so libXX.la libXX.a when I…
Jokester
  • 5,501
  • 3
  • 31
  • 39
75
votes
2 answers

g++ linker: force static linking if static library exists?

I've a program which links to many libraries. g++, by default, prefers to link to shared libraries, even if the corresponding archive exists. How can I change this preference to prefer static archives over dynamic libraries, if a static archive…
kumar
  • 2,696
  • 3
  • 26
  • 34
75
votes
4 answers

nm: "U" The symbol is undefined

When I nm on one of my libs: nm libmylib.so I get a line like this U _ZNSs4_Rep20_S_empty_rep_storageE@@GLIBCXX_3.4 I checked the man page for nm and I got "U" The symbol is undefined. What does an undefined symbol really mean? If…
bbazso
  • 1,959
  • 6
  • 22
  • 30
74
votes
9 answers

CMake: use a custom linker

I want to setup a custom toolchain with CMake. I've set the compiler but I don't know how to set the linker. This error is reported because CMake try to use the compiler to link: The C compiler "xgcc.exe" is not able to compile a simple test…
Breezeight
  • 1,855
  • 2
  • 22
  • 27
73
votes
5 answers

The use of double include guards in C++

So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double guard is as follows: Header file, "header_a.hpp": #ifndef __HEADER_A_HPP__ #define…
sh3rifme
  • 1,054
  • 1
  • 11
  • 26