简体   繁体   English

如何在Linux上进行编译,以使生成的可执行文件不需要共享库

[英]How to compile on linux so that the resulting executable does not require shared libraries

I successfully compiled swftools using their instructions (http://wiki.swftools.org/index.php/FAQ) on my Fedora 14 machine. 我在Fedora 14机器上按照其说明(http://wiki.swftools.org/index.php/FAQ)成功编译了swftools。 I want to use one of the tools (pdf2swf) on another Linux machine. 我想在另一台Linux机器上使用其中一种工具(pdf2swf)。 When I move it and run it on the other machine it asks for some shared libraries. 当我移动它并在另一台计算机上运行它时,它会要求一些共享库。 Is it possible to compile swftools (in particular pdf2swf) so that when I run it on another Linux machine it does not ask for any shared libraries? 是否可以编译swftools(尤其是pdf2swf),以便在另一台Linux机器上运行它时不要求任何共享库? It is OK if the executable itself is bigger in size, as far as it can run independently. 如果可执行文件本身可以更大,并且可以独立运行就可以了。

I am new to Linux, so if something requires advanced knowledge please point me to the appropriate online resource. 我是Linux的新手,所以如果您需要高级知识,请向我指出相应的在线资源。

Regards 问候

it's trivial: link with -static. 这很简单:与-static链接。 of course, this implies that you'll need static libraries installed. 当然,这意味着您需要安装静态库。 the linker (often called through cc) simply defaults to using shared libraries when both are available. 当链接器(通常通过cc调用)都可用时,它们仅默认使用共享库。

[hahn@box ~]$ cat hello.c
#include <stdio.h>
int main() {
    printf("Hello, world!\n"); 
    return 0; 
}
[hahn@box ~]$ cc hello.c -o hello
[hahn@box ~]$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, not stripped
[hahn@box ~]$ ldd hello
        linux-gate.so.1 =>  (0x00205000)
        libc.so.6 => /lib/libc.so.6 (0x00697000)
        /lib/ld-linux.so.2 (0x005b4000)
[hahn@box ~]$ cc hello.c -o hello -static
[hahn@box ~]$ file hello
hello: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, not stripped
[hahn@box ~]$ ldd hello
        not a dynamic executable
[hahn@box ~]$ ./hello
Hello, world!

to make this work, I needed to install glibc-static, which is not installed by default (at least on this box, which is Fedora14). 为了使此工作有效,我需要安装glibc-static,默认情况下未安装它(至少在此框上是Fedora14)。 some packages let you select static linking at the ./configure level, or else you may need to modify the Makefile. 有些软件包允许您在./configure级别选择静态链接,否则您可能需要修改Makefile。

Well, what you want are statically linked libraries, as opposed to dynamically/shared libraries. 好吧,您想要的是静态链接库,而不是动态/共享库。

You need to compile your application and link it statically. 您需要编译您的应用程序并进行静态链接。 If you use gcc you can apply the static switch to your call of the compiler: 如果使用gcc,则可以将静态开关应用于您的编译器调用:

gcc -static <and the whole gcc shebang>

Most of the times you can edit the makefile (look for a CC define or CC_ARGS something like that) and just include the static switch as above. 大多数情况下,您可以编辑makefile(查找CC定义或CC_ARGS之类的东西),并且只包含上述静态开关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM