What does the compilation error "SIG_BLOCK undeclared" mean? - c

I get an error message error: ‘SIG_BLOCK’ undeclared for this code when compiling with -ansi.
sigprocmask(SIG_BLOCK, &my_sig, NULL);
Did I forget to be explicit about some header file? These are my includes
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>

You need to tell the compiler you want SIG_BLOCK to be defined.
From man sigaction:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
sigprocmask(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
So you might like to pass the option
-D_POSIX_SOURCE
or
-D_POSIX_C_SOURCE=1
to gcc for example
Alternativly you can put those "requests" as preprocessor directives right at the top of your sources:
#define _POSIX_SOURCE
... /* other includes */
#include <signal.h>
or
#define _POSIX_C_SOURCE 1
... /* other includes */
#include <signal.h>

Related

Weird behavior with defines for usleep() and inet_aton()

I have created two modules: files.h and connection.h.
files.h is included in connection.h.
files.h uses usleep() function and connection.h uses inet_aton() function at some point of the respective .c files. Those functions need the following defines:
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
So, as files.h is included in connection.h, I thought I could just write those defines in files.h but when I compile I get the following error:
connection.c:23:6: error: implicit declaration of function ‘inet_aton’
So I decided to try to write those defines in connection.h instead of files.h just to compile and get the following error:
files.c:298:3: error: implicit declaration of function ‘usleep’
At this point, my next option was writing the defines in the respective .c files to solve this. But instead, I got this error while compiling:
files.c:302:3: error: implicit declaration of function ‘usleep’
connection.c:23:6: error: implicit declaration of function ‘inet_aton’
I don't understand what's the issue. How can I use both functions?
files.h
#ifndef _FILES_H_
#define _FILES_H_
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
// ...
#endif
connection.h
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "files.h"
#include <ctype.h>
#include <pthread.h>
// ...
#endif
This seems to be an ordering issue..
When you include in this way:
#include <unistd.h> // other includes as well
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
The header files are brought in without the override defines.
However, the purpose of the defines is to change what functions/signatures are imported from the headers!
And since C is very linear, the order matters..
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include <unistd.h> // other includes as well
Basically, define your requests first, before you include any standard header file.
These must be included before the FIRST TIME the header is seen -
So if 'connection.c' includes something before connection.h, then the defines may not be present for the first include of <unistd.h>
I finally solved this issue writing the defines above the includes thanks to your suggestions BUT in the .c files. Still not working if I write the defines anywhere in the .h files.
files.c
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include "files.h"
connection.c
#define _XOPEN_SOURCE 500
#define _POSIX_C_SOURCE 1
#define _GNU_SOURCE
#include "connection.h"

C macro name must be an identifier

I've created a c project and this is the beginning of the main.c file:
#include <curl/curl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "include/httpdef.h"
//...some code
The httpdef.h beginning is this:
#ifndef httpdef
#define httpdef
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
//definitions
#endif
At the very first line of both files I get the error from the gcc compiler:
macro name must be an identifier
What could be the problem?
EDIT: I realized now that actually the compiler doesn't give any error, it's my vim plugin (YouCOmpleteMe) that generates this error. If I compile everything works and the error doesn't appear

issue compiling c code on linux (Missing headers)

I am having issues compiling my code. It seems to an issue with the include headers
Here are my headers:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <asm/page.h>
#define __KERNEL__
#include <asm/unistd.h>
Here's what I am using to compile:
gcc -o file file.c -I/usr/src/linux-headers-4.12.0-kali2-common/include/asm-generic
I keep getting this error when I compile:
fatal error: uapi/asm-generic/signal.h: No such file or directory
#include <uapi/asm-generic/signal.h>
If I try adding asm/ or asm-generic/ to signal.h, I get:
redefinition of ‘struct timeval’

handling warning: implicit declaration of function ‘sigignore’

Here's my code:
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
int main(int argc, char** argv) {
sigignore(SIGTERM);
return 0;
}
Why do I get the following warning and how could I remove it?
implicit declaration of function ‘sigignore’
[-Wimplicit-function-declaration] sigignore(SIGTERM);
The program must be compiled like this: gcc -o foo.o foo.c.
Thanks
Man sigignore tells you to use #define _XOPEN_SOURCE 500 to enable sigignore. More on X/Open can be found here
The function you want to call has been marked as obsolete 15 years ago. The normal way to discourage people from using those functions (without actually breaking programs) is to have the implementation of the function left in the standard library, but remove the declaration from header files (or at least make it hard to enable).
Use sigaction or sigprocmask (depending on what you actually want to accomplish).

Error including netinet/in.h

Even after trying all orders of header file inclusion,
I still get the error for netinet/in.h
/usr/include/netinet/in.h:34: error: expected identifier before numeric constant
I have included the following header files
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/select.h>
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/in.h>
How do I get rid of this error?
I compile with gcc -g3 -Wall.
netinet/in.h doesn't have header guard so what's happening is some variable is already been defined in netinet/ip.h header file. try pushing netinet/in.h to beginning of the file.

Resources