-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to compile a statically linked executable with valac or gcc #1715
Comments
The solution is to use the
The Using |
The What is preoccupying here is that dependency declared as static get linked dynamically. It appears that the pkg-config flags do not land at the right location (should be after the |
It looks like it is using the C compiler to call whatever linker the C compiler uses. So the If you do After stage 3 the executable has been produced and as you say this is a concatenation of static objects. That to my mind is the definition of a statically linked executable! If you look at So the three steps make sense to:
What you seem to be suggesting is to generate the statically linked executable in two steps, rather than three. At the moment the See also #1104 for comments on Windows and how that might impact on any changes. There is an interesting comment there:
|
This seems like a good idea. I was able to compile a statically linked binary with 'meson configure -Dc_link_args=-static'. First I tried using c_args; as @astavale noted above, usually
But when I used '-Dc_args=-static', I just got a dynamically linked binary. |
Your example gcc command does both compiling and linking in a single step. It's not really comparable. The |
I see. What options can I give to meson configure that would build a binary program that normally links to ncurses shared libs, but instead build with the ncurses static library? For example, this program? |
dep_curses = dependency('curses') If you always want to link statically, you can add the #9603 proposed an option to make that a global configuration option tuneable. Some.projects have homebrewed such an option like this: dep_curses = dependency('curses', static: get_option('buildstatic')) |
Normally, no. I wanted to build statically on my PC, then transfer the binary to a server I was shelled into. I got some shared hosting yesterday, shelled in, cloned the rmw repo, but I couldn't build it because meson wasn't installed. Today I installed meson and ninja with pip3, but meson failed to complete reporting an outdated ninja version. Running 'ninja --version' threw errors about permissions (it was 1.10.2.3). I have a satisfactory resolution for now though... I used an AppImage of rmw.
Seems like that would be what I'm looking for. Thanks @eli-schwartz |
The intention was to run a simple test of cross-compilation with Meson. The idea was to create a statically linked executable compiled on Linux x86_64 that would run on an armv7 Android phone. The test was broken down in to two test outcomes:
Unfortunately I am unable to figure out how to get outcome one, the statically linked native executable, to compile with Meson.
I have broken the problem down in to three examples:
Compiling a Statically Linked Executable with valac
The Vala file is:
Valac can also call the C compiler, so a single line
valac
command to build the binary is:valac -X -static -X pthreads hello.vala
Using the command
ldd hello
reports:not a dynamic executable
and using
readelf --program-headers hello
shows no DYNAMIC section:So the output is what was requested - a statically linked binary.
Adding the
-v
switch to the valac command shows the C compiler command used:cc -o '/home/hello' '/home/hello.vala.c' -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -lgobject-2.0 -lglib-2.0 '-static' '-pthread'
Compiling a Statically Linked Executable with GCC
The C file is:
This can be compiled with:
gcc -static hello.c $(pkg-config --cflags --libs --static glib-2.0) -o hello
Note: the order of the command arguments matters here because it is a statically linked executable. Dependant static libraries and C files must be placed before their dependencies. For more detail see:
The
gcc
command also produces a statically linked executable as expected. Using the commandldd hello
reports:not a dynamic executable
and using
readelf --program-headers hello
shows no DYNAMIC section:Compiling a Statically Linked Executable with Meson
The
meson.build
file:A working binary is produced, but it is 9.8k in size, rather than the 1.6M for the statically linked executable. Using
ldd hello
reveals:Clearly something is wrong, either an option is missing from the
messon.build
file or Meson itself can't currently compile statically linked executables.The
-v
flag was passed as a C flag so the output fromninja
also gives the GCC options:The
compile_commands.json
file contains:Both the
gcc -v
andcompile_commands.json
output do not show-lglib-2.0
, which is output frompkg-config --cflags --libs --static glib-2.0
. This may be related to #380 or there is a linker stage with ninja that doesn't produce any output.From #1240 I assume people are producing statically linked executables, but issues #1104 and #1709 suggest this is not obvious or it does not work in everyone's case.
Any help appreciated...
The text was updated successfully, but these errors were encountered: