compiling with gcc with system()
Solution 1
Add #include <stdlib.h>
at the top of main()
.
Tip: When you see implicit declaration of a built-in function, you have to search for the function (with google, for example now you should have searched with "system() C"), in order to find the corresponding header, i.e. where the function is declared. Then one of the results should be the ref of the function.
In our case this link. There you can see:
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
which tells you that you have to include stdlib
header to use system()
.
As Mr. Bright noticed, if you're on on a inux-like OS, man 3 system
should do the trick too.
Example:
samaras@samaras-A15:~$ man 3 system
SYSTEM(3) Linux Programmer's Manual SYSTEM(3)
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h> <-- this is what we are looking for!
int system(const char *command);
...
Solution 2
Since it appears you are using a Posix system, you should know about the man
command which shows documentation for most library calls. On my system, when I type:
$ man system
I get:
SYSTEM(3) Linux Programmer's Manual
NAME
system - execute a shell command
SYNOPSIS
#include <stdlib.h>
int system(const char *command);
Notice that in the synopsis it tells you the include file you need to use. The man page also includes a lot of other documentation such as the return value.
Josh Buchanan
Updated on June 11, 2022Comments
-
Josh Buchanan almost 2 years
My code does not compile right when I use:
system("gcc -o filename temp.c");
I am getting:
implicit declaration of function system
I'm not sure what is missing, because it only throws the system error on the gcc call.
Here is my code:
#include <stdio.h> int main() { ... system("gcc -o filename temp.c"); return 0; }
-
Sean Bright about 9 yearsIf you're on on a unixy OS,
man 3 system
should do the trick too. -
Josh Buchanan about 9 yearsOk, so if something throws a implicit declaration of function, it means that there is header file missing?
-
gsamaras about 9 yearsYes @JoshBuchanan, not any header though, the header that needs to be included by that particular function at the time. Now
system()
requiredstdlib.h
to be included. Nice question though, you got my +1. Hope you keep my tip, it will help I believe! -
Josh Buchanan about 9 years@G.Samaras Thank you, I will keep that in mind, this is like my third C program. Still trying to figure out what to look and when.