CMake and D

Jan Allersma janallersma at gmail.com
Thu Aug 4 20:29:30 UTC 2022


Hello,

I am trying to compile an application with both C++ and D source 
code.

First I have `main.d`:

```
extern (C++) void init();
extern (C++) void draw(int row, int column, int x, int y);
extern (C++) void render();

void main() {
     init();
     draw(3, 3, 0, 0);
     draw(3, 3, 2, 2);
     draw(3, 3, 1, 1);
     render();
}
```

And I have `viewer.cpp`:

```
void init() { ... }
void draw() { ... }
void render() { ... }
```

Lastly, I have `CMakeLists.txt`:

```
cmake_minimum_required (VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)

# Below line is for using C++14 standard
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")

# Our project name
project (framework)

add_executable(framework main.o viewer.cpp)

# DLang
#INCLUDE(main.o)

# For finding our SDL2 package!
INCLUDE(FindPkgConfig)

# Searching SDL2 package.
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)

# Including our external library's directory for header files!
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})

# Linking SDL2 library with our project.
target_link_libraries(framework ${SDL2_LIBRARIES})
```

I execute the following lines in my terminal:

```
dmd -c main.d
cd build
cmake ..
make
```

And I get this error:

```
/usr/bin/ld: ../main.o: in function `main':
main.d:(.text.main[main]+0xc): undefined reference to 
`_d_run_main'
/usr/bin/ld: ../main.o:(.text.d_dso_init[.data.d_dso_rec]+0x22): 
undefined reference to `_d_dso_registry'
```

So something goes wrong with linking, but I dont know what.


More information about the Digitalmars-d-learn mailing list