F_SEAL_SEAL undeclared, even when headers are included - c

I'm trying to use file sealing on Linux. Here's an example C program.
#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
int main(void) {
(void)F_SEAL_SEAL;
}
You can build it using gcc -Wall -o ./linux_file_sealing linux_file_sealing.c or similar.
When I build it, I get an error about F_SEAL_SEAL.
gcc -Wall -o ./linux_file_sealing linux_file_sealing.c
linux_file_sealing.c: In function ‘main’:
linux_file_sealing.c:7:19: error: ‘F_SEAL_SEAL’ undeclared (first use in this function)
printf("%d\n",F_SEAL_SEAL);
^
linux_file_sealing.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
I'm including unistd.h and fcntl.h, as per the man page... so what else should I be doing, and where is that described?
(The man pages just say that sealing is "Linux-specific", but give no further details. This is the reason for including the GNU_SOURCE define, which is how you get the other Linux-specific stuff, but for F_SEAL_SEAL it seems to make no difference.)
(Ubuntu 16.04 LTS, Linux 4.4.0-36)

You want
#include <linux/fcntl.h>
instead of
#include <fcntl.h>

Related

unknown type name "pthread_barrier_t"

I am trying to parallelize an algorithm in C. I want to use pthread_barrier_t but my Ubuntu wsl can't find it for some reason. I have pthread.h included and I can use the rest of the pthread functions. libthread.a is installed.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Error occurs here */
pthread_barrier_t barrier;
Exact error is: "identifier pthread_barrier_t is undefined"
I saw elsewhere it could be the way I'm compiling.
Compiling as follows:
gcc -o test test.c -Wall -std=c99 -lpthread -lm
Also, VS Code can't identify the function.
The problem is your -std=c99 option. Using strict C mode disables a bunch of stuff, including something that stops pthread_barrier_t from getting defined. If you use -std=gnu99 instead, it should compile. (Tested on Ubuntu 16.04 on WSL).
Alternatively, add
#define _XOPEN_SOURCE 600 /* Or higher */
or
#define _POSIX_C_SOURCE 200112L /* Or higher */
before the first #include in your source. See man 7 feature_test_macros for the acceptable values of these macros and more information.

Using gcc to compile userspace application using linux kernel headers

I have a really simple c program that I want to compile using gcc, importing from linux kernel headers.
#include <stdio.h>
#include <stdlib.h>
#include <linux/random.h>
int main(){
int rand;
get_random_bytes(&rand,sizeof(rand));
printf("%d",rand);
return 0;
}
I have tried to compile this program using the following command:
gcc rand.c -D__KERNEL__ -isystem /lib/modules/`uname -r`/build/include
But I get a bunch of errors (below). What am I missing?:
/usr/src/kernels/4.9.8-201.fc25.x86_64/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
#include <asm/linkage.h>
From some quick Google searches, it seems like get_random_bytes might be a private function only usable from within the kernel.
How about you try using getrandom instead? Here is the documentation of getrandom:
http://man7.org/linux/man-pages/man2/getrandom.2.html

Is there a way to ask gcc to treat #include <> like #include ""?

Is there a compiler or preprocessor flag that will force gcc to treat #include <x.h> like it would #include "x.h"? I have a bunch of generated code that uses #include <> for files in the current directory, and gcc reports No such file or directory for these files. I'm looking for a workaround that doesn't involve editing the code.
EDIT: -I. doesn't do it. Let's say I have the following files:
foo/foo.h:
#include <foo2.h>
foo/foo2.h:
#define FOO 12345
xyz/xyz.c
#include <stdio.h>
#include "foo/foo2.h"
int main(void)
{
printf("FOO is %d\n", FOO);
return 0;
}
If, inside the xyz directory, I compile with gcc -o xyz I.. xyz.c, the compile fails:
In file included from xyz.c:2:
../foo/foo.h:1:18: error: foo2.h: No such file or directory
xyz.c: In function ‘main’:
xyz.c:6: error: ‘FOO’ undeclared (first use in this function)
xyz.c:6: error: (Each undeclared identifier is reported only once
xyz.c:6: error: for each function it appears in.)
Adding -I. doesn't change anything.
But, if I change foo/foo.h to:
#include "foo2.h"
Then the compile works. I know I could add -I../foo to my command line, but I was looking for a more generic way to treat #include <> as #include "". Does one exist?
Yes, you can pass the switch -I . to the compiler to add the current directory to the include search path.
The -I- option might help you. From gcc's man page:
-I- Split the include path. Any directories specified with -I options
before -I- are searched only for headers requested with
"#include "file""; they are not searched for "#include <file>". If
additional directories are specified with -I options after the -I-,
those directories are searched for all #include directives.

STDERR_FILENO undeclared on ubuntu

I'm trying to compile an example stack trace displaying code. When I compile the test.c file with:
gcc -g -rdynamic ./test.c -o test
I get following error:
./test.c: In function ‘handler’:
./test.c:16: error: ‘STDERR_FILENO’ undeclared (first use in this function)
./test.c:16: error: (Each undeclared identifier is reported only once
./test.c:16: error: for each function it appears in.)
My includes are the same as in the original post code:
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
My machine is ubuntu 13.04. Am I missing some library or haven't included something?
Also #include <unistd.h>.
See this GNU documentation.

How do I use strcasestr()?

I #include <string.h> but when I call strcasestr(src, search); I get the following error message implicit declaration of function ‘strcasestr’. how to I compile: gcc-4.6 -Wall -lsqlite3 -lunac -Werror -O2 -o foo.out foo.c how to fix this? Thanks in advance.
As specified in the corresponding manpage, since strcasestr is a nonstandard extension you must #define _GNU_SOURCE before the #include <string.h> before any #include (other files may already include <string.h>, thanks #Cubbi for pointing out this potential problem); this can also easily be accomplished by specifying -D_GNU_SOURCE on the compiler command line.
You must add:
#define _GNU_SOURCE
before the string.h include, since the function is non-standard.

Resources