4
  • 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

Community
  • 1
  • 1
  • 3
    Try adding `-D_GNU_SOURCE` to your compiler invocation (or maybe to your `CFLAGS`). – Kerrek SB Mar 19 '15 at 16:17
  • Maybe MinGW simply doesn't implement that function? – cremno Mar 19 '15 at 16:36
  • @cremno, I'm not sure what version of glibc the OP's MinGW provides, but glibc has had `alphasort()` for a long time. As in, it's in every version of glibc 2. – John Bollinger Mar 19 '15 at 16:38
  • 2
    "with Cygwin and MinGW" - this may very well be the problem. These two are two distinct products and do not need to be used both. Based on your post, I'd say you want to use Cygwin and not MinGW (see [here](http://stackoverflow.com/questions/771756/what-is-the-difference-between-cygwin-and-mingw) for more info). – fstanis Mar 19 '15 at 16:39
  • 1
    @JohnBollinger: MinGW doesn't use glibc and Cygwin uses newlib (which apparently implements it). – cremno Mar 19 '15 at 16:41
  • @JohnBollinger it doesn't, MinGW doesn't provide ANY version of glibc because glibc would require kernel support that doesn't exist on windows. – Mgetz Mar 19 '15 at 16:43
  • Ok, my bad. But if MinGW is not using glibc, then that certainly makes it doubtful that `_GNU_SOURCE` is the correct feature test macro to get `alphasort()` declared. The documentation of the *relevant* C library (Microsoft's?) should be consulted. The function may or may not be available at all. – John Bollinger Mar 19 '15 at 16:52
  • @JohnBollinger: For Cygwin your answer may have been correct. Can you restore it? And MinGW(-w64) really doesn't implement it. – cremno Mar 19 '15 at 16:58
  • Thank you for your help and valuable time, @ cremno, I could have made a mistake as I tagged mingw32, I am maybe using the 64 bit version, but I can't currently double check it. –  Mar 19 '15 at 18:04

1 Answers1

4

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • `_GNU_SOURCE` requests everything, including the latest `_XOPEN_SOURCE` at the time your glibc (or newlib) was released, so it should be sufficient to get a declaration of `alphasort` in cygwin. Useless on mingw though, which is the main problem. –  Mar 19 '15 at 20:46
  • Actually it looks like MinGW works fine with `_GNU_SOURCE` but mingw64 does not... – Simon Sobisch Dec 08 '19 at 19:54