-1

When I make a gcc compile command with the option "-L",I should add the gblic library path or add -D_GNU_SOURCE or the gcc will run error.

$gcc sscanf_test.c -I/si/usr/aa/include/arch -o sscanf_test_2  -m64  -L$ORACLE_HOME/precomp/lib/ -L$ORACLE_HOME/lib/ -L$ORACLE_HOME/lib/stubs/ -lc
$/tmp/ccq1Z6T1.o:function ‘main’:
  sscanf_test.c:(.text+0x32):undefined reference to ‘__isoc99_sscanf’
  sscanf_test.c:(.text+0xf8):undefined reference to ‘__isoc99_sscanf’
  sscanf_test.c:(.text+0x1e4):undefined reference to ‘__isoc99_sscanf’
  collect2: ld return 1
$gcc sscanf_test.c -I/si/usr/aa/include/arch -o sscanf_test_2  -m64  -L$ORACLE_HOME/precomp/lib/ -L$ORACLE_HOME/lib/ -L$ORACLE_HOME/lib/stubs/ -D_GNU_SOURCE
$gcc sscanf_test.c -I/si/usr/aa/include/arch -o sscanf_test_2  -m64  -L$ORACLE_HOME/precomp/lib/ -L$ORACLE_HOME/lib/ -L$ORACLE_HOME/lib/stubs/ /lib64/libc.so.6

Now, I have puzzle that why gcc need -D_GNU_SOURCE.Another machine have a similar environment can run normally without -D_GNU_SOURCE.

hrl
  • 131
  • 1
  • 11
  • what is `gblic`? Did you mean `glibc`? – Sourav Ghosh Nov 29 '17 at 08:24
  • [Related?](https://stackoverflow.com/q/16376341/2173917) – Sourav Ghosh Nov 29 '17 at 08:25
  • @SouravGhosh I mean the superset of all other standards under GNU C libraries. – hrl Nov 29 '17 at 08:29
  • Why this C++1 tag? What does this have to do with C++ ? – Jens Gustedt Nov 29 '17 at 08:30
  • @JensGustedt em,isn't gcc similar to c++? – hrl Nov 29 '17 at 08:33
  • 1
    @hrl, gcc is a compiler C++ is a programming language. How can one be another? gcc is a compiler for many languages, C and C++ among them. – Jens Gustedt Nov 29 '17 at 08:35
  • @SouravGhosh Thanks for your recommendation.It may related to my question ,but I don't understand its conclusion and can't get a solution from it. – hrl Nov 29 '17 at 08:37
  • @JensGustedt I am sorry for that. I will add gcc tag and remove the c++ tag,thanks.But as you know ,gcc is a compiler for many languages , I just think someone who is good at c++ may also know more about gcc complier and help me to find the answer .So that I add the c++ tag. – hrl Nov 29 '17 at 08:40
  • "So that I add the c++ tag." This is called "tag spamming" and it's considered as bad behavior. – Scheff's Cat Nov 29 '17 at 11:16
  • @Scheff I'm sorry about that. I'll pay attention next time – hrl Nov 30 '17 at 01:30

1 Answers1

-1

You need -D_GNU_SOURCE in-order to use GNU extension features/functions. You can use low level functions such as socket, ifconfig, etc.

Macro: _GNU_SOURCE If you define this macro, everything is included: ISO C89, ISO C99, POSIX.1, POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions. In the cases where POSIX.1 conflicts with BSD, the POSIX definitions take precedence.

Compiling without -D_GNU_SOURCE may run on one Linux machine without any problem but then fail on other Linux variants if they use a different C standard library.


Very straight forward answer:

  • #define _GNU_SOURCE 1 enables GNU extensions supported by the GNU C library. Define it when you're using non-standard functions and macros. In your case, you're trying to use a non-standard function that is part of POSIX, that's why you need to #define _GNU_SOURCE 1.

  • You want to get the full effect of _GNU_SOURCE but make the BSD definitions take precedence over the POSIX definitions, the use the _GNU_SOURCE.

Check this answer for more info about defining _GNU_SOURCE.

Alex Shroyer
  • 3,499
  • 2
  • 28
  • 54
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
  • This is so wrong. First of all, it's `-D_GNU_SOURCE`, not `-DGNU_SOURCE` or `-GNU_SOURCE`. Second of all, header files are located in `/usr/include`, not `/usr/lib`. – S.S. Anne Jun 14 '19 at 04:17