Thread overview
Is `__GNUC__` defined in dmd when using importC?
Oct 10
felixfxu
Oct 10
felixfxu
Oct 10
felixfxu
October 10

Hi,

I'm testing dmd's importC in Ubuntu 24.04, my .c/.h code has many code specific to compilers, like

#ifdef __GNUC__

I'm surprise to see that it seems like __GNUC__ is defined in dmd.

I can test with this code:

//app.d
import std.stdio;
import importc;
void main()
{
	importc.test();
}

and

//importc.c
#include <stdio.h>
void test()
{
#ifdef __GNUC__
    printf("__GNUC__ is available: %d\n", __GNUC__);
#else
    printf("__GNUC__ is NOT available\n");
#endif
}

compiled them by : dmd source/app.d source/importc.c

and the output is:

>

GNUC is available: 16

yes, the value is 16! (maybe it's set by my system? gcc-16 is not released yet! but I do have a local gcc compiled/installed from latest source)

>

/usr/local/bin/gcc --version
gcc (GCC) 16.0.0 20250930 (experimental)

My default gcc is 13:

>

/usr/bin/gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0

When the same code is run on Windows dmd, it's:

>

GNUC is NOT available

I searched the dmd source code, I don't see __GNUC__ in the source code.

What could go wrong here?

October 10

my dmd is downloaded from the website (not locally compiled from source):

>

apt search dmd
Sorting... Done
Full Text Search... Done
dmd/now 2.111.0-0 amd64 [installed,local]
Digital Mars D Compiler

and

>

dmd --version
DMD64 D Compiler v2.111.0

October 10

On Friday, 10 October 2025 at 08:34:15 UTC, felixfxu wrote:

>

Hi,

I'm testing dmd's importC in Ubuntu 24.04, my .c/.h code has many code specific to compilers, like

#ifdef __GNUC__

I'm surprise to see that it seems like __GNUC__ is defined in dmd.

Reading the https://dlang.org/spec/importc.html will help you solve the mystery. This section in particular: https://dlang.org/spec/importc.html#preprocessor

October 10

On Friday, 10 October 2025 at 09:02:33 UTC, Dejan Lekic wrote:

>

Reading the https://dlang.org/spec/importc.html will help you solve the mystery. This section in particular: https://dlang.org/spec/importc.html#preprocessor

yes, thanks, now it is clear.

Actually my default c pre-processor is my locally compiled cpp (version 16):

>

which cpp
/usr/local/bin/cpp

and it's used to pre-process the .h files, so then GNUC is 16.

But that introduce another question: dmd is not a complete gcc replacement I guess, but in the headers, all #ifdef __GNUC__ is true, it will have problems for code like this:

SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
{
#if defined(__GNUC__) && (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
    /* Count Leading Zeroes builtin in GCC.
     * http://gcc.gnu.org/onlinedocs/gcc-4.3.4/gcc/Other-Builtins.html
     */
    if (x == 0) {
        return -1;
    }
    return 31 - __builtin_clz(x);
#elif
    ...

__builtin_clz will then be undefined in dmd compiling.

October 10

On Friday, 10 October 2025 at 10:06:39 UTC, felixfxu wrote:

>

But that introduce another question: dmd is not a complete gcc replacement I guess, but in the headers, all #ifdef __GNUC__

What made you think DMD is a replacement for a C compiler? Perhaps you are confusing importC with betterC? ImportC is there to help D developers easily interface with C libraries (so you do not have to write your own bindings). betterC is an abomination that may be interesting to some people but I suggest you avoid it.

October 10

On Friday, 10 October 2025 at 10:06:39 UTC, felixfxu wrote:

>

But that introduce another question: dmd is not a complete gcc replacement I guess, but in the headers, all #ifdef __GNUC__ is true, it will have problems for code like this:

As a workaround, you can use DMD's -P switch and GCC's -U switch to disable any macros that cause problems. For example, -P=-U__GNUC__ would disable the __GNUC__ macro.