Skip to content
Advertisement

Is it possible to compile statically with gcc or g++ on Linux based systems?

I am working on a project which I would like to be able to “ship” to other Linux based computers as a complete executable with no dependencies. (In order that I can copy just the 1 file to other systems, and then run that file on those systems.)

In the windows world, I think static linking is done just by passing -static to the compiler, perhaps with some other options for specific libraries*, if you are using Visual Studio.

*eg: Using SFML you also have to define SFML_STATIC for some reason?

Is this possible using gcc / g++, and if so how? I tried searching for this but didn’t manage to find anything. I have heard before that this may be a non-trivial task.

Edits:

BasileStarynkevitch suggested compiling with the flag -static.

I don’t know if this is what I want, but I wrote a test program to try it out:

JavaScript

And then compiled with:

JavaScript

The results are:

JavaScript

So it looks like it might be working?

TonyD suggested a method of checking:

JavaScript

Advertisement

Answer

Compiling with -static appears to work for basic c++ programs, probably all of the standard library?

However when using with other libraries, this may no longer work.

Advertisement