How do I resolve this error 'cannot find -lc'? - c

How do I compile this code in CentOS 7 ? I am reading one book and in the book they use -static while compiling so that's how I did it and I get errors I mentioned below but when I dont use -static I get no errors and it compiles successfully.
First attempt:
main()
{
exit(0);
}
I get this error when I try to compile it.
$ gcc -static -o exit exit.c
exit.c: In function _main_:
exit.c:3:9: warning: incompatible implicit declaration of built-in function _exit_ [enabled by default]
exit(0);
^
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
Second attempt:
Then I google this error and lots of articles told me to include stdlib.h library so I did that as well and I get this error:
Code:
#include <stdlib.h>
main()
{
exit(0);
}
Now when I compile it, I get following error.
$ gcc -static -o exit exit.c
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
linux version:
$ uname -a
Linux localhost.localdomain 3.10.0-1127.13.1.el7.centos.plus.i686 #1 SMP Thu Jun 25 16:59:06 UTC 2020 i686 i686 i386 GNU/Linux

As #KamilCuk noted, this requires a different set of libraries for static linking, and on my CentOS 7 machine, this installs the proper libraries:
# yum install glibc-static
Then the compile should work as you expect.
The libraries installed by this package are:
$ rpm -q -l glibc-static
/usr/lib64/libBrokenLocale.a
/usr/lib64/libanl.a
/usr/lib64/libc.a
/usr/lib64/libc_stubs.a
/usr/lib64/libcrypt.a
/usr/lib64/libdl.a
/usr/lib64/libm.a
/usr/lib64/libnsl.a
/usr/lib64/libpthread.a
/usr/lib64/libresolv.a
/usr/lib64/librt.a
/usr/lib64/libutil.a

Related

why is -ldgcc option not working in my make file

im trying to complie some operating system example code and when i run make (in running the default debian eviroment on a chromebook lunix termial) it gives me the folowing error:
kuzai_longmane#penguin:/mnt/chromeos/removable/32GB/Operating systems (source code)/my_cool_os/my_cool_os$ make
gcc -m32 -nostdlib -nodefaultlibs -lgcc start.o libc/string/memcmp.o libc/string/memset.o libc/string/strcat.o libc/string/strchr.o libc/string/strcmp.o libc/string/strcpy.o libc/string/strlen.o libc/string/strncmp.o libc/string/strstr.o libc/string/strutil.o libc/string/ctos.o kernel/tty.o kernel/io.o kernel.o -T linker.ld -o myos
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/10/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
collect2: error: ld returned 1 exit status
make: *** [Makefile:28: myos] Error 1
any sugestions or things i overlooked?
You're trying to generate and link 32-bit code (the -m32 option), but the linker can't find a 32-bit version of libgcc. The message says its is finding a 64-bit version, so the problem is probably just that you don't have 32-bit libs installed. I think on debian, you need to install the ia32-libs package for that.

Compile C on CentOS to static library

