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 中编译
- 编译后 strip 程序
1.2 交叉编译
1 | # gcc 包含文件查询路径 |
对于 rpath 和 sysroot 可参考
Building and cross-compile tutorial
Ubuntu实现树莓派交叉编译(也解释了 build host target)
using-a-shared-library-in-another-shared-library
编译时指定 soname -soname=name
,也可用工具修改 so 名字
1.3 Windows 编译
安装 cygwin,保留安装程序用于后续添加/删除 package
- 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*
确认
gcc
is just a hard link tox86_64-pc-cygwin-gcc
x86_64-pc-cygwin-gcc
andi686-pc-cygwin-gcc
are as you have said the Cygwin compilers, that is to say the compilers that create programs that rely oncygwin1.dll
x86_64-w64-mingw32-gcc
andi686-w64-mingw32-gcc
are compilers provided by the Mingw-w64 projecti686-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 | # 通过查找名称和描述找到包,i 表示已安装,也可用 apt |
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 运行
- 在 64 位 linux 运行 32 位程序
- 参考 这个 将编译器课程中的 spim 脚本
[${1:0:1} != "-"]
改成两个方括号外加参数判断[[ $# -ne 0 && "${1:0:1}" != "-" ]]