Bug 22332 – ImportC: declarations in imported C files conflict with D declarations.
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-24T02:44:22Z
Last change time
2021-09-24T07:20:29Z
Keywords
ImportC
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2021-09-24T02:44:22Z
When a D file imports a preprocessed c file, there are likely going to be symbols declared in both D and in C when using c standard headers. For example:
// stdio.i
int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
// hello.d
import stdio;
import core.stdc.stdio;
void main(){
printf("Hello world\n”);
}
Compiling hello.d results in the following error:
hello.d(5): Error: function `core.stdc.stdio.printf` at .../core/stdc/stdio.d/stdio.d(1274) conflicts with function `stdio.printf` at stdio.i(1)
As the probability of such a conflict in a project is very high, these conflicts need to be resolved somehow.
Comment #1 by mipri — 2021-09-24T06:14:40Z
This happens already with only D modules:
import std.uni, std.ascii;
enum x = 'x'.toUpper;
Error: function `std.ascii.toUpper!char.toUpper` at /usr/include/dlang/dmd/std/ascii.d(697) conflicts with function `std.uni.toUpper` at /usr/include/dlang/dmd/std/uni/package.d(9980)
Which can be resolved with selective imports or renamed imports or as such:
import std.uni, std.ascii;
enum x = std.ascii.toUpper('x');
In your case, stdio.printf("...") or core.stdc.stdio.printf("...")
The latter's a mouthful but you can fix that with a short name that you can also require:
import stdio;
static import cio = core.stdc.stdio;
void main() {
printf("this is from importC\n");
cio.printf("Hello, world!\n");
}