Thread overview
Why does the importC example not compile?
Jan 13, 2023
kdevel
Jan 13, 2023
novice2
Jan 13, 2023
Dennis
Jan 13, 2023
kdevel
Jan 13, 2023
Dennis
Jan 14, 2023
Gavin Ray
January 13, 2023

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.

January 13, 2023

try to rename function to distinguish from source module

January 13, 2023

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.

January 13, 2023

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?

January 13, 2023

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

January 14, 2023

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:

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));
}
(dmd-nightly)[user@MSI source]$ dmd app.d my_c_funcs.c
(dmd-nightly)[user@MSI source]$ ./app
The square of 7 is 49