C - 编译包含依赖关系 [英] C - Compile with dependencies included

查看:163
本文介绍了C - 编译包含依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码要运行在我没有root权限的机器上。
该机器没有运行此代码所需的一些库。



在编译时有没有办法包含所有的依赖关系?我意识到结果文件可能相当大。

解决方案

您正在寻找的是静态编译。执行静态编译将所有库包含到可执行文件本身中,因此您不必担心特定系统,分发等上的依赖链。



你可以这样做:

  gcc -Wl,-Bstatic -llib1 -llib2 file.c 

-Wl将标志传递给链接器,-Bstatic指示链接静态(如果可能),然后lib1,lib2是

或者,试试:

  gcc -static-libgcc -static file.c 

汇编仍然需要与非特权系统。你需要在编译系统上安装静态库(lib.a)。如果编译正确,运行时应该显示不是动态的可执行文件:

  ldd a.out 


I have some code which I want to run on a machine which I do not have root access to. That machine does not have some of the libraries needed to run this code.

Is there any way to include all dependencies when I compile? I realize the resultant file may be quite large.

解决方案

What you're looking for is static compiling. Performing static compilation includes all of the libraries into the executable itself, so you don't have to worry as much about dependency chains on a specific system, distribution, etc.

You can do this with:

gcc -Wl,-Bstatic -llib1 -llib2 file.c

The -Wl passes the flags following to the linker, -Bstatic tells it to link static if possible, and then lib1, lib2, are the libs you intend to link.

Alternatively, try:

gcc -static-libgcc -static file.c

The compilation will still need to match the architecture of the non-privileged system. And you need to have the static libraries installed on the compiling system (lib.a)

If compiled properly, it should show "not a dynamic executable" when you run:

ldd a.out

这篇关于C - 编译包含依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