_GNU_SOURCE and __USE_GNU
_GNU_SOURCE
is the only one you should ever define yourself. __USE_GNU
is defined internally through a mechanism in features.h
(which is included by all other glibc headers) when _GNU_SOURCE
is defined, and possibly under other conditions. Defining or undefining __USE_GNU
yourself will badly break the glibc headers.
What does #define _GNU_SOURCE imply?
Defining _GNU_SOURCE
has nothing to do with license and everything to do with writing (non-)portable code. If you define _GNU_SOURCE
, you will get:
- access to lots of nonstandard GNU/Linux extension functions
- access to traditional functions which were omitted from the POSIX standard (often for good reason, such as being replaced with better alternatives, or being tied to particular legacy implementations)
- access to low-level functions that cannot be portable, but that you sometimes need for implementing system utilities like
mount
,ifconfig
, etc. - broken behavior for lots of POSIX-specified functions, where the GNU folks disagreed with the standards committee on how the functions should behave and decided to do their own thing.
As long as you're aware of these things, it should not be a problem to define _GNU_SOURCE
, but you should avoid defining it and instead define _POSIX_C_SOURCE=200809L
or _XOPEN_SOURCE=700
when possible to ensure that your programs are portable.
In particular, the things from _GNU_SOURCE
that you should never use are #2 and #4 above.
TEMP_FAILURE_RETRY and __USE_GNU
__USE_GNU
is an internal macro, so you shouldn't define it yourself.
But you may define _GNU_SOURCE
, either in your code, or when compiling (using the -D
option).
I think defining this one will help to make TEMP_FAILURE_RETRY
available.
How's GNU extended function running when didn't defined _GNU_SOURCE
Implicit function declarations, a GCC extension which is enabled by default, use a return type of int
, however the function is defined as returning long long
. With such a type mismatch, anything can happen. In practice, the results depend on the calling convention, and that is why they vary across architectures.
You should really compile with -Werror=implicit-function-declaration
(or use C++). Implicit function declarations were removed from C close to twenty years ago (in ISO/IEC 9899:1999), but we still cannot change the GCC default because too many autoconf checks would break, resulting in software accidentally losing features.
(I think you meant to write retval
in your question instead of errno
.)
How to prevent GNU-specific function when defined two sources
How can I use XSI-compliant strerror_r instead
Create a separate source file with the needed strerror
verssion:
#include <string.h>
int xsi_strerror_r(int errnum, char *buf, size_t buflen) {
return strerror_r(errnum, buf, buflen);
}
Create a header file xsi_strerror.h
with function declaration:
#include <stddef.h>
int xsi_strerror_r(int errnum, char *buf, size_t buflen);
Then use in your source file that uses fcntl
use your function:
#define _GNU_SOURCE
#include <fcntl.h>
#include "xsi_strerror.h"
int main() {
if (fcntl(...)) {
int error_num = xsi_strerror_r(errno, ERROR_MESSAGE_BUFF, ERROR_MESSAGE_LENGTH);
}
}
Compile both files together.
How to prevent GNU-specific function when defined two sources
How can I use XSI-compliant strerror_r instead
Create a separate source file with the needed strerror
verssion:
#include <string.h>
int xsi_strerror_r(int errnum, char *buf, size_t buflen) {
return strerror_r(errnum, buf, buflen);
}
Create a header file xsi_strerror.h
with function declaration:
#include <stddef.h>
int xsi_strerror_r(int errnum, char *buf, size_t buflen);
Then use in your source file that uses fcntl
use your function:
#define _GNU_SOURCE
#include <fcntl.h>
#include "xsi_strerror.h"
int main() {
if (fcntl(...)) {
int error_num = xsi_strerror_r(errno, ERROR_MESSAGE_BUFF, ERROR_MESSAGE_LENGTH);
}
}
Compile both files together.
clone() function implicit declaration
Add the following lines at the top of you source file
#define _GNU_SOURCE
#include <linux/sched.h> /* or #include <sched.h> */
_GNU_SOURCE
is a Feature test macro.
Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled. In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value
) or by #define
-ing the macro within the source code before #include
-ing any headers.
Defining
_GNU_SOURCE
internally defines_USE_GNU
.sched.h
included<bits/sched.h>
In
<bits/sched.h>
, the functionclone()
is declared ONLY if_USE_GNU
is defined.
Compilation fails with error: ‘next_ioctl’ undeclared (first use in this function) despite I included dlfcn.h
dlfcn.h
itself doesn't define any symbol with name next_
smth. (In SUS, dlfcn.h only defines several dl*
functions and RTLD_
macro: http://pubs.opengroup.org/onlinepubs/7908799/xsh/dlfcn.h.html)
You should define this as pointer to function in your program code in explicit way. Something like this: (taken from https://port70.net/svn/misc/remac/remac.c or from https://github.com/itm/forward-sensor/blob/master/preload.c or ... any google search for "next_ioctl"
):
static int (*next_ioctl) (int fd, int request, void *data) = NULL;
Or, if you want a collective blog-reading session, there is additional line in the blog post with ioctl overloading: http://scaryreasoner.wordpress.com/2007/11/17/using-ld_preload-libraries-and-glibc-backtrace-function-for-debugging/ (just before first huge code fragment)
Then, declare a function pointer to hold the value
of the “real” ioctl() function from glibc:
static int (*next_ioctl)(int fd, int request, void *data) = NULL;
Then declare your replacement ioctl function:
Related Topics
Cost of Throwing C++0X Exceptions
Why Does Constexpr Static Member (Of Type Class) Require a Definition
Erasing While Iterating an Std::List
How to Implement No-Op MACro (Or Template) in C++
Opencv: Find All Non-Zero Coordinates of a Binary Mat Image
Does C and C++ Guarantee the Ascii of [A-F] and [A-F] Characters
When to Use Vectors and When to Use Arrays in C++
Std::Remove_Const with Const References
Understanding Region of Interest in Opencv 2.4
Why Sizeof Built in Types Except Char Is Compiler Dependent in C & C++
What Does This C Code Do [Duff's Device]
Add Library to Visual Studio 2008 C++ Project
Can You Have a Triple Minus Signs in C Programming? What Does It Mean
Is an Iterator in C++ a Pointer
Namespaces and Operator Overloading in C++
Does the Restrict Keyword Provide Significant Benefits in Gcc/G++
How to Pass Multiple Ints into a Vector at Once
Weird Msc 8.0 Error: "The Value of Esp Was Not Properly Saved Across a Function Call..."