Unit tests via DUB

Steven Schveighoffer schveiguy at gmail.com
Sat Apr 2 15:56:29 UTC 2022


On 4/2/22 7:53 AM, alexanderzhirov wrote:
> I don't quite understand why compiling unit tests using DUB doesn't work.
> 
> JSON:
> ```json
> {
>    "authors": [
>      "alexander"
>    ],
>    "description": "Array Slicing",
>    "license": "proprietary",
>    "name": "array_slicing",
>    "targetName": "program",
>    "targetPath": "bin"
> }
> ```
> Projects structure:
> ```sh
> ├── bin
> ├── dub.json
> └── source
>      └── app.d
> ```
> Errors:
> ```sh
> No source files found in configuration 'library'. Falling back to "dub 
> -b unittest".
> Performing "unittest" build using /usr/bin/dmd for x86_64.
> array_slicing ~master: building configuration "application"...
> Linking...
> /usr/bin/ld: 
> /usr/lib64/gcc/x86_64-solus-linux/11/../../../../lib64/crt1.o: в функции 
> «_start»:
> /home/build/YPKG/root/glibc/build/glibc.git/csu/../sysdeps/x86_64/start.S:110: 
> неопределённая ссылка на «main»
> collect2: ошибка: выполнение ld завершилось с кодом возврата 1
> Error: linker exited with status 1
> /usr/bin/dmd failed with exit code 1.
> ```
> 

So to explain a couple things here:

1. you have a source file called `app.d`, which means to dub that this 
is an executable target, not a library.
2. `dub test` is made for *libraries*. What it does is add its own test 
runner and main function. Because it thinks you are trying to test an 
executable, it defaults to running `dub -b unittest` which is the same 
as building an application, but just adds the `-unittest` flag to the 
compiler.
3. Your build has no `main` function, so the linker fails.

Solutions:

1. Add a `main()` function to your program.
2. Change the name of your source file so it doesn't think this is an 
application
3. Declare the project is a library via `targetType`.

Without knowing what you are doing with the build, I'd say likely you 
should add the main function as suggested by Salih Dincer.

-Steve


More information about the Digitalmars-d-learn mailing list