static linking with cross compiler - static

My test.c program uses printf function. I want to statically link library I need. I use arm-linux-gnueabi-gcc cross compiler.
When I compile my code like this
arm-linux-gnueabi-gcc test.c -o test
it passes, but I think he still uses dynamic linking by looking at dissasembly of test.
So,what option should I add in order to statically link libc.a library?

Try
arm-linux-gnueabi-gcc test.c -o test -Xlinker -static /path/to/libc.a
This worked for me. Also, it might complain that you don't have static libgcc_s. Then try this:
arm-linux-gnueabi-gcc test.c -o test -Xlinker -static /path/to/libc.a -static-libgcc
Hope this helps!

Related

Would gcc -O take effect for linked static library?

such as:
gcc test.c mystaticlib.a -o test -O2
would -O2 take effect for mystaticlib.a, or just test.c?
-O2 is a compilation flag. The only input you're compiling in this example is test.c. mystaticlib.a is not compiled, but rather linked with the compilation output of test.c to create the executable test. Since mystaticlib.a isn't compiled here, the -O2 flag does not affect it.

Windows Eclipse C/C++ cannot find external library [duplicate]

I use code::blocks to compile my static library. The output result is a libstatic.a file.
Now, how do I link to my library to use functions that were compiled?
(I tried to use #include "libstatic.a" but my project doesn't compile)
cc -o yourprog yourprog.c -lstatic
or
cc -o yourprog yourprog.c libstatic.a
You should #include "libstatic.h", i.e. use the appropriate header file in your code (that's why your code doesn't compile) and include the path to your libstatic.a in the linker options as one of your input libraries.
This webpage has some examples on linking to a static library, e.g.
gcc -I. -o jvct jvct.c libjvc.a
I had to set the library path in my makefile. For this case you could use:
gcc -o myapp main.c -L. -lstatic
gcc -I. -o jvct jvct.c libjvc.a
To link purely statically, use -static
cc -static yourprogram.c libstatic.a

Create static library including ncurses

I want to create a static library implementing a TUI interface using ncurses. I'm new to this topic and applying these answers didn't work
out for me.
rpath
combine multiple libraries
I want to link varViewer.c in a library. I use this code to compile a example file.
Compiling src/example.c and src/viewer/varViewer.c:
gcc -Wall -Wextra -Wnonnull -Isrc/viewer/
-c src/example.c -o obs/example.o -lncurses
-fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2
gcc -Wall -Wextra -Wnonnull -Isrc/viewer/
-c src/viewer/varViewer.c -o obs/viewer/varViewer.o -lncurses
-fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2
Linking obs/example.o to bin/example:
gcc -o bin/example -Isrc/viewer/
obs/example.o obs/viewer/varViewer.o -lncurses
-fsanitize=undefined -fprofile-arcs -ftest-coverage -Winline -O2
bin/example works as expected. I tried to compile a suitable object file:
Compiling src/viewer/varViewer.c:
gcc -Wall -Wextra -Wnonnull -Isrc/viewer/
-c src/viewer/varViewer.c -o lib/objects/varViewer.o
-lncurses -fPIC -O2
and put it into library:
ar -cvr lib/libvarViewer.a lib/objects/varViewer.o
when I try to use it in other projects, a include it using
-L ..../src/viewer/lib -lvarViewer
flags but all references to functions I used from ncurses library and
string.h are undefined:
nm /lib/libvarViewer.a
....
U __snprintf_chk
U __stack_chk_fail
U start_color
U stdscr
U strchr
U strlen
U strncmp
U strncpy
U strnlen
U waddnstr
U wborder
U wclear
U wcolor_set
U wgetnstr
U winsdelln
U winsnstr
U wmove
U wrefresh
What is the correct way to call ar? I already tried to produce a "thin" library using -T option.
The command
gcc -Wall -Wextra -Wnonnull -Isrc/viewer/
-c src/viewer/varViewer.c -o lib/objects/varViewer.o
-lncurses -fPIC -O2
will simply compile the varViewer.c file and thus, will ignore the libraries because it won't link to a final executable.
Consequently, the object file varViewer.o will have undefined references (U reported in nm as you have shown) to the symbols not defined within varViewer.c. These not only include the libncurses library but also the regular libc library (which provides symbols such as strlen, strncpy, ...). However, remember that libc will be automatically added into your link stage by the compiler unless you explicitly state otherwise.
You could use the command ar x to extract the object files (.o) from other static libraries and then use ar again to generate a new library. While this is possible I don't think that including all the libraries into yours is a good idea. I think that it is better to link against all the libraries needed because that ensures that the application is linked against the latest version available -- while if you embed one library into yours, then will stick to that version until you upgrade it.

Cannot compile using ALSA

I am trying to create an C application on Debian GNU/Linux which uses the PortAudio interface. To do this I must compile my program with gcc -lrt -lasound -ljack -lpthread -o YOUR_BINARY main.c libportaudio.a from this docs.
For this I installed libasound2-dev, and I checked where the files are using apt-file search libasound.so, this is the output:
lib32asound2: /usr/lib32/libasound.so.2
lib32asound2: /usr/lib32/libasound.so.2.0.0
lib32asound2-dev: /usr/lib32/libasound.so
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0
libasound2-dev: /usr/lib/x86_64-linux-gnu/libasound.so
So the libasound should be installed correctly, but when I compile my program with this makefile:
DMXTest: main.c libdmx.a
gcc -static -Wall main.c -L. -ldmx -lusb -lrt -lasound -ljack -lfftw3 -g -o main libportaudio.a
I get the following error: /usr/bin/ld: cannot find -lasound.
How can I link this library correctly?
You don't have libasound.a for -static, you will need that, or you can almost certainly just remove -static from the Makefile (likely in LDFLAGS or CFLAGS).
There's is a related Debian bug 522544, and a related Ubuntu bug #993959.
You may be able to build your own libasound from source, though as it also uses other libraries (notably libpthread.so, librt.so and libdl.so) I suspect it may remove some functionality when you build it statically, though it's supported with ./configure --enable-static at build time
(or try --enable-shared=no --enable-static=yes).
FWIW, the use of static binaries is "discouraged" by the glibc maintainers, though I don't agree...
To compile my code i used the following command
gcc -o rec_mic rec_mic.c -lasound
and it works perfectly, without create my own static library.

How to link to a static library in C?

I use code::blocks to compile my static library. The output result is a libstatic.a file.
Now, how do I link to my library to use functions that were compiled?
(I tried to use #include "libstatic.a" but my project doesn't compile)
cc -o yourprog yourprog.c -lstatic
or
cc -o yourprog yourprog.c libstatic.a
You should #include "libstatic.h", i.e. use the appropriate header file in your code (that's why your code doesn't compile) and include the path to your libstatic.a in the linker options as one of your input libraries.
This webpage has some examples on linking to a static library, e.g.
gcc -I. -o jvct jvct.c libjvc.a
I had to set the library path in my makefile. For this case you could use:
gcc -o myapp main.c -L. -lstatic
gcc -I. -o jvct jvct.c libjvc.a
To link purely statically, use -static
cc -static yourprogram.c libstatic.a

Resources