Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - Compile with dependencies included

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.

like image 360
Hector Avatar asked Apr 27 '13 00:04

Hector


People also ask

Is G ++ and gcc the same?

“GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”). When referring to C++ compilation, it is usual to call the compiler “G++”.

Can G ++ compile c?

gcc is used to compile C program. g++ can compile any . c or . cpp files but they will be treated as C++ files only.

What are dependencies in c?

In Dependency relationships class diagrams, a dependency relationship indicates that a change to one class, the supplier, might cause a change in the other class, the consumer. The supplier is independent because a change in the consumer does not affect the supplier.

What can gcc compile?

GCC stands for GNU Compiler Collections which is used to compile mainly C and C++ language. It can also be used to compile Objective C and Objective C++.


1 Answers

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 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
like image 64
Alec Bennett Avatar answered Oct 21 '22 18:10

Alec Bennett