Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

When to define _GNU_SOURCE [Solved]

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
Akkara
Bodhisattva
Bodhisattva
User avatar
Posts: 6702
Joined: Tue Mar 28, 2006 12:27 pm
Location: &akkara

When to define _GNU_SOURCE [Solved]

  • Quote

Post by Akkara » Fri Dec 26, 2008 4:32 am

I was writing some C code that needed the pow10 function. I got a "incompatible implicit declaration" error despite #including <math.h>.

From the man page I learned it is a gnu extension, and that _GNU_SOURCE needs to be defined. So I define it:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#define _GNU_SOURCE     1
#include <math.h>
But I still get that error. I search and search, and nothing. As far as I can tell I was doing everything correctly.

On a whim I massage the code some:

Code: Select all

#define _GNU_SOURCE     1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
The error goes away and the code works!

So I think, "that's strange, maybe pow10 is defined in one of the other header files?" I try putting _GNU_SOURCE between stdio and stdlib, but the error returns. It only works when _GNU_SOURCE is defined first, before *any* include files have been included.

My problem is solved, but I'm still curious: how does defining a macro before or after including <stdio.h> have any bearing on what happens when <math.h> is included? There seems to be a strange side-effect going on here. Maybe it is a bug? Maybe I don't understand how _GNU_SOURCE is supposed to operate?

Code: Select all

$ equery list glibc
[ Searching for package 'glibc' in all categories among: ]
 * installed packages
[I--] [ ~] sys-libs/glibc-2.8_p20080602-r1 (2.2)

$ gcc --version
gcc (Gentoo 4.3.2 p1.1) 4.3.2
Last edited by Akkara on Fri Dec 26, 2008 6:26 am, edited 1 time in total.
Top
Hu
Administrator
Administrator
Posts: 24401
Joined: Tue Mar 06, 2007 5:38 am

  • Quote

Post by Hu » Fri Dec 26, 2008 5:42 am

A quick glance through /usr/include/features.h shows that defining _GNU_SOURCE causes various other preprocessor values to be modified. In particular, it enables __USE_GNU. pow10 is guarded by __USE_GNU, rather than _GNU_SOURCE. Thus, you need the headers to enable __USE_GNU to grant you access to pow10. Since stdio.h includes features.h, and features.h has an include guard, the preprocessor values get configured as soon as you include stdio.h and stay that way for the rest of the preprocessing phase.

It should be possible to include headers before you define _GNU_SOURCE. However, the libc headers that I have checked include features.h, so you can only safely include your own headers, and then only if they do not in turn use libc headers.
Top
Akkara
Bodhisattva
Bodhisattva
User avatar
Posts: 6702
Joined: Tue Mar 28, 2006 12:27 pm
Location: &akkara

  • Quote

Post by Akkara » Fri Dec 26, 2008 6:26 am

Thanks!, Hu, for a very clear explanation.
Top
Captain Newbie
Apprentice
Apprentice
User avatar
Posts: 182
Joined: Fri Dec 22, 2006 2:10 am
Location: Socal

  • Quote

Post by Captain Newbie » Fri Dec 26, 2008 7:16 am

Akkara wrote:Thanks!, Hu, for a very clear explanation.
I think I've seen that bad boy defined on the command line to gcc, i.e.:

Code: Select all

gcc -O2 -march=nocona -pipe -D_GNU_SOURCE foo.c -o ...
which might let you set it and forget it in your Makefile.
/* Nobody will ever see this message :-) */
panic("Cannot initialize video hardware\n");
"As much as it pains me, we hope that developers know what they're doing." - wolf31o2
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic