Why do I see the “/bin/ld: cannot find -lc” when I want to compile a c program with the “-static” option?
Environment
- Red Hat Enterprise Linux release 7.x 8.x 9.x.
- gcc
- glibc-static
Issue
- Unable to compile c code with "-static" option.
- Failed to compile and occur "/bin/ld: cannot find -lc" error.
Resolution
- Enable the codeready-builder(RHEL8,9) or optional-rpms(RHEL7) repository.
Warning:optional-rpms
andcodeready-builder
are out of Red Hat support scope, those repositories are optional for developers.
- For RHEL7
subscription-manager repos --enable rhel-7-server-optional-rpms
- For RHEL8
subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
- For RHEL9
subscription-manager repos --enable codeready-builder-for-rhel-9-x86_64-rpms
- Installing the
glibc-static
.
yum install -y glibc-static
Root Cause
According to the document below, Red Hat doesn't suggest compiling the c program with a static library, because it will introduce some unknown risks.
Compatibility
Static linking appears to provide executable files independent of the versions of libraries provided by the operating system. However, most libraries depend on other libraries. With static linking, this dependency becomes inflexible and as a result, both forward and backward compatibility is lost. Static linking is guaranteed to work only on the system where the executable file was built.Support coverage
Most static libraries provided by Red Hat are in the CodeReady Linux Builder channel and not supported by Red Hat.
So if you want to compile your c program with a static library, you need to install "glibc-static".
Diagnostic Steps
Following the below steps to test without installing the glibc-static
.
# Editing a test c program.
cat << EOF > program.c
int main(void)
{
return 0;
}
EOF
# Compiling it with "-static" option
[root@node-0 gcc_test]# /opt/rh/gcc-toolset-9/root/usr/bin/gcc program.c -static
/usr/bin/ld: cannot find -lc
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments