On Tuesday, 7 January 2025 at 17:55:50 UTC, sfp wrote:
File `meson.build`:
```
project('unittest', 'd')

mainlib = library('mainlib', ['blah.d'])

executable('maintest', ['main.d'], link_with: [mainlib], d_unittest: true)
```

Your first version is indeed wrong because the library is not compiled at all in unittest version. you can see this with `-v` option of `meson compile` :

```
# '-unittest' opt not present
╰─> meson compile -v
...
[2/5] ldc2 -I=libmainlib.so.p -I=. -I=.. -enable-color -wi -g -d-debug -relocation-model=pic -makedeps=libmainlib.so.p/blah.d.o.deps -of=libmainlib.so.p/blah.d.o -c ../blah.d
...
```

-------------

The second sample is pretty fine, unittests are actually activated for the library. The strange behavior comes from D runtime, not meson : the main is not being runned because by default if a unittest is present, the runtime won't run the main function.

see : https://dlang.org/phobos/core_runtime.html#.runModuleUnitTests

To have both unittests and main running, you'll have to set the `--DRT-testmode` option to "run-main". You can do it either when running the binary :

``` bash
./testmain --DRT-testmode="run-main"
```

or in the source code :

```
╰─> cat main.d
extern(C) __gshared string[] rt_options = [ "testmode=run-main"];

import blah;

void main() {
  f();
}
```

You can even put it in your lib :
```
╰─> cat blah.d
extern(C) __gshared string[] rt_options = [ "testmode=run-main"];
...
```

-------------

Note: the default 'testmode' was "run-main" in previous runtime version (< 2.080 I believe), so you might get different behavior with old compilers

Note2: I'm not 100% sure what happens if rt_options is set in multiples files with a different value. Someone knows ?

Reply via email to