无法在 C# 项目中添加对 C++ DLL 的引用

问题描述 投票:0回答:0

我使用带有以下 CMakeLists.txt 的 CMake 创建了一个 C++ DLL:

cmake_minimum_required(VERSION 3.10)

# Define the project and supported versions
project(MyLibrary VERSION 1.0.0)

# Add options for compiling the library
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# Add the library sources
add_library(MyLibrary SHARED src/setup.h src/setup.cpp)

# Specify the directory to search for headers
target_include_directories(MyLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)

# Specify the output name
set_target_properties(MyLibrary PROPERTIES OUTPUT_NAME "MyLibrary_${PROJECT_VERSION}")

# Specify the output path
set_target_properties(MyLibrary PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/lib")

我现在正尝试将此 DLL 导入到 C# 项目中。为此,我在 Visual Studio 2019 中右键单击该项目,选择“添加”、“引用”、“浏览”,然后选择我创建的 DLL 文件。但是,我收到错误消息

Could not add reference to 'Path\to\file.dll'. Make sure the file is accessible and that the assembly or COM component is valid.

什么可能导致此错误?如何在我的 C# 项目中成功添加对 C++ DLL 的引用?

c# c++ visual-studio cmake dll
© www.soinside.com 2019 - 2024. All rights reserved.