使用 GCC 创建静态链接的可执行文件

Posted

技术标签:

【中文标题】使用 GCC 创建静态链接的可执行文件【英文标题】:Creating statically linked executable using GCC 【发布时间】:2014-11-10 08:59:53 【问题描述】:

我正在为我的一个应用程序创建嵌入式 Mono 存档。根据this 链接,我需要创建 c/c++ 可执行文件,它将实例化适当的框架并将我的托管程序集加载到其中。

我尝试通过在 g++ 命令行中指定 -static 来创建静态链接的可执行文件,但没有成功。下面是我的 g++ 命令的详细输出 -

$>g++  -Wall -o monolauncherstatic.out MonoLauncher.cpp `pkg-config --cflags --libs mono-2` -v -static

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i586-suse-linux/4.7/lto-wrapper
Target: i586-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,fortran,obj-c++,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.7 --enable-ssp --disable-libssp --disable-libitm --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap --with-slibdir=/lib --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --enable-linker-build-id --program-suffix=-4.7 --enable-linux-futex --without-system-libunwind --with-arch-32=i586 --with-tune=generic --build=i586-suse-linux
Thread model: posix
gcc version 4.7.2 20130108 [gcc-4_7-branch revision 195012] (SUSE Linux) 
COLLECT_GCC_OPTIONS='-Wall' '-o' 'monolauncherstatic.out' '-D' '_REENTRANT' '-I' '/usr/lib/pkgconfig/../../include/mono-2.0' '-L/usr/lib/pkgconfig/../../lib' '-v' '-static' '-mtune=generic' '-march=i586'
 /usr/lib/gcc/i586-suse-linux/4.7/cc1plus -quiet -v -I /usr/lib/pkgconfig/../../include/mono-2.0 -D_GNU_SOURCE -D _REENTRANT MonoLauncher.cpp -quiet -dumpbase MonoLauncher.cpp -mtune=generic -march=i586 -auxbase MonoLauncher -Wall -version -o /tmp/cc9htj2d.s
GNU C++ (SUSE Linux) version 4.7.2 20130108 [gcc-4_7-branch revision 195012] (i586-suse-linux)
        compiled by GNU C version 4.7.2 20130108 [gcc-4_7-branch revision 195012], GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/pkgconfig/../../include/mono-2.0
 /usr/include/c++/4.7
 /usr/include/c++/4.7/i586-suse-linux
 /usr/include/c++/4.7/backward
 /usr/lib/gcc/i586-suse-linux/4.7/include
 /usr/local/include
 /usr/lib/gcc/i586-suse-linux/4.7/include-fixed
 /usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/include
 /usr/include
End of search list.
GNU C++ (SUSE Linux) version 4.7.2 20130108 [gcc-4_7-branch revision 195012] (i586-suse-linux)
        compiled by GNU C version 4.7.2 20130108 [gcc-4_7-branch revision 195012], GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 909fa5113dc6c530ae791996b13c020f
COLLECT_GCC_OPTIONS='-Wall' '-o' 'monolauncherstatic.out' '-D' '_REENTRANT' '-I' '/usr/lib/pkgconfig/../../include/mono-2.0' '-L/usr/lib/pkgconfig/../../lib' '-v' '-static' '-mtune=generic' '-march=i586'
 /usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/as -v -I /usr/lib/pkgconfig/../../include/mono-2.0 --32 -o /tmp/ccwER6Jm.o /tmp/cc9htj2d.s
GNU assembler version 2.23.1 (i586-suse-linux) using BFD version (GNU Binutils; openSUSE 12.3) 2.23.1
COMPILER_PATH=/usr/lib/gcc/i586-suse-linux/4.7/:/usr/lib/gcc/i586-suse-linux/4.7/:/usr/lib/gcc/i586-suse-linux/:/usr/lib/gcc/i586-suse-linux/4.7/:/usr/lib/gcc/i586-suse-linux/:/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/
LIBRARY_PATH=/usr/lib/gcc/i586-suse-linux/4.7/:/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/lib/:/usr/lib/gcc/i586-suse-linux/4.7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-Wall' '-o' 'monolauncherstatic.out' '-D' '_REENTRANT' '-I' '/usr/lib/pkgconfig/../../include/mono-2.0' '-L/usr/lib/pkgconfig/../../lib' '-v' '-static' '-mtune=generic' '-march=i586'
 /usr/lib/gcc/i586-suse-linux/4.7/collect2 --build-id -m elf_i386 -static -o monolauncherstatic.out /usr/lib/gcc/i586-suse-linux/4.7/../../../crt1.o /usr/lib/gcc/i586-suse-linux/4.7/../../../crti.o /usr/lib/gcc/i586-suse-linux/4.7/crtbeginT.o -L/usr/lib/pkgconfig/../../lib -L/usr/lib/gcc/i586-suse-linux/4.7 -L/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/lib -L/usr/lib/gcc/i586-suse-linux/4.7/../../.. /tmp/ccwER6Jm.o -lmono-2.0 -lrt -ldl -lpthread -lstdc++ -lm --start-group -lgcc -lgcc_eh -lc --end-group /usr/lib/gcc/i586-suse-linux/4.7/crtend.o /usr/lib/gcc/i586-suse-linux/4.7/../../../crtn.o
/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/ld: cannot find -lrt
/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/ld: cannot find -ldl
/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/ld: cannot find -lpthread
/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/ld: cannot find -lm
/usr/lib/gcc/i586-suse-linux/4.7/../../../../i586-suse-linux/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

我也试过pkg-config --cflags --libs mono-2

【问题讨论】:

【参考方案1】:

您似乎没有安装静态库,例如libc.alibm.alibpthread.a

如果您使用的是 SuSE,也许yum install glibc-devel-static 会起作用。也许其他一些库需要安装其他包。

【讨论】:

以上是关于使用 GCC 创建静态链接的可执行文件的主要内容,如果未能解决你的问题,请参考以下文章

如何向 mingw-gcc 编译的可执行文件添加图标?

gcc 编译使用动态链接库和静态链接库

减小与 opencv 静态链接的可执行文件的大小

在构建我的 C++ 可执行文件 (gcc) 时,我可以获得所有链接库的报告吗? (包括静态链接)

将静态库和动态库链接到相同的可执行文件会导致哪些问题?

Linux动态和静态链接