musl 知:构建1.2.3版本

文章目录

  • 1. 获取版本
  • 2. 构建安装
  • 3. 测试

1. 获取版本

主页:http://musl.libc.org/

musl 知:构建1.2.3版本_第1张图片
这里选择 1.2.3 版本进行下载。

2. 构建安装

tar -zxf musl-1.2.3.tar.gz
cd musl-1.2.3
./configure
make -j4
make install

1)configure默认配置 prefix 为 /usr/local/musl,这也是安装的目录,可以用通过 --help 参数了解更多配置
2)make后面的-j4,其中4可以根据CPU数进行调整

完整的构建和安装过程如下:

[maminjie@fedora musl]$ tar -zxf musl-1.2.3.tar.gz
[maminjie@fedora musl]$ ls
musl-1.2.3  musl-1.2.3.tar.gz
[maminjie@fedora musl]$ cd musl-1.2.3/
[maminjie@fedora musl-1.2.3]$ ./configure
checking for C compiler... gcc
checking whether C compiler works... yes
checking whether compiler accepts -Werror=unknown-warning-option... no
checking whether compiler accepts -Werror=unused-command-line-argument... no
checking whether compiler accepts -Werror=ignored-optimization-argument... no
checking whether linker accepts -Werror=unknown-warning-option... no
checking whether linker accepts -Werror=unused-command-line-argument... no
checking for C compiler family... gcc
...
checking whether linker accepts -Wl,--dynamic-list=./dynamic.list... yes
checking whether linker accepts -lgcc... yes
checking whether linker accepts -lgcc_eh... yes
using compiler runtime libraries: -lgcc -lgcc_eh
checking preprocessor condition __ILP32__... false
checking whether compiler's long double definition matches float.h... yes
checking preprocessor condition __FAST_MATH__... false
creating config.mak... done
[maminjie@fedora musl-1.2.3]$ make -j4
mkdir -p lib
mkdir -p obj
mkdir -p obj/crt
mkdir -p obj/crt/x86_64
...
/unistd/setresgid.o obj/src/unistd/setresuid.o obj/src/unistd/setreuid.o obj/src/unistd/setsid.o obj/src/unistd/setuid.o obj/src/unistd/setxid.o obj/src/unistd/sleep.o obj/src/unistd/symlink.o obj/src/unistd/symlinkat.o obj/src/unistd/sync.o obj/src/unistd/tcgetpgrp.o obj/src/unistd/tcsetpgrp.o obj/src/unistd/truncate.o obj/src/unistd/ttyname.o obj/src/unistd/ttyname_r.o obj/src/unistd/ualarm.o obj/src/unistd/unlink.o obj/src/unistd/unlinkat.o obj/src/unistd/usleep.o obj/src/unistd/write.o obj/src/unistd/writev.o
ranlib lib/libc.a
[maminjie@fedora musl-1.2.3]$ sudo make install
[sudo] password for maminjie:
./tools/install.sh -D -m 644 lib/Scrt1.o /usr/local/musl/lib/Scrt1.o
./tools/install.sh -D -m 644 lib/crt1.o /usr/local/musl/lib/crt1.o
./tools/install.sh -D -m 644 lib/rcrt1.o /usr/local/musl/lib/rcrt1.o
./tools/install.sh -D -m 644 lib/crti.o /usr/local/musl/lib/crti.o
./tools/install.sh -D -m 644 lib/crtn.o /usr/local/musl/lib/crtn.o
./tools/install.sh -D -m 644 lib/libc.a /usr/local/musl/lib/libc.a
./tools/install.sh -D -m 755 lib/libc.so /usr/local/musl/lib/libc.so
./tools/install.sh -D -m 644 lib/libm.a /usr/local/musl/lib/libm.a
./tools/install.sh -D -m 644 lib/librt.a /usr/local/musl/lib/librt.a
...
./tools/install.sh -D -m 644 include/values.h /usr/local/musl/include/values.h
./tools/install.sh -D -m 644 include/wait.h /usr/local/musl/include/wait.h
./tools/install.sh -D -m 644 include/wchar.h /usr/local/musl/include/wchar.h
./tools/install.sh -D -m 644 include/wctype.h /usr/local/musl/include/wctype.h
./tools/install.sh -D -m 644 include/wordexp.h /usr/local/musl/include/wordexp.h
./tools/install.sh -D obj/musl-gcc /usr/local/musl/bin/musl-gcc
[maminjie@fedora musl-1.2.3]$ find /usr/local/musl | grep -E "bin/|lib/"
/usr/local/musl/lib/Scrt1.o
/usr/local/musl/lib/crt1.o
/usr/local/musl/lib/rcrt1.o
/usr/local/musl/lib/crti.o
/usr/local/musl/lib/crtn.o
/usr/local/musl/lib/libc.a
/usr/local/musl/lib/libc.so
/usr/local/musl/lib/libm.a
/usr/local/musl/lib/librt.a
/usr/local/musl/lib/libpthread.a
/usr/local/musl/lib/libcrypt.a
/usr/local/musl/lib/libutil.a
/usr/local/musl/lib/libxnet.a
/usr/local/musl/lib/libresolv.a
/usr/local/musl/lib/libdl.a
/usr/local/musl/lib/musl-gcc.specs
/usr/local/musl/bin/musl-gcc

3. 测试

下面写一个打印hello world的c程序,基于安装的 musl 1.2.3 进行执行。

1)程序 test.c

#include 

int main(void)
{
    printf("hello world\n");
    return 0;
}

2)编译、执行

动态链接:

[maminjie@fedora test]$ PATH=$PATH:/usr/local/musl/bin musl-gcc -o test hello.c

[maminjie@fedora test]$ ll -h
total 16K
-rwxrwxrwx. 1 root root  82 Sep 30 00:29 hello.c
-rwxrwxrwx. 1 root root 16K Sep 30 00:32 test
[maminjie@fedora test]$ readelf -d test

Dynamic section at offset 0x2e38 contains 22 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libc.so]
 0x000000000000000c (INIT)               0x401000
 0x000000000000000d (FINI)               0x40115e
 0x0000000000000019 (INIT_ARRAY)         0x403e20
 0x000000000000001b (INIT_ARRAYSZ)       8 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x403e28
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x0000000000000004 (HASH)               0x4002f8
 0x000000006ffffef5 (GNU_HASH)           0x400330
 0x0000000000000005 (STRTAB)             0x400418
 0x0000000000000006 (SYMTAB)             0x400358
 0x000000000000000a (STRSZ)              113 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x404000
 0x0000000000000002 (PLTRELSZ)           72 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x4004d8
 0x0000000000000007 (RELA)               0x400490
 0x0000000000000008 (RELASZ)             72 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x0000000000000000 (NULL)               0x0
[maminjie@fedora test]$ ./test
hello world

静态链接:

[maminjie@fedora test]$ PATH=$PATH:/usr/local/musl/bin musl-gcc -o test -static hello.c
[maminjie@fedora test]$ ll -h
total 19K
-rwxrwxrwx. 1 root root  82 Sep 30 00:29 hello.c
-rwxrwxrwx. 1 root root 19K Sep 30 00:36 test
[maminjie@fedora test]$ readelf -d test

There is no dynamic section in this file.
[maminjie@fedora test]$ ./test
hello world

你可能感兴趣的:(libc,linux,musl)