Compiling to a standalone file without any library dependencies

Hey everyone. I tried googling it, but with no luck. I have written this small program with C++, and I am trying to make it standalone.
I compile with
g++ -std=c++0x -lbcrypt -lboost_regex -o main main.o aes256.o `pkg-config --cflags --libs gtk+-2.0`

Bcrypt is a library I found on the internet.
But when I try to run the compiled program on another computer, it doesn't work, says libraries missing. So how can I make it standalone?

Sorry I'm that dumb.
Last edited on
Seek "static linking".
I tried to do something like this

g++ -static -std=c++0x -lbcrypt -lboost_regex -o main main.o aes256.o `pkg-config --cflags --libs gtk+-2.0`

And the output is
/usr/bin/ld: cannot find -lbcrypt
/usr/bin/ld: cannot find -lgtk-x11-2.0
/usr/bin/ld: cannot find -lgdk-x11-2.0
/usr/bin/ld: cannot find -lpangocairo-1.0
/usr/bin/ld: cannot find -latk-1.0
/usr/bin/ld: cannot find -lcairo
/usr/bin/ld: cannot find -lgdk_pixbuf-2.0
/usr/bin/ld: cannot find -lgio-2.0
/usr/bin/ld: cannot find -lpangoft2-1.0
/usr/bin/ld: cannot find -lpango-1.0
/usr/bin/ld: cannot find -lgobject-2.0
/usr/bin/ld: cannot find -lglib-2.0
/usr/bin/ld: cannot find -lfontconfig
/usr/bin/ld: cannot find -lfreetype
collect2: error: ld returned 1 exit status

Where is it looking for those?
Last edited on
Well on linux you just download the repo it gets put in a single global folder which is bin (I think) and everything works.

If your project is to be distributed by source code, it would be easier to just tell people to download the dependencies, otherwise you still need to download the repo's of the libraries so that you can static compile them. I think the repo variant should be called "dev" sor something like that, and for all the missing libraries you can easily google what repo you need to download by looking up the command.

Also consider using the linux board next time you have problems related to an OS.
On dynamic linking the linker adds to the binary instructions that make the dynamic linker load the required shared objects (libraries, .so) when loading the program.

On static linking the linker copies object code from static libraries (.a) into the (monolithic) binary. Static library is essentially a collection concatenated object files.


On CentOS 7 (as example), package

zlib.x86_64 contains:
/usr/lib64/libz.so.1
/usr/lib64/libz.so.1.2.7

zlib-devel.x86_64 contains:
/usr/lib64/libz.so
and header files

zlib-static.x86_64 probably contains:
/usr/lib64/libz.a

Furthermore,
lrwxrwxrwx. /usr/lib64/libz.so -> libz.so.1.2.7
lrwxrwxrwx. /usr/lib64/libz.so.1 -> libz.so.1.2.7
-rwxr-xr-x. /usr/lib64/libz.so.1.2.7

i.e. libz.so.1.2.7 is real file and the other two are mere links.


All libraries do not have the static version (in package). Either "not needed" or "too hard to create".


Check your distro.



Edit: tldr
-lfoo looks for libfoo.so

-static -lfoo looks for libfoo.a
Last edited on
-lfoo looks for libfoo.a. -static changes the mode of linking, not the search behavior.
True, I was too brief.
GNU ld manual wrote:
-l namespec --library=namespec
Add the archive or object file specified by namespec to the list of files to link. ... it will search the library path for a file called libnamespec.a.

On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a.


-static
Do not link against shared libraries. This is only meaningful on platforms for which shared libraries are supported. You may use this option multiple times on the command line: it affects library searching for -l options which follow it.

Sounds like:
look for .so
IF found AND static not set
THEN use the .so
ELSE look for .a and use it, if found


Replacing libfoo.so with libfoo.a is not trivial, because the libfoo.so is likely to link/depend on other shared libraries, which also have to be replace with static versions, and those shared libraries might depend on ...
I solved my problem by reducing the number of libraries my code uses, keeping the libbcrypt.so in the same folder as the compiled application and using this command to compile:

g++ -o main -std=c++0x encryption.cpp main.cpp aes256.cpp -L . -l bcrypt $(pkg-config --cflags --libs gtkmm-3.0) -Wl,-rpath,"."

Thanks for all the help.
Topic archived. No new replies allowed.