I have a project with native components in c/c++, as well as some Windows managed projects using .NET which uses p/invoke to call into the native DLL's. So I would like to have a cmake generated .NET C# project which will build the managed code and copy the native DLL's it needs so it can be run without additional steps (e.g. to build and run tests directly in Visual Studio).
This was working with .NET 4.8 by adding a custom command to copy the DLL's, but I recently wanted to try out .NET 6 with an SDK-style vs project:
set_target_properties(${this_target} PROPERTIES
DOTNET_SDK "Microsoft.NET.Sdk"
DOTNET_TARGET_FRAMEWORK "net6.0")
My previous solution to copy the native DLLs was to define a custom function like this, which could copy the DLL's needed:
# in the native cmakelist for my dll with target name native_dll:
function(copy_native_dlls target output_directory)
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${build_binaries_path}/$<CONFIG>/native_dll/native1.dll ${output_directory}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${build_binaries_path}/$<CONFIG>/native_dll/native1.pdb ${output_directory}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${build_binaries_path}/$<CONFIG>/native_dll/native2.dll ${output_directory}
DEPENDS native_dll
COMMENT "copying native DLLs"
)
endfunction()
And in each managed project, I would add this when I needed it:
copy_native_dlls(${this_target} $<TARGET_FILE_DIR:${this_target}>)
Unfortunately, when I try to move to .NET 6 with the SDK-style projects, I can no longer use add_custom_command: DOTNET_SDK documentation
Note The Visual Studio Generators in this version of CMake have not yet learned to support add_custom_command() in .NET SDK-style projects. It is currently an error to attach a custom command to a target with the DOTNET_SDK property set.
Is there some other way to maybe add a .DLL that is built in my project to another project so that it will be copied to the output directory when I build it? I'm hoping there is some way to point to these dlls in cmake and set up the dependencies properly