I'm able to compile and run my application in C on CentOS with the following parameters:
gcc test.c -o test -lpcap -lssl -lcrypto -L /usr/local/lib -L /usr/local/opt/openssl/lib/ -lhiredis
But I need to run the application on a machine where I cannot compile, nor download ex. hiredis.
So I need to compile everything together on my own CentOS - which does match in setup.
I've read about the static flag, but when doing so I get the following errors:
gcc -static test.c -o test -lpcap -lssl -lcrypto -L /usr/local/lib -L /usr/local/opt/openssl/lib/ -lhiredis
/usr/bin/ld: cannot find -lpcap
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
/usr/local/lib/libhiredis.a(net.o): In function `_redisContextConnectTcp':
/home/alfredballe/hiredis/net.c:399: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
What am I doing wrong?

Linking fails with gcc 4.8.2 / ld 2.24, succeeds with gcc 4.4.7 / ld 2.20

In a chroot based on CentOS 6.4 I'm working in, linking against ncurses with ld 2.20 succeeds, but linking with ld 2.24 fails. I don't directly invoke the linker, gcc is handling it -- gcc 4.4.7 is using ld 2.20, and gcc 4.8.2 is using ld 2.24.
Here is a minimal example that fails to link with gcc 4.8.2 / ld 2.24 in my particular environment.
#include <ncurses.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
WINDOW* window = NULL;
if (!(window = initscr())) {
printf("Error initializing ncurses.");
exit(1);
}
halfdelay(50);
getch();
endwin();
}
Success (ld 2.20):
$ gcc main.c -lncurses -Wl,--verbose | grep "ncurses.*succeeded"
attempt to open /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../lib64/libncurses.so succeeded
$
Failure (ld 2.24):
$ /opt/gcc/4.8.2/bin/gcc48 main.c -lncurses -Wl,--verbose | grep "ncurses.*succeeded"
attempt to open /usr/lib/../lib64/libncurses.so succeeded
/opt/binutils/2.24/bin/ld24: /tmp/ccCxUFxl.o: undefined reference to symbol 'halfdelay'
/lib64/libtinfo.so.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
$
Note that both commands appear to link against the same libncurses.so.
For what it's worth, in a different chroot based on CentOS 5.4, there is a different libncurses version, and it links fine with ld 2.24, but unfortunately, building in that chroot is not an option. The nm utility shows that the (static) libncurses.a here does have the required symbols (nm lists no symbols for libncurses.so -- so I'm just assuming they are similar).
In the CentOS 6.4 chroot, however, nm shows that all of the ncurses symbols I'm getting "undefined reference" messages for are indeed undefined or not present in libncurses.a in the Centos 6.4 chroot, which is confusing, because linking with gcc 4.4.7 works. Something is not right.
Also, I tried producing an object with gcc 4.4.7, and then linking with gcc 4.8.2, but that did not help.
I'm confused as to why the one compiler / linker would succeed while the other would fail. Is this an ABI issue? Does anyone know what is happening here? Are there any flags I can pass to gcc to make the new linker work?
Your libncurses library is itself linked to libtinfo, which causes your older toolchain to also look for symbols in libtinfo.
But newer toolchains normally runs the linker with the --as-needed, and the --no-copy-dt-needed-entries, the latter which probably causes the difference you're seeing.
Basically you'll need to link to libtinfo as well, which is where the halfdelay function resides.
gcc main.c -lncurses -ltinfo

Code for adding iptables rules using IPTC library in linux is not linking

I am trying to compile the example C code provided here http://www.bani.com.br/lang/en/2012/05/programmatically-managing-iptables-rules-in-c-iptc/
using (as described in the post):
cc `pkg-config –cflags –libs libiptc` iptablesExample_2.c -o iptablesExample_2
I have also tried compiling it using:
sudo gcc -g -o iptablesExample_2 iptablesExample_2.c -liptc -liptables -ldl
it did not work also and the error this time was:
/usr/bin/ld: cannot find -liptables
collect2: ld returned 1 exit status
I also tried the example provided at the end of this
http://wiki.tldp.org/iptc%20library%20HOWTO
And I had the same issues
Linux kernel: 3.8.2 iptables version: v1.4.12

linker error cannot find symbol name in library

g++ (GCC) 4.7.2
3.7.6-201.fc18.x86_64 #1 SMP Mon Feb 4 15:54:08 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Fedora release 18 (Spherical Cow)
Hello,
I am compiling and having a problem trying to link a program.
The linker error is:
/usr/bin/ld: point.o: undefined reference to symbol '_Znwj##GLIBCXX_3.4'
/usr/bin/ld: note: '_Znwj##GLIBCXX_3.4' is defined in DSO /lib/libstdc++.so.6 so try adding it to the linker command line
/lib/libstdc++.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
This object file is point.o is trying to call a function that doesn't exist in libstdc++.
When I try and check if the symbol name does exist using readelf I can't find it.
readelf --all libstdc++.so.6.0.17 | grep _Znwj##GLIBCXX_3.4
Is this because the point.o is looking for a symbol in a older libstdc++ that could have been removed in a later version?
Many thanks for any suggestions,
In my case I was using gcc not g++. It used to work in '12, but a later build on a different machine in '14 choked. The gnu compiler has indeed changed. The answer in my case was to add
-lstdc++
onto the end of the linking line, as the error messages suggest. HTH.

Resources