vfork() implicit declaration - c

I working in C with vfork(). My program working fine, but I have warning about implicit declaration.
My code:
if(vfork()==0){
...
}
My warning is:
implicit declaration of function 'vfork' [-Wimplicit-function-declaration] if(vfork()==0){^
I include those:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
If I use fork() and not vfork() warning gone. Soo problem is only vfork() in my program.
I don't know what this mean or how I fix that.

You need to include these 2 headers:
#include <sys/types.h>
#include <unistd.h>
Also, add this line in the beginning of the program:
#define _BSD_SOURCE

If you already have the required include files, then, depending on your system version, you may need to define some feature test macros. Please see documentation for your system (man vfork on unix-like systems)

Adding onto Igor's answer, make sure you aren't compiling for C99. clang gives me the error "implicit declaration of function 'vfork' is invalid in C99", and removing -std=c99 from the arguments fixed the issue.

Related

usleep issue with unistd.h [duplicate]

I'm fairly new to C but writing a small multithreaded application. I want to introduce a delay to a thread. I'd been using 'usleep' and the behavior is what I desire - but it generates warnings in C99.
implicit declaration of function ‘usleep’
It's only a warning, but it bothers me. I've Googled for an answer but all I could find was a while-loop / timer approach that seemed like it would be CPU intensive.
EDIT:
My includes are:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
And I'm invoking the compiler with:
c99 program.c -Wall -pedantic -W -lpthread
EDIT #2:
I've created a new file that contains:
#include <unistd.h>
int main(void) {
usleep(10);
}
And I still get the warning.
EDIT #3:
As suggested, I've updated the text of the question.
The problem is that you are using a standard C99 compiler, but you're trying to access POSIX extensions. To expose the POSIX extensions you should for example define _POSIX_C_SOURCE to 200809L (for the current standard). For example this program:
#include <time.h>
int main(void) {
struct timespec reqtime;
reqtime.tv_sec = 1;
reqtime.tv_nsec = 500000000;
nanosleep(&reqtime, NULL);
}
will compile correctly and wait for 1.5 seconds (1 second + 500000000 nanoseconds) with the following compilation command:
c99 main.c -D _POSIX_C_SOURCE=200809L
The _POSIX_C_SOURCE macro must be defined with an appropriate value for the POSIX extensions to be available.
Also the options -Wall, -pedantic and -W are not defined for the POSIX c99 command, those look more like gcc commands to me (if they work on your system then that's fine, just be aware that they are not portable to other POSIX systems).
You are probably on a modern POSIX system:
POSIX.1-2008 removes the specification of usleep().
On my system (linux) there is a detailed explanation of the macros that must be set to get that function back. But you should just follow the advice that zvrba already gave, use nanosleep instead.
From the manual page: This function is obsolete. Use nanosleep instead.
implicit declaration of function ‘usleep’
This warning usually means that you didn't #include the right header file, in this case unistd.h.
Did you have the #include <unistd.h> ?.
And you can use some of the similar methods instead: nanosleep() for nanoseconds and sleep() for seconds. Or another way could be using clock(), but i think this is more CPU wasting.

implicit declaration of function 'getline' even thought it is

I'm currently having some problems with getline() in c.
I know it's not a standard C function, however I am using the proper resources according to what I've seen online.
I have both:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
However I am still having problems compiling, it gives me this output:
main.c: In function 'read-file':
main.c:46:17: warning: implicit declaration of function 'getline' [-Wimplicit-function-declaration]
C:\Users\Miguel\AppData\Local\Temp\cc6aYESe.o:main.c:(.text+0x85): undefined reference to `getline'
collect2.exe: error: ld returned 1 exit status
I can't seem to figure out what's wrong, I've seen several problems like this, however no solution seems to apply.
try with the following preprocessor sentence:
#define _POSIX_C_SOURCE 200809L
"It allows you to use functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard. Using the macros described in feature_test_macros allows you to control the definitions exposed by the system header files."
ref: What does #define _POSIX_SOURCE mean?

how to change pipe size in c (or let parent get child's value without using pipe) [duplicate]

I have included following headers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
I have also tried to use
#define _GNU_SOURCE
before #include <unistd.h>, but it also does not help.
I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message:
error: ‘F_SETPIPE_SZ’ undeclared (first use in this function)
I actually found out that I don't need this, but I'm just curious why I can't use it.
Thank you.
So here's solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
So here's the solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
You should also pay attention to Chrono Kitsune's other comment.
F_SETPIPE_SZ/F_GETPIPE_SZ are relatively recent. Older kernels (e.g. 2.6.32 as used in RHEL6) don't have them. If you look in /usr/include/linux/fcntl.h and these constants aren't defined, then this API isn't going to work and you'll have to find some way to bypass it in whatever you're building.

F_SETPIPE_SZ undeclared

I have included following headers:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
I have also tried to use
#define _GNU_SOURCE
before #include <unistd.h>, but it also does not help.
I try to use fcntl and pass it F_SETPIPE_SZ as second argument, but I keep getting this error message:
error: ‘F_SETPIPE_SZ’ undeclared (first use in this function)
I actually found out that I don't need this, but I'm just curious why I can't use it.
Thank you.
So here's solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
So here's the solution, thanks to Chrono Kitsune:
Put
#define _GNU_SOURCE
before any includes.
You should also pay attention to Chrono Kitsune's other comment.
F_SETPIPE_SZ/F_GETPIPE_SZ are relatively recent. Older kernels (e.g. 2.6.32 as used in RHEL6) don't have them. If you look in /usr/include/linux/fcntl.h and these constants aren't defined, then this API isn't going to work and you'll have to find some way to bypass it in whatever you're building.

Suppress GCC warning "extra tokens at end of #include directive"

I'm writing a program in C intended to be compiled and run on a HP NonStop machine. However, I want to do the main development on my workstation running Linux. The HP NonStop C-Compiler requires non-standard #include directives like the following:
#include <stdio.h> nolist
For each #include directive, my workstation's GCC is complaining:
S88USF.c:139:21: warning: extra tokens at end of #include directive
How can I suppress this particular warning?
Note: On SO, similar questions have already been asked, the correct answer being along the lines of "don't give gcc any reason to complain in the first place". In this scenario however, I explicitly want to have the #include directives exactly as they are.
I know what I'm doing, I just don't know how to inform gcc about it.
One workaround would be to define a header that includes the following preproccesor macro:
//hp_workaround.h
#ifdef HP_
#define HP_INCLUDE_DIRECTIVE(x) x
#else
#define HP_INCLUDE_DIRECTIVE(x)
#endif
then guard your directives with this macro
#include "hp_workaround.h"
#include <stdio.h> HP_INCLUDE_DIRECTIVE(nolist)
Macroexpansion happening within include can probably help.
GCC will accept this:
#define nolist
#include <stdlib.h> nolist
/* maybe #undef nolist here */

Resources