Compililng C++ and D together without going mad
IGotD-
nise at nise.com
Wed Apr 29 20:12:23 UTC 2020
On Wednesday, 29 April 2020 at 10:25:31 UTC, Jan Hönig wrote:
> In my pet project, I am using some C++ libraries. The main
> file/function is also C++. All of it successfully compiles with
> cmake. Now I want to add some functionality by calling my own D
> functions (which use some other modules/projects/phobos).
>
> My questions is, what is the "proper" building tool/way.
> Do I extend my CMakeFile.txt with additional instructions,
> which basically do a "dmd -c ...".
> Do I use dub to compile the D part as a library, and use that
> in the cmake file?
> Do I use dub to compile the C++ part as well?
>
> Surely somebody has done something similar.
> Can you pinpoint me to some examples? I could go from there.
For mixed language project use Cmake only, you do not need to
involve dub.
You extend Cmake to compile D files. You add these files with the
contents.
CMakeDCompiler.cmake.in:
set(CMAKE_D_COMPILER "@CMAKE_D_COMPILER@")
set(CMAKE_D_COMPILER_LOADED 1)
set(CMAKE_D_SOURCE_FILE_EXTENSIONS
@CMAKE_D_SOURCE_FILE_EXTENSIONS@)
set(CMAKE_D_OUTPUT_EXTENSION @CMAKE_D_OUTPUT_EXTENSION@)
set(CMAKE_D_COMPILER_ENV_VAR "@CMAKE_D_COMPILER_ENV_VAR@")
CMakeDetermineDCompiler.cmake:
# Find the D compiler
find_program(
CMAKE_D_COMPILER
NAMES "ldc2"
HINTS "${CMAKE_SOURCE_DIR}"
DOC "LDC compiler"
)
mark_as_advanced(CMAKE_D_COMPILER)
set(CMAKE_D_SOURCE_FILE_EXTENSIONS d)
set(CMAKE_D_OUTPUT_EXTENSION .obj)
set(CMAKE_D_COMPILER_ENV_VAR "D")
# Configure variables set in this file for fast reload later on
configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeDCompiler.cmake.in
${CMAKE_PLATFORM_INFO_DIR}/CMakeDCompiler.cmake)
CMakeDInformation.cmake:
if(NOT CMAKE_D_COMPILE_OBJECT)
set(CMAKE_D_COMPILE_OBJECT "<CMAKE_D_COMPILER>
${CMAKE_D_FLAGS} -c --of=<OBJECT> <SOURCE>")
endif()
set(CMAKE_D_INFORMATION_LOADED 1)
CMakeTestDCompiler.cmake:
# For now just do nothing in here
set(CMAKE_D_COMPILER_WORKS 1 CACHE INTERNAL "")
Put these files somewhere and tell Cmake were to find them.
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}"
${THE_PATH_TO_THE_FILES}" CACHE INTERNAL "")
You need to enable D language to use it with.
enable_language(D)
You can make this more complex but this will make you start
quickly. Now Cmake will compile D files as soon as it encounters
source files with the .d extension. You can gladly mix C++ and D
files and it will link them all together.
More information about the Digitalmars-d-learn
mailing list