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

When / How does Linux load shared libraries into address space?

When is the address of shared objects specified in programs? During linking? Loading? If I wanted to find the memory address of the system command inside of libc inside of my program I could find it easily in gdb, but what if I don't want to…
Ryan
  • 3,579
  • 9
  • 47
  • 59
51
votes
2 answers

CMake: how to produce binaries "as static as possible"

I would like to have control over the type of the libraries that get found/linked with my binaries in CMake. The final goal is, to generate binaries "as static as possible" that is to link statically against every library that does have a static…
pszilard
  • 1,942
  • 1
  • 15
  • 18
51
votes
3 answers

Best practices for debugging linking errors

When building projects in C++, I've found debugging linking errors to be tricky, especially when picking up other people's code. What strategies do people use for debugging and fixing linking errors?
tsellon
  • 2,396
  • 5
  • 24
  • 33
50
votes
1 answer

GHCi runtime linker issue when using FFI declarations

I have a problem regarding FFI in Haskell and the interactive mode of GHC again. Consider FFISo.hs: {-# LANGUAGE OverloadedStrings #-} module Main where import qualified Data.ByteString.Char8 as B import FFIFun.Foo main :: IO () main = do …
lewurm
  • 1,103
  • 7
  • 19
49
votes
5 answers

C Linking Error: undefined reference to 'main'

I have read the other answers on this topic, and unfortunately they have not helped me. I am attempting to link several c programs together, and I am getting an error in response: $ gcc -o runexp.o scd.o data_proc.o -lm…
Nicole
  • 1,255
  • 2
  • 12
  • 15
48
votes
2 answers

Linking a shared library using gcc

I have a shared library (*.so) created using Real View Compiler Tools (RVCT 3.2) on windows target. Then I try to link this *.so file with my application using gcc on linux system. What is the gcc option to link this shared library with my…
goldenmean
  • 18,376
  • 54
  • 154
  • 211
48
votes
4 answers

Is C++ linkage smart enough to avoid linkage of unused libraries?

I'm far from fully understanding how the C++ linker works and I have a specific question about it. Say I have the following: Utils.h namespace Utils { void func1(); void func2(); } Utils.cpp #include "some_huge_lib" // Needed only by…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
48
votes
2 answers

What does exactly the warning mean about hidden symbol being referenced by DSO?

I have a problem linking some shared library with g++. It gives me a warning like: hidden symbol XXX in YYY is referenced by DSO /usr/lib/... I've read some related questions about particular problems, but I want to understand it in a whole - what…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
47
votes
1 answer

How to set the runtime path (-rpath) of an executable with gcc under Mac OSX?

I want to set under Mac OSX the runtime path of an executable (for the linker) at compile time, such that shared libraries at non-standard locations are found by the dynamic linker at program start. Under Linux this is possible with -Xlinker -rpath…
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
47
votes
1 answer

Out-of-Line Virtual Method

What exactly is a out-of-line virtual method and why does it affect link times? LLVM Coding Standards says If a class is defined in a header file and has a vtable (either it has virtual methods or it derives from classes with virtual methods),…
Daniel Eggert
  • 6,665
  • 2
  • 25
  • 41
47
votes
2 answers

How to link C++ object files with ld

I'm trying to link the output of C++ using ld and not g++. I'm only doing this to learn how to do it, not for practical purposes, so please don't suggest just to do it with g++. Looking at this question, the person gets the same error when they run…
gsgx
  • 12,020
  • 25
  • 98
  • 149
47
votes
4 answers

C++ Shared Library with Templates: Undefined symbols error

I'm trying to link to a shared library with a template class, but it is giving me "undefined symbols" errors. I've condensed the problem to about 20 lines of code. shared.h template class myclass { Type x; public: myclass() { x=0;…
nolk
  • 696
  • 1
  • 6
  • 10
46
votes
9 answers

Very strange linker behavior

This is strange because I was able to get the error below to go away by removing the reference to libm. gcc -o example example.o -Wl -L/home/kensey/cdev/lib -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -L/usr/lib/x86_64-linux-gnu -lm…
Don Wool
  • 1,147
  • 4
  • 13
  • 18
46
votes
4 answers

xcode4: Linker error: Directory not found for option

I'm getting this error for my profile build (debug build was OK). {Directory not found for option '-L../../../Mac/Profile/lib [full path of that directory] Library not found for -lMyLib} When I check the path, the directory exists and the lib file…
akbuildmaster
  • 481
  • 1
  • 4
  • 6
46
votes
5 answers

Query on -ffunction-section & -fdata-sections options of gcc

The below mentioned in the GCC Page for the function sections and data sections options: -ffunction-sections -fdata-sections Place each function or data item into its own section in the output file if the target supports arbitrary sections. The…
Jay
  • 24,173
  • 25
  • 93
  • 141