C - Compile with dependencies included

自作多情 提交于 2019-11-30 04:11:09

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/16246923/c-compile-with-dependencies-included

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!