CMake and D

Jan Allersma janallersma at
Sat Aug 6 18:22:45 UTC 2022

On Saturday, 6 August 2022 at 13:52:28 UTC, Jan Allersma wrote:
> I forgot that I have to add some DUB dependencies. So I have to 
> use `dub` instead of `dmd`.
> My strategy is to
> 1) Create a D function which will be called in C++.
> 2) Build a static library containing the D function (using `dub 
> build`).
> 3) Build an executable file with CMake.
> However, I get an `ld` error:
> ```
> /usr/bin/ld: 
> ../libhello-sdl.a(app_1_144.o):(.text.d_dso_init[.data.d_dso_rec]+0x22): undefined reference to `_d_dso_registry'
> collect2: error: ld returned 1 exit status
> make[2]: *** [CMakeFiles/framework.dir/build.make:85: 
> framework] Fout 1
> make[1]: *** [CMakeFiles/Makefile2:76: 
> CMakeFiles/framework.dir/all] Fout 2
> make: *** [Makefile:84: all] Fout 2
> ```
> I do not know what `_d_dso_registry` is. Therefore, I don't 
> know how to solve this problem.

I figured out a strategy to solve te problem:

1) Create a C++ function which will be called in D.
2) Build a static C++ library with CMake and add dependencies (In 
my case: SDL libraries)
3) Create a new project (`dub init`).
4) Add these lines to `dub.json`:

	"dflags": ["-L-lstdc++"],
	"lflags": ["-Lbuild", "-lframework", "-lSDL2"],

- `-L-lstdc++` is needed to use standard C++ functions.
- `-Lbuild` is to add the `build` directory a library path (this 
is the directory where my earlier compiled C++ library is in).
- `-lframework` is used to link my earlier compiled C++ library 
- And finally `-lSDL2` because that is a dependency used by my 
C++ library.

Now I can add dependencies for my D code as well using `dub add`!

More information about the Digitalmars-d-learn mailing list