Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何链接C++/CLI库到C++;应用 我在C++应用程序中使用了C+YDLL,制作了一个C++/CLI包装器。我使用visualstudio来管理项目和依赖项,我让它以这种方式工作_C++_Cmake_C++ Cli - Fatal编程技术网

如何链接C++/CLI库到C++;应用 我在C++应用程序中使用了C+YDLL,制作了一个C++/CLI包装器。我使用visualstudio来管理项目和依赖项,我让它以这种方式工作

如何链接C++/CLI库到C++;应用 我在C++应用程序中使用了C+YDLL,制作了一个C++/CLI包装器。我使用visualstudio来管理项目和依赖项,我让它以这种方式工作,c++,cmake,c++-cli,C++,Cmake,C++ Cli,但是,当我使用CMake链接VisualStudio为我构建的库时,我无法让它工作 cmake_minimum_required (VERSION 3.12) project(playground CXX) SET(CMAKE_CXX_STANDARD 17) set(PLAYGROUND_SOURCE_DIR ${CCMAKE_CURRENT_SOURCE_DIR}) set(PLAYGROUND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) set(

但是,当我使用CMake链接VisualStudio为我构建的库时,我无法让它工作

cmake_minimum_required (VERSION 3.12)

project(playground CXX)

SET(CMAKE_CXX_STANDARD 17)

set(PLAYGROUND_SOURCE_DIR ${CCMAKE_CURRENT_SOURCE_DIR})
set(PLAYGROUND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})

set(PLAYGROUND_SRC
main.cpp)

add_executable(Playground ${PLAYGROUND_SRC} ${PLAYGROUND_HDR})

find_library(comm_dll CommWrapper Lib/CommDLL)

target_link_libraries(Playground PUBLIC
${comm_dll}
)

使用此方法会得到一个“未解决的外部符号错误”。我将.dll、.lib和.h文件放在同一个文件夹中。

正如Hans Passant所说:您必须将C++/CLI代码编译为动态库,以便能够从非托管应用程序中使用它。CLI/托管代码不能从静态库运行,也不能驻留在静态库中。如果将C++/CLI库目标从静态库更改为动态库,则可以成功编译非托管C++应用程序。 我的一个想法是:如果您使用混合模式C++/CLI DLL来使用托管功能,我认为您会做得更好—您将能够完全摆脱对CLR的引用

元素类的这种混合模式包装器的标题如下所示:

#pragma once

#pragma unmanaged

#if defined(LIB_EXPORT)
#define DECLSPEC_CLASS __declspec(dllexport)
#else
#define DECLSPEC_CLASS __declspec(dllimport)
#endif

class ElementWrapperPrivate;

class __declspec(dllexport) ElementWrapper
{
private:
    ElementWrapperPrivate* helper;

public:
    ElementWrapper();
    ~ElementWrapper();
public:
    void ExecuteCommand();
};
#include "ElementWrapper.h"
#pragma managed

#include "Element.h"
#include <msclr\auto_gcroot.h>

using namespace System::Runtime::InteropServices;

class ElementWrapperPrivate
{
public:
    msclr::auto_gcroot<Element^> elementInst;  // For Managed-to-Unmanaged marshalling
};

ElementWrapper::ElementWrapper()
{
    helper = new ElementWrapperPrivate();
    helper->elementInst = gcnew Element();
}

ElementWrapper::~ElementWrapper()
{
    delete helper;
}

void ElementWrapper::ExecuteCommand()
{
    helper->elementInst->ExecuteCommand();
}
实施过程如下所示:

#pragma once

#pragma unmanaged

#if defined(LIB_EXPORT)
#define DECLSPEC_CLASS __declspec(dllexport)
#else
#define DECLSPEC_CLASS __declspec(dllimport)
#endif

class ElementWrapperPrivate;

class __declspec(dllexport) ElementWrapper
{
private:
    ElementWrapperPrivate* helper;

public:
    ElementWrapper();
    ~ElementWrapper();
public:
    void ExecuteCommand();
};
#include "ElementWrapper.h"
#pragma managed

#include "Element.h"
#include <msclr\auto_gcroot.h>

using namespace System::Runtime::InteropServices;

class ElementWrapperPrivate
{
public:
    msclr::auto_gcroot<Element^> elementInst;  // For Managed-to-Unmanaged marshalling
};

ElementWrapper::ElementWrapper()
{
    helper = new ElementWrapperPrivate();
    helper->elementInst = gcnew Element();
}

ElementWrapper::~ElementWrapper()
{
    delete helper;
}

void ElementWrapper::ExecuteCommand()
{
    helper->elementInst->ExecuteCommand();
}
#包括“ElementWrapper.h”
#布拉格管理
#包括“Element.h”
#包括
使用名称空间System::Runtime::InteropServices;
类ElementWrapperPrivate
{
公众:
msclr::auto_gcroot elementInst;//用于托管到非托管封送
};
ElementWrapper::ElementWrapper()
{
helper=newelementwrapperprivate();
helper->elementInst=gcnew-Element();
}
ElementWrapper::~ElementWrapper()
{
删除助手;
}
void ElementWrapper::ExecuteCommand()
{
helper->elementInst->ExecuteCommand();
}

然后只需将Element.cpp+ElementWrapper.cpp编译成DLL,并在非托管应用程序中使用ElementWrapper.h。

我已经这样做了。我在一个非托管C++应用程序中得到包装器,但是VisualStudio帮助了依赖关系。我的问题是如何让它与CMake一起工作。