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

推荐订阅源

G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
V
Visual Studio Blog
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 叶小钗
I
InfoQ
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
P
Proofpoint News Feed
WordPress大学
WordPress大学
小众软件
小众软件
B
Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
量子位
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
Why Are Some Libraries Linked by Default in GCC?
2020-12-10 · via jdhao's digital space

Different behavior of linkage between Clang and GCC.

Different behavior of linkage for C and C++#

For the following program:

#include <math.h>
#include <stdio.h>

int main() {

    printf("e^{1} = %f\n", exp(1));
    return 0;
}

If we compile the program with clang test.c -o test, compilation failed with the following error:

/tmp/test-32c2ea.o: In function `main':
test.c:(.text+0x18): undefined reference to `exp'
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

This is totally expected since we use exp() function here, which is implemented in the shared object libm.so. For clang, the linker will not link against libm.so by default. In order to compile the program, we need to explicitly link libm.so:

One explanation for the separation of libm.so and libc.so is that in the early days of computing, floating point calculation is expensive. People use various workarounds for floating point calculations. It makes no sense to integrate math functions into libc.so. It makes libc.so larger with little benefit. As computer hardware upgrades with time, the separation of libm and libc does not make much sense any more.

However, if we translate the above program to C++:

#include <iostream>
#include <cmath>

int main() {

    std::cout << "e^{1} = " << std:: exp(1) << std::endl;
    return 0;
}

we can actually compile the program without linking against libm:

The output of command ldd test is:

> ldd test
        linux-vdso.so.1 =>  (0x00007ffd009a4000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00002b4d4d13c000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00002b4d4d51f000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00002b4d4d828000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00002b4d4da40000)
        /lib64/ld-linux-x86-64.so.2 (0x00002b4d4cf14000)

libm.so is actually implicitly linked. According to post here, for C++ programs, since libstdc++.so requires libm.so, so libm.so will be implicitly linked even if you haven’t instructed its linkage1.

It is interesting that shared library like libc.so2 is linked even if we haven’t specified that. This behaviour can be disabled by providing the -nostdlib option:

clang -nostdlib test.c -o test

You will now see an error message about undefined printf():

test.c:(.text+0x29): undefined reference to `printf'

Ref:

Different between GCC and Clang on linkage#

You might think that GCC will also fail if we compile the test.c without linking libm, but it is not true. The following command runs without error for gcc 4.8.0 and 7.5.0 on my servers:

This is because GCC uses its built-in implementation of some standard C library functions to produce faster and smaller executables. According to post here, we can use -fno-builtin to disable this behavior. We can also use -fno-builtin-somefunc to disable a single function named somefunc. Take the above test.c for an example, the following command will result in compilation error:

gcc -fno-builtin-sqrt test.c -o test
# adding -lm will fix the compilation errors.
# gcc -fno-builtin-sqrt test.c -lm -o test

Ref: