www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Should importC fail on invalid C code?

reply kdevel <kdevel vogtner.de> writes:
After reading Walter's remark "ImportC sees those macro 
definitions and transforms them into manifest constant 
declarations" in

     Issue 23622 - ImportC #defines conflict with declarations

I wondered how it was implemented:

`myccode.c`
```
int getx ()
{
    return X;
#define X 1
}
```

`imc.d`
```
unittest {
    import myccode;
    assert (false, "Should img.c really compile?");
}
```

```
dmd -O -checkaction=context -unittest -main myccode.c -run imc.d
imc.d(3): [unittest] Should img.c really compile?
1/1 modules FAILED unittests
```
Jan 13 2023
parent Dennis <dkorpel gmail.com> writes:
On Friday, 13 January 2023 at 12:50:44 UTC, kdevel wrote:
 Should importC fail on invalid C code?
In general, no. The purpose is to build / interface with existing C code, not to develop new C code with it. ImportC also has its own extensions by borrowing D features such as __import, CTFE, and forward references. A strict C compiler would reject those.
Jan 13 2023