Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler ignores #define _GNU_SOURCE

  • I am not a native english speaker, so please excuse any spelling or grammar mistakes
    • I am not a compiling expert, nor do I have any useful experience with builds and their errors
    • I am C# programmer and mainly working in an MS Enviroment
    • I only know the 3 "must know to survive in Linux commands" "./configure, make & make install" from my little Linux Experience

My Development Enviroment

  • I am using a Windows 7 Workstation
    with Cygwin and MinGW (as Linux 'Replacement') to compile.

The Problem

I want to compile C source code on windows, which is primary written for Linux distributions.
/Configure works without problems.
If I use the command make to compile the sources, I run into following error:

Error
grib_keys.c:50:34:
error: 'alphasort' undeclared (first use in this function)

Research:

My Research proved me, that this problem already has been solved,
but unfortunately, the answer isn't working for me.

Implicit declaration of scandir; alphasort is undeclared
http://ubuntuforums.org/archive/index.php/t-1653576.html

The solution says, that I only have to include following: #define _GNU_SOURCE
Which I tried, but as already stated, it doesn't work.

I included it in following files:

- grib_keys.c
- config.h

and tried to compile them with concurrent and not concurrent inclusion.

In the end, the important parts of the files looked like this:

config.h
********
/* Add #define _GNU_SOURCE to solve  "'alphasort' undeclared" error  */
#define _GNU_SOURCE

grib_keys.c
***********
#define _GNU_SOURCE
count = scandir(dir, &files, 0, alphasort);

What I want to achive & to know:

  • I want to compile the whole sourcecode of below named API, to use the binaries on a windows operating system.
  • Also I would like to know, whether I wrote the "#define _GNU_SOURCE"-Tag
    to the right place, or if I made a mistake.

Downloads:

Api
https://software.ecmwf.int/wiki/display/GRIB/Home


1 Answers

If you're going to declare feature-test macros such as _GNU_SOURCE, you must ensure that the preprocessor sees them before it sees any code that uses them. That generally means they have to be processed before any system headers. The best placement, therefore, is at the top of each of your C source files (not headers), before any #include directives.

With that said, you need a solution that applies to the C library you're actually using, and its development headers. For MinGW, it seems that would be Microsoft's C library, which does not appear to document an alphasort() function.

Even if you were using glibc (Cygwin's version, for instance) my glibc docs claim that the needed feature-test macro for alphasort() is either _BSD_SOURCE or _SVID_SOURCE, not _GNU_SOURCE. Since glibc 2.10, it looks like it's probably best to use _POSIX_C_SOURCE >= 200809L, or _XOPEN_SOURCE >= 700, as these reflect the fact that the function was standardized in POSIX.1-2008.

like image 129
John Bollinger Avatar answered Oct 18 '25 08:10

John Bollinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!