简体   繁体   English

在Cmake C#项目中使用DLL

[英]Using a DLL in a Cmake C# project

I'm trying to use CMake to build a C# DLL. 我正在尝试使用CMake构建C#DLL。 For this, I need to add a third party .dll to the project, so I can use some functions of it that I need. 为此,我需要向项目中添加第三方.dll,以便可以使用其所需的某些功能。

The problem is, I can access the library, not even 问题是,我可以访问该库,甚至无法访问

using Namespace;

works, as the Namespace is not recognized. 有效,因为无法识别Namespace

# cmake version
cmake_minimum_required (VERSION 3.8)

# project
project ("VILAPI" CSharp)

# include directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# source and library output paths and directories
set(SRCS_VILAPI "./src/main.cs")
set(OUT_DIR ${CMAKE_HOME_DIRECTORY}/bin)

# actual building process
add_library (${project} SHARED ${SRCS_VILAPI})

set_property(TARGET ${project} PROPERTY DOTNET_TARGET_FRAMEWORK_VERSION "v4.0")
set_property(TARGET ${project} PROPERTY WIN32_EXECUTABLE FALSE)
set_property(TARGET ${project} PROPERTY VS_CONFIGURATION_TYPE ClassLibrary)
set_property(TARGET ${project} PROPERTY VS_DOTNET_REFERENCES
    "System"
    "./lib/Vector.CANoe.TFS.dll"
)

set_target_properties(${project} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${OUT_DIR})
set_target_properties(${project} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${OUT_DIR})

CMake doesn't throw an error. CMake不会抛出错误。 I've also tried to add it the following way: 我也尝试通过以下方式添加它:

set_property(TARGET ${project} PROPERTY     VS_DOTNET_REFERENCE_Vector.CANoe.TFS.dll "./lib/Vector.CANoe.TFS.dll")

which was suggested in an answer on stackoverflow, but it still does not work. 这是在关于stackoverflow的答案中建议的,但仍然无法正常工作。

For third-party DLL references, you should be using VS_DOTNET_REFERENCE_<refname> property. 对于第三方DLL引用,应使用VS_DOTNET_REFERENCE_<refname>属性。 The VS_DOTNET_REFERENCES property is typically used for Visual Studio managed references only. VS_DOTNET_REFERENCES属性通常仅用于Visual Studio托管引用。

Your set_property() command using the VS_DOTNET_REFERENCE_<refname> property was close, it just needs a small change: 您使用VS_DOTNET_REFERENCE_<refname>属性的set_property()命令已关闭,只需要进行一点更改:

set_property(TARGET ${project} PROPERTY VS_DOTNET_REFERENCE_Vector.CANoe.TFS "./lib/Vector.CANoe.TFS.dll")

The <refname> should refer to the DLL file name without the .dll file extension (ie only Vector.CANoe.TFS ). <refname>应该引用不带 .dll文件扩展名的DLL文件名(即,仅Vector.CANoe.TFS )。 See this answer for another example. 参见此答案的另一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM