Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should _GNU_SOURCE be defined throughout the project?

Tags:

c

gnu

If I'm planning to use something that is only provided after declaring _GNU_SOURCE, do I need to declare _GNU_SOURCE at the top of all source files in the project?

Is it safe to only declare it at the top of any source files that require it?

My initial concern is related to type declarations... it's of course possible that a struct changes shape after defining _GNU_SOURCE, but is that likely, or is it guaranteed that such things "will not change shape"?

For example, if I use a struct to declare a variable in one file (with _GNU_SOURCE), and then use that variable in another (without _GNU_SOURCE), is guaranteed that I will not run into problems?


In this case I'm after pthread_tryjoin_np().

like image 465
Attie Avatar asked Jul 14 '26 23:07

Attie


1 Answers

It is safe to declare it only in files that need it.

After all, the whole point is that some code would break if was defined, but it still needs to be linked with code that uses it.

As @IanAbbott notes below, exception is when you use some of the varying types in your interface. Then you need to keep the definition consistent for the modules that use it. E.g. off_t becomes, under _GNU_SOURCE, alias for off64_t, so if you then include the same header with _GNU_SOURCE turned off, it will define different functions.

That said, within a project there is really not much reason not to define it everywhere, because once you define it in any file, you depend on it. So defining it locally only helps anything if it is in an optional component or if you have alternates for other systems that use functions specific to those other systems instead.

like image 56
Jan Hudec Avatar answered Jul 17 '26 15:07

Jan Hudec