I'm trying to work through XDP tutorial and having issue with samples compilation. Why clang complains regarding includes that are already done?
Example:
common_params.c:105:4: warning: incompatible implicit declaration of built-in function ‘strncpy’
common_params.c:105:4: note: include ‘<string.h>’ or provide a declaration of ‘strncpy’
common_params.c:110:6: error: ‘errno’ undeclared (first use in this function)
110 | errno, strerror(errno));
| ^~~~~
common_params.c:15:1: note: ‘errno’ is defined in header ‘<errno.h>’; did you forget to ‘#include <errno.h>’?
14 | #include "common_params.h"
+++ |+#include <errno.h>
Cheching common_params.c shoes it already have string.h and errno.h included:
# head ../common/common_params.c
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
Files are presented in the box:
# ls -l /usr/include/errno.h
-rw-r--r-- 1 root root 23 Jun 6 18:36 /usr/include/errno.h
# ls -l /usr/include/string.h
-rw-r--r-- 1 root root 238 Jun 6 18:36 /usr/include/string.h
# cat /usr/include/errno.h
#include <asm/errno.h>
# cat /usr/include/asm/errno.h
#include <asm-generic/errno.h>
# head /usr/include/asm-generic/errno.h
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef _ASM_GENERIC_ERRNO_H
#define _ASM_GENERIC_ERRNO_H
#include <asm-generic/errno-base.h>
#define EDEADLK 35 /* Resource deadlock would occur */
#define ENAMETOOLONG 36 /* File name too long */
#define ENOLCK 37 /* No record locks available */
Related
// SYSTEM INCLUDE FILES
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/uio.h>
// C STANDARD LIBRARY INCLUDE FILES
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
char *source = "file1.txt";
char *dest = "file2.txt";
printf("%d\n", faccessat(AT_FDCWD, source, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
printf("%d\n", faccessat(AT_FDCWD, dest, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
printf("Value of errno: %s\n", strerror(errno));
return 0;
}
When compiling this code with clang 14 and the command clang -lrt -pthread -I/usr/include -std=c11 -o copy copy.c I run into numerous issues:
copy.c:21:20: warning: implicit declaration of function 'faccessat' is invalid in C99 [-Wimplicit-function-declaration]
printf("%d\n", faccessat(AT_FDCWD, source, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
^
copy.c:21:30: error: use of undeclared identifier 'AT_FDCWD'
printf("%d\n", faccessat(AT_FDCWD, source, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
^
copy.c:21:68: error: use of undeclared identifier 'AT_SYMLINK_NOFOLLOW'
printf("%d\n", faccessat(AT_FDCWD, source, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
^
copy.c:22:30: error: use of undeclared identifier 'AT_FDCWD'
printf("%d\n", faccessat(AT_FDCWD, dest, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
^
copy.c:22:66: error: use of undeclared identifier 'AT_SYMLINK_NOFOLLOW'
printf("%d\n", faccessat(AT_FDCWD, dest, F_OK | R_OK | W_OK, AT_SYMLINK_NOFOLLOW));
^
1 warning and 4 errors generated.
The same code works on my Mac with MacOS 12, but does not work on Ubuntu in WSL. How would I fix this issue?
I tried to reinstall build-essentials, clang, gcc, glibc. I reviewed the header files and found that fcntl.h does not contain the AT_* definitions, yet the man page says they do. I was expecting the header file to contain the definitions.
From what the comments have told me, and online, faccessat is a gnu extension in C, so to use it I would have to compile my code with -std=gnu11 to use these.
I'm trying to get size of pipe:
printf("pipe 0 size: %d bytes\npipe 1 size: %d bytes\n", fcntl(fd[0], F_GETPIPE_SZ), fcntl(fd[1], F_GETPIPE_SZ));
Used headers (half of them used by another parts of code):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/wait.h>
When I'm trying to compile, gcc fails with this error:
‘F_GETPIPE_SZ’ undeclared (first use in this function)
kernel version - 5.4.0-88-generic
libc6-dev version - 2.31-0ubuntu9.2
gcc version - 4:9.3.0-1ubuntu2
Since this macro is not part of POSIX, you must define the _GNU_SOURCE feature test macro before including <fcntl.h>.
This is stated in the fcntl(2) man page, in the "Conforming To" section.
See What does "#define _GNU_SOURCE" imply?
I'm using a header called lib.h to organize my source code. The header is like:
#define TOT_REP 10
#define TOT_PAT 10
#define TIME_REP 15
The source file include the header, but when i compile with gcc i'm getting this:
error: ‘TIME_REP’ undeclared (first use in this function)
So i tried to compile with gcc -E -dM and i got something like this:
...
#define SIGUSR2 12
#define TIME_REP 15
#define ____mbstate_t_defined 1
#define __SIGRTMIN 32
...
I also tried with gcc -E and in the outuput i found that the macro is properly replaced with its value.
What can I do to solve this?
EDIT: The code where TIME_REP is used is this:
while((!ending|| *(shmAddress+0)!=0)&& quitSignal==0){
totFolder=0;
buf=(char*)calloc(2,sizeof(char));
patientString=(char*)calloc(2,sizeof(char));
sleep(TIME_REP);
while(read(fd,buf,sizeof(char))>0){
/*read from a file and get some data*/
}
}
EDIT 2: I tried to rename the lib.h and it seems to work now but i just can't understand why if with gcc -E -Dm found the macro then i can't compile the code.
Anyway to answer to Woodrow Barlow:
i have the lib.h and a rep.c the rep.c include the lib.h and other headers:
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include "ReaderWriter.h"
#include "lib.h"
To compile I use gcc rep.c -o rep -Wall -pedantic
i want to use struct ifreq, but it cannot be access when i make:
the code is :
1 #include <sys/types.h>
2 #include <sys/ioctl.h>
3 #include <sys/socket.h>
4 #include <net/if.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <netdb.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <string.h>
12
13 typedef uint32_t uint32;
14 #define MAX_IF 10
15 int
16 main()
17 {
18
19 struct ifreq ifVec[MAX_IF];
20
21 int sock = -1;
when i make, there are some error :
windeal#ubuntu:~/Windeal/apue$ make
gcc "-std=c99" -o getIfInfo.o -c getIfInfo.c
getIfInfo.c: In function ‘main’:
getIfInfo.c:18:15: error: array type has incomplete element type
As stated here, you should use -std=gnu99 instead of -std=c99 when compiling.
I'm using Linux Mint 13 and I am studying the book "Unix Network Programming: Interprocess Communication". I downloaded the source code of the book - http://www.kohala.com/start/unpv22e/unpv22e.html - and followed the instructions.
First, I ran ./configure in the base directory
Then, I went into the lib directory and ran make. This gives the following error -
gcc -c "/home/linux/Code/c/unix_network_programming/main.c" -g -o ./Debug/main.o "-I." "-I."
In file included from /usr/lib/gcc/i686-linux-gnu/4.6/include/stdint.h:3:0,
from /usr/include/netinet/in.h:24,
from /usr/include/rpc/types.h:91,
from /usr/include/rpc/rpc.h:38,
from /home/linux/Code/c/unix_network_programming/unpipc.h:115,
from /home/linux/Code/c/unix_network_programming/main.c:2:
/usr/include/stdint.h:49:24: error: duplicate ‘unsigned’
/usr/include/stdint.h:49:24: error: two or more data types in declaration specifiers
/usr/include/stdint.h:50:28: error: duplicate ‘unsigned’
/usr/include/stdint.h:50:28: error: duplicate ‘short’
/usr/include/stdint.h:52:23: error: duplicate ‘unsigned’
/usr/include/stdint.h:52:23: error: two or more data types in declaration specifiers
Here's the file that is giving the error -
#ifndef _GCC_WRAP_STDINT_H
#if __STDC_HOSTED__
# include_next <stdint.h>
#else
# include "stdint-gcc.h"
#endif
#define _GCC_WRAP_STDINT_H
#endif
Any ideas on how to fix this error as I have very little C experience?
Apply the following, mildly nasty patch:
diff -U 3 ./aclocal.m4 ./aclocal.m4
--- ./aclocal.m4 1997-10-10 22:45:46.000000000 +0100
+++ ./aclocal.m4 2013-03-25 17:35:22.287397177 +0000
## -31,6 +31,7 ##
AC_TRY_COMPILE(
[
#include "confdefs.h" /* the header built by configure so far */
+#include <stdint.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
diff -U 3 ../unpv22e/config.h.in ./config.h.in
--- ./config.h.in 1998-06-10 18:26:41.000000000 +0100
+++ ./config.h.in 2013-03-25 17:42:18.788139903 +0000
## -2,6 +2,7 ##
#undef CPU_VENDOR_OS
/* *INDENT-OFF* */
+#undef HAVE_STDINT_H
#undef HAVE_DOOR_H /* <door.h> */
#undef HAVE_MQUEUE_H /* <mqueue.h> */
#undef HAVE_POLL_H /* <poll.h> */
## -49,6 +50,9 ##
#undef HAVE_DEV_ZERO
/* Define the following to the appropriate datatype, if necessary */
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
#undef int8_t /* <sys/types.h> */
#undef int16_t /* <sys/types.h> */
#undef int32_t /* <sys/types.h> */
diff -U 3 ../unpv22e/configure.in ./configure.in
--- ./configure.in 1998-06-06 22:42:29.000000000 +0100
+++ ./configure.in 2013-03-25 17:38:14.555324559 +0000
## -105,7 +105,7 ##
dnl but used in "lib/wrapunix.c".
dnl
AC_HEADER_STDC
-AC_CHECK_HEADERS(sys/types.h sys/time.h time.h errno.h fcntl.h limits.h signal.h stdio.h stdlib.h string.h sys/stat.h unistd.h sys/wait.h sys/ipc.h sys/msg.h sys/sem.h sys/shm.h mqueue.h semaphore.h sys/mman.h sys/select.h poll.h stropts.h strings.h sys/ioctl.h sys/filio.h pthread.h door.h rpc/rpc.h sys/sysctl.h)
+AC_CHECK_HEADERS(stdint.h sys/types.h sys/time.h time.h errno.h fcntl.h limits.h signal.h stdio.h stdlib.h string.h sys/stat.h unistd.h sys/wait.h sys/ipc.h sys/msg.h sys/sem.h sys/shm.h mqueue.h semaphore.h sys/mman.h sys/select.h poll.h stropts.h strings.h sys/ioctl.h sys/filio.h pthread.h door.h rpc/rpc.h sys/sysctl.h)
dnl ##################################################################
dnl Checks for typedefs.
Then run autoconf (use autoconf2.13 rather than a newer version), delete config.cache, and try again.
Just comment out these lines in config.h (execute ./configure first):
#define uint8_t unsigned char /* <sys/types.h> */
#define uint16_t unsigned short /* <sys/types.h> */
#define uint32_t unsigned int /* <sys/types.h> */
It works for me:).