Mageia-Linux x86_64 Error: exec shell C code with static compilation

63 Views Asked by At

I am trying to exec a shell in C using the following code:

#include <unistd.h>
int main(){
    char *name[2];
    name[0] = "/bin/sh";
    name[1] = NULL;
    execve(name[0],name,NULL);
    return(0);
}

To compile this, I am using the gcc -static flag:

gcc -static -o shell shell.c

However, I get the following error:

/bin/ld: cannot find -lc

So far, I have tried to run : ld -lc --verbose and here's what I've found:

attempt to open /usr/x86_64-mageia-linux-gnu/lib64/libc.so failed
attempt to open /usr/x86_64-mageia-linux-gnu/lib64/libc.a failed
attempt to open /usr/local/lib64/libc.so failed
attempt to open /usr/local/lib64/libc.a failed

Can anyone please help me figure out what's going wrong here?

2

There are 2 best solutions below

0
On

If you want to use -static, you need to install glibc-static-devel.

0
On

You're missing (at least) a static version of the glibc. If you really want to link statically (I assume dynamic linking, without the -static flag, would work? Why do you want to link statically here?) -- you will have to find out which package contains the libc.a file needed for that on your distribution.

On the other hand, if compiling never works, regardless of -static or not, you're missing development packages, something called typically libc-dev, libc-devel, glibc-dev, glibc-devel or something similar, it really depends on your distribution.