惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Static and Dynamic Linkage in C++
2021-07-04 · via jdhao's digital space

In C++, we can build a library either statically or dynamically. In this post, I want to summarize how to build a static and a shared/dynamic library1.

Build a static and dynamic library#

Suppose we have the following source and header files.

foo.h:

#ifndef _FOO_H
#define _FOO_H
void foo();

#endif

foo.cpp:

#include "foo.h"
#include <iostream>

void foo(){
  std::cout << "This is foo" << std::endl;
}

main.cpp:

 #include "foo.h"

 int main()
 {
   foo();
   return 0;
 }

Here is how to build a dynamic and static library:

g++ -fPIC -c foo.cpp

# build a shared library
g++ -shared -fPIC -o libfoo.so foo.o

# build a static library
ar rvs libfoo.a foo.o

# build main.o object file
g++ -c main.cpp

Use a static and dynamic library#

To link the static library and main.o to produce an executable, we use:

g++ main.o libfoo.a -o main

The order of object file and library file matters. The following won’t work for static library:

g++ libfoo.a main.o -o main

It will result in the following error:

main.o: In function main': main.cpp:(.text+0x5): undefined reference to foo()' collect2: error: ld returned 1 exit status

It is because the linker process the files from left to right. If we put static library before the object file, there is no unresolved symbols yet, all symbol in the static lib will be dropped. When the object file is processed, the symbol foo() can be resolved since it is the last object that is processed.

According to here, if you pass --start-group to linker via -Wl,--start-group, the linker will search all libraries for unresolved symbol. So the following works:

g++ -Wl,--start-group libfoo.a main.o -o main_static

If we link the dynamic library with main.o, things are different. The following two commands both work:

g++ libfoo.so main.o -o main
g++ main.o libfoo.so -o main

In this case, the order of library and the object file does not matter. According to answer here, if we pass --as-needed option to linker via -Wl,--as-needed, then the library file must follow the object file that depend on it.

g++ -Wl,--as-needed libfoo.so main.o -o main_shared  # will fail with undefined reference error

References#