linux软件编译、安装、运行

1 编译

参考 https://askubuntu.com/questions/466651/how-do-i-use-the-latest-gcc-on-ubuntu

1.1 静态链接 libc

  • 不推荐链接 glibc (如 getaddrinfo)。用 alpine 的 musl,pull 官方 docker 后安装 这些
  • 启动 docker 映射本地的源码,在 docker 中编译
    • 用 -pthread 而非 -lpthread,区别
    • gcc 链接时使用 -static。一个复杂的 例子
  • 编译后 strip 程序

1.2 交叉编译

1
2
3
4
5
6
7
8
9
10
11
12
13
# gcc 包含文件查询路径
echo | gcc -E -Wp,-v -
# 加 -Wl,--verbose 来看使用库的路径信息
gcc -o test main.o -ltest -L. -Wl,--verbose
# 查看 so 名字
readelf -a -W elffile | grep SONAME
# 查看可执行文件需要的 so
readelf -a -W elffile | grep NEEDED
# 查看符号 https://stackoverflow.com/questions/34732/how-do-i-list-the-symbols-in-a-so-file
objdump -t Lib1.o
nm -g yourLib.so
# 查看 man 文档
man -l tcpdump.1

对于 rpath 和 sysroot 可参考

Building and cross-compile tutorial

Ubuntu实现树莓派交叉编译(也解释了 build host target)

交叉编译器的命名规则

using-a-shared-library-in-another-shared-library

编译时指定 soname -soname=name,也可用工具修改 so 名字

理清gcc、libc、libstdc++的关系

一张图看懂C/C++核心库

1.3 Windows 编译

安装 cygwin,保留安装程序用于后续添加/删除 package

gcc 编译器安装包的选择

  • cygwin32-gcc-g++ is a compiler for 32 bit cygwin
  • gcc-g++ is the basic 64 bit compiler (you probably must install this one).
  • mingw-gcc-g++ is a 32 bit compiler for native 32 bit Windows
  • mingw64-x86_64-gcc-g++ is a 64 bit compiler for native 64 bit Windows
  • mingw64-i686-gcc-g++ is a 64 bit compiler for native 32 bit Windows

gcc 编译器名称区别,注意 /usr/bin 下的 gcc 使用的是硬连接,可以用 ls -il /usr/bin/*gcc* 确认

  1. gcc is just a hard link to x86_64-pc-cygwin-gcc
  2. x86_64-pc-cygwin-gcc and i686-pc-cygwin-gcc are as you have said the Cygwin compilers, that is to say the compilers that create programs that rely on cygwin1.dll
  3. x86_64-w64-mingw32-gcc and i686-w64-mingw32-gcc are compilers provided by the Mingw-w64 project
  4. i686-pc-mingw32-gcc is a compiler provided by the MinGW project

编译和运行

  • 依赖 cygwin 的程序要将 /bin/cygwin1.dll (c:\cygwin\bin\cygwin1.dll) 添加到 windows 的 path 中,使用 ming 编译的不需要
  • 切换到 d 盘,cd /cygdrive/d

必要时使用编程解决,下载 mingw-w64 的 x86_64-win32-seh,安装参考

2 安装

  • 安装前备份虚拟机快照
  • 修改的配置进行备份,方便出错恢复
  • 官方安装指南是重点,安装时的提示与安装后的帮助也注意。其它的方法 ./configure –help,README and INSTALL 文档
  • 版本不求新,stable 和 LTS 优先
  • 版本太老可以考虑万能的源码安装,方便可以选择 apt 安装。ppa 也可以考虑

2.1 apt 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 通过查找名称和描述找到包,i 表示已安装,也可用 apt
apt search <package>
aptitude search <package>
# 查看包详细信息
apt show <package>
apt-cache show <package>
# 查询已安装和要升级某个软件的版本
apt policy fdisk
aptitude versions fdisk
# 模拟安装和模拟升级
apt-get -s install <package>
aptitude -V -s install <package>
apt-get -V -s upgrade
# 指定版本安装
sudo apt-get install <package>=<version>
# 已安装的软件列表
apt list --installed
# 已安装包的文件列表
dpkg -L <package>
# 下载包的源码
apt-get source <package>
apt-get source <package>=<version>

# 为什么安装和为什么没装,从下往上看
aptitude why <package>
aptitude why-not <package>

2.2 源码安装

源码安装典型步骤

The autogen.sh script generates the configure script (from configure.ac, using autoconf) and any files it needs (like creating Makefile.in from Makefile.am using automake). This requires autotools to be installed on your system, and it must be run when checking out the project from source control (if configure isn’t checked in). People who download source tarballs can usually skip this step, because output of this step is included in source tarballs.

The configure script generates Makefile and other files needed to build. Typically Makefile.in is used as a template to generate Makefile (and config.h.in to generate config.h). This process happens using only standard tools installed on your system, like sed and awk, and doesn’t require autotools to be installed.

The make command builds the software.

The make install command installs it.

3 运行