6

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.

Marko
  • 1,267
  • 1
  • 16
  • 25
  • 2
    How are you calling `fcntl`. Can you post that in your question? – Fiddling Bits Aug 20 '14 at 18:51
  • 3
    You should `#define _GNU_SOURCE` before including any headers. I doubt that will help, but it is worth saying. What does `uname -r` show? If it is less than 2.6.35, you shouldn't have `F_SETPIPE_SZ`. The man page I have documents it as being available since Linux 2.6.35. –  Aug 20 '14 at 20:16
  • Thanks, it actually did solve problem, I don't know how but it did. And one more problem to. Awesome! Thank you. (And no, my kernel is not that old :) ) – Marko Aug 21 '14 at 15:54
  • 2
    @ChronoKitsune don't define like this. the formal way to enable it is by cli: gcc --std=gnu blabla. – Jason Hu Aug 21 '14 at 16:01
  • 2
    You shouldn't add the answer to question. Instead post it as an answer. You're allowed to answer your own questions, and you'll be to accept in a day or two. – Ross Ridge Aug 21 '14 at 16:04
  • 1
    @HuStmpHrrr You're correct, but since `_GNU_SOURCE` is a Linux glibc feature test macro ([`man feature_test_macros`](http://man7.org/linux/man-pages/man7/feature_test_macros.7.html)), just like `_POSIX_C_SOURCE` and `_XOPEN_SOURCE`, it is perfectly reasonable to simply define the macro on a per-file basis **before including any headers**. The man page specifically states that it is allowed. The important point is to define it before any headers are included, meaning either on the command line or in a file. –  Aug 21 '14 at 19:55

2 Answers2

13

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.

Community
  • 1
  • 1
Marko
  • 1,267
  • 1
  • 16
  • 25
2

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.