Cannot compile betterC app in windows using stderr

Petar Petar
Wed Feb 2 12:19:31 UTC 2022


On Wednesday, 2 February 2022 at 11:46:47 UTC, bauss wrote:
> [..]
>
> extern(C) should imply extern in my book. I'm not sure if it 
> does, but it seems wrong if it doesn't.

No, there is a difference between **declaration** and 
**definition** at the linker level. `extern 
(C|C++|C|Objective-C|Windows|System)` sets the mangling of a 
variable declaration or definition. `extern` changes a variable 
definition to a declaration.

Check the following cases:


```d
import core.stdc.stdio : FILE, printf, fprintf;

shared FILE* stderr; // 1.
// extern (D) shared FILE* stderr; // 2.
// extern (C) shared FILE* stderr; // 3.
// extern shared FILE* stderr; // 4.
// extern extern (C) shared FILE* stderr; // 5.


pragma (msg, stderr.mangleof, "\n");

void main()
{
     if (stderr)
         fprintf(stderr, "stderr exists.\n");
     else
         printf("stderr doesn't exist.\n");
}
```

1. `shared FILE* stderr;` -> A global variable `stderr` 
**declared** and **defined** in the current module with D linkage
     * Compiles and links ok.
     * Prints `_D9onlineapp6stderrOPS4core4stdc5stdio8_IO_FILE` at 
compile-time.
     * Prints `stderr doesn't exist.` at run-time.
2. `extern (D) shared FILE* stderr;` -> Same as above - `extern 
(D)` is the default linkage.
3. `extern (C) shared FILE* stderr;` -> A global variable 
`stderr` **declared** and **defined** in the current module with 
C linkage
     * Compiles and links ok.
     * Prints `stderr` at compile-time.
     * Prints `stderr doesn't exist.` at run-time.
4. `extern shared FILE* stderr;` -> A global variable `stderr` 
**declared** but **not defined** in the current module with D 
linkage. It has to be defined in another module (library)
     * Compiles ok, but fails to link.
     * Prints `_D9onlineapp6stderrOPS4core4stdc5stdio8_IO_FILE` at 
compile-time.
     * Doesn't run
6. `extern extern (C) shared FILE* stderr;` -> A global variable 
`stderr` **declared** but **not defined** in the current module 
with C linkage. It has to be defined in another module (library)
     * Compiles and links ok (the compiler/linker driver 
automatically links libc).
     * Prints `stderr` at compile-time.
     * Prints `stderr exists.` at run-time.


More information about the Digitalmars-d mailing list