www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why does the importC example not compile?

reply kdevel <kdevel vogtner.de> writes:
https://dlang.org/spec/importc.html

`square.c`
```
int square(int i)
{
    return i * i;
}
```

`demo.d`
```
import std.stdio;
import square;
void main()
{
     int i = 7;
     writefln("The square of %s is %s", i, square(i));
}
```

```
$ dmd --version
DMD64 D Compiler v2.101.1
Copyright (C) 1999-2022 by The D Language Foundation, All Rights 
Reserved written by Walter Bright
$ dmd demo.d square.c
demo.d(6): Error: function expected before `()`, not `module 
square` of type `void`
```

I would have expected that each and every piece of code in the 
documentation is automatically compiled with any new compiler 
release.
Jan 13 2023
next sibling parent novice2 <sorry noem.ail> writes:
try to rename function to distinguish from source module
Jan 13 2023
prev sibling parent reply Dennis <dkorpel gmail.com> writes:
Thanks for reporting this. PR:
https://github.com/dlang/dlang.org/pull/3489

On Friday, 13 January 2023 at 11:10:23 UTC, kdevel wrote:
 I would have expected that each and every piece of code in the 
 documentation is automatically compiled with any new compiler 
 release.
Individual D snippets can be tested when given the right DDoc macro, such as `SPEC_RUNNABLE_EXAMPLE_RUN` or `SPEC_RUNNABLE_EXAMPLE_FAIL`. (Thanks to Nick Treleaven for adding it to many examples that didn't have it before!) I don't think there's a way to test examples of separate compilation in the spec currently.
Jan 13 2023
parent reply kdevel <kdevel vogtner.de> writes:
On Friday, 13 January 2023 at 12:20:23 UTC, Dennis wrote:
 I don't think there's a way to test examples of separate 
 compilation in the spec currently.
What must be added or changed in order to test every example which is intended to produce an executable?
Jan 13 2023
parent reply Dennis <dkorpel gmail.com> writes:
On Friday, 13 January 2023 at 12:33:28 UTC, kdevel wrote:
 What must be added or changed in order to test every example 
 which is intended to produce an executable?
Support for separate compilation / ImportC would need to be added to dspec_tester: https://github.com/dlang/dlang.org/blob/master/tools/dspec_tester.d
Jan 13 2023
parent Gavin Ray <ray.gavin97 gmail.com> writes:
On Friday, 13 January 2023 at 12:46:33 UTC, Dennis wrote:
 On Friday, 13 January 2023 at 12:33:28 UTC, kdevel wrote:
 What must be added or changed in order to test every example 
 which is intended to produce an executable?
Support for separate compilation / ImportC would need to be added to dspec_tester: https://github.com/dlang/dlang.org/blob/master/tools/dspec_tester.d
I ran into this issue too, what I discovered is that when you import `square`, it is importing the file `square.c` (if one exists). Because you have a function called `square` in a file called `square`, confusingly, you want `square.square`. Hence the error message about trying to invoke parens `()` on a module. If you rename the file to `my_c_funcs.c` and then you do: ```d import std.stdio; import my_c_funcs; void main() { int i = 7; writefln("The square of %s is %s", i, my_c_funcs.square(i)); } ``` ```sh (dmd-nightly)[user MSI source]$ dmd app.d my_c_funcs.c (dmd-nightly)[user MSI source]$ ./app The square of 7 is 49 ```
Jan 14 2023