STDERR_FILENO undeclared on ubuntu - c

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.

Related

Do I need to compile separately included header files from another direcotry?

I'm working on a project (hangman.c) and I want to include a module I wrote from another directory (quicksort.h). Here is the top of my hangman.c file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../quicksort/quicksort.h"
I've been running this command:
clang -o hangman hangman.c
And i get this error message:
/usr/bin/ld: /tmp/hangman-3292ab.o: in function `append_and_order':
hangman.c:(.text+0x26c): undefined reference to `quicksort'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I compile them separately and link them together manually with
gcc -c hangman.c
gcc -c ../quicksort/quicksort.c
gcc -o hangman hangman.o quicksort.o
(As corrected). And that works, but isn't the point of #include to do this automatically at the preprocessing stage?

F_SEAL_SEAL undeclared, even when headers are included

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>

gcc cannot compile C files using mpi.h

I just received a message where someone could not compile a C file.
When they try to compile, it's getting the following error.
$ gcc mpi01.c
mpi01.c:1:17: fatal error: mpi.h: No such file or directory
#include <mpi.h>
^
compilation terminated.
$
I'm sure the C code is present so it must be a problem with the installation, but mpi.h is there.
/opt/mpss/3.6/sysroots/k1om-mpss-linux/usr/src/kernel/drivers/message/fusion/lsi/mpi.h
/usr/include/openmpi-x86_64/mpi.h
/usr/src/kernels/3.10.0-229.20.1.el7.x86_64/include/linux/mpi.h
Does someone know what I could do?
The system is running Centos7.
Edit 1:
To respond to the answers. It is not my code that I am trying to compile. What I did to "ensure" everything is in place for C compiling:
yum install gcc openmpi kernel-devel kernel-headers openmpi-devel
I do not know if I am allowed to post the code, but the following headers are included in the code:
#include <mpi.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
The compiler command line was the following:
vim mpi01.c
module load mpi
gcc -o mpi01 mpi01.c
Resulting in the error above.

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.

Compiling against libusb-dev on Ubuntu

I am trying to compile the example libusb.c provided by libusb package (if you dl the source code.)
It doesn't work to say the least.
#include <stdio.h>
#include <sys/types.h>
#include <libusb/libusb.h>
That causes it to fail, there is no libusb/libusb.h it's usb.h, so I change that. And it fails in new and innovative ways.
I've copied the file over, exactly, and named it example.c
I am using these commands and variations:
gcc -o example example.c -lusb -L /usr/lib/libusb.a
gcc -o example example.c -lusb -L /usr/lib/libusb.so
The errors I get when compiling are:
example.c:25: error: expected ‘)’ before ‘*’ token
example.c: In function ‘main’:
example.c:46: error: ‘libusb_device’ undeclared (first use in this function)
example.c:46: error: (Each undeclared identifier is reported only once
example.c:46: error: for each function it appears in.)
example.c:46: error: ‘devs’ undeclared (first use in this function)
Line 25: static void print_devs(libusb_device **devs)
Line 46: libusb_device **devs;
At first I followed a tutorial, and that failed to compile, in more or less the same ways, so I decided to just try the provided example, and that failed.
Can anyone help me out? Explain what I am doing wrong, cause I am lost on this one.
This is what I had to do on Debian. It should be at least similar in Ubuntu.
Install libusb-1.0-0-dev
Instead of:
#include <libusb/libusb.h>
do:
#include <libusb.h>
Compile with:
gcc example.c `pkg-config --libs --cflags libusb-1.0`
Just en explanation why your attempt to replace libusb/libusb.h with usb.h fails: usb.h is a header from linux-headers, not from libusb-dev. You need #include <libusb.h> instead.

Resources