C - Compile with dependencies included

南笙酒味 提交于 2019-12-01 04:45:54
Alec Bennett

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