用C++和C#.NET6 SDK项目使用PINVOKE并复制DLL
我有一个具有C/C ++中本机组件的项目,以及一些使用.NET使用P/Invoke调用本机DLL的Windows托管项目。因此,我想拥有一个Cmake生成的.NET C#项目,该项目将构建托管代码和复制所需的本机DLL,以便可以在没有其他步骤的情况下运行它Visual Studio)。
通过添加自定义命令来复制DLL,这是与.NET 4.8一起使用的,但是我最近想尝试使用SDK风格的VS项目尝试.NET 6:
set_target_properties(${this_target} PROPERTIES
DOTNET_SDK "Microsoft.NET.Sdk"
DOTNET_TARGET_FRAMEWORK "net6.0")
我以前复制本机DLLS的解决方案是定义一个自定义功能,例如这可能会复制所需的DLL:
# 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()
在每个托管项目中,我会在需要时添加它:
copy_native_dlls(${this_target} $<TARGET_FILE_DIR:${this_target}>)
不幸的是,当我尝试使用SDK式项目转到.NET 6时,我无法再使用> add_custom_command
:
请注意,此版本的CMake中的Visual Studio Generator尚未学会支持.NET SDK式项目中的add_custom_command()。当前,将自定义命令连接到目标dotnet_sdk属性集的目标是错误。
还有其他方法可以将我项目中构建的.dll添加到另一个项目中,以便在构建它时将其复制到输出目录中?我希望有一些方法可以指向CMAKE中的这些DLL并正确设置依赖项
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}/lt;CONFIG>/native_dll/native1.dll ${output_directory}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${build_binaries_path}/lt;CONFIG>/native_dll/native1.pdb ${output_directory}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${build_binaries_path}/lt;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} lt;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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这听起来不错,但是我能使它起作用的只是这个
I know it sounds wrong but only way that I can make it work is this
仅供参考,这就是我处理的方式:
像my_native_dll_helper.csproj一样拥有一个虚拟csproj
,然后将其作为CMAKE中的外部项目创建
,并具有对生成的托管项目的依赖性。
请注意,上面的“ my_managed_path”部分是生成的项目正在复制二进制文件的位置,因此将本机DLL复制到同一位置。
FYI, this is how I handled it:
Have a dummy csproj like my_native_dll_helper.csproj
Then create it as an external project in cmake
And have that as a dependency on the generated managed project.
Note that the "my_managed_path" part above is where the generated project is copying binaries so this will copy the native DLL's to the same place.