Include directives not importing - c

I've been having this problem multiple times throughout a program I'm currently working on where gcc doesn't recognize a function or file type that I import from a library even though the include directive is clearly at the top of the program with all the others. To get around this I've just been finding similar libraries and using functions from those, but for what I'm trying to do right now none of the libraries I have found are working. Here is my list of directives:
#include "oscar.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <utime.h>
#include <malloc.h>
I'm currently trying to change the access and modification times in a file. I've tried using utime(), utimes(), futimens() and several other variations and all of them are having the same problem. GCC isn't recognizing many of the functions or file types. For example, I'm trying to use futimens():
futimens(m_file_desc, times);
but gcc gives the following error
warning: implicit declaration of function ‘futimens’

You probably need to need to specify the feature test macros.
From the man page:
futimens():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
So, depending on your glibc version, you need to either define _GNU_SOURCE or _XOPEN_SOURCE, or _POSIX_C_SOURCE with appropriate values.

Related

#ifdef SIOCSHWTSTAMP not found

I want to take hardware timestamps on the network card. I found some example code from Solarflare which checks whether the following label is defined:
#ifdef SIOCSHWTSTAMP
struct ifreq ifr;
struct hwtstamp_config hwc;
#else
static_assert(false); // I added this to check whether my system has the label defined
#endif
but the static_assert is getting triggered, so this label is not defined in my system.
I am including these headers:
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
so clearly it's not defined in those.
We installed linux-libc-dev but it doesn't seem to have helped.
Which header should it exist in, so I can check?
My kernel version is 3.10.0-1062.4.1.el7.x86_64 and CentOS 7
In the Linux OS it's defined in the User API kernel headers, i.e. in the
include/uapi/linux/sockios.h for latest v3.10.y kernel as of today.
For newer kernel it's still there.
The necessary structure and enumeration values are defined in include/uapi/linux/net_tstamp.h.

Modularity of a system in c

i have a few libraries which I use in modules of the same system.
My problem is that when I do #include to the h files, finally in the system it says "undefined reference to ..." different functions of the module which included twice, once in each different module. It is probably because of the double declaration, how do I manage it ?
I have "rialtor.h" in which:
#include <stdbool.h>
#include "apartment.h"
#include "offer.h"
I have "client.h", in which:
#include <stdbool.h>
#include "apartment.h"
#include "offer.h"
I have "system.c" in which:
#include "rialtor.h"
#include "client.h"
You're looking in the wrong place. An undefined reference is a linker error. You're not including all the needed object files or libraries on the command line that produces your executable.

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.

vfork() implicit declaration

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.

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.

Resources