我创建了一个基本的C ++ / CLI库,它有两个类。一个应该是通过常规C ++实现调用的非托管包装器。 CLI项目正确构建,但当我尝试调用它时,我收到错误:“C1190:托管目标代码需要'/ clr'选项”。 我不想在C ++实现上使用clr选项。我做错了吗?
C ++ / CLI代码:
ProcessorNative.cpp
#include "StdAfx.h"
#include <vcclr.h>
#using <mscorlib.dll>
#include "PreProcessorNative.h"
#include "PreprocessorWrapper.h"
namespace PreprocessorWrapper{
PreProcessorNative::PreProcessorNative(){}
PreProcessorNative::~PreProcessorNative(){}
double PreProcessorNative::Test(double d){
gcroot<PreprocessorWrapper^> _preProcessor = gcnew PreprocessorWrapper();
return _preProcessor->Test(d);
}
}
ProcessorNative.h
namespace PreprocessorWrapper {
public class __declspec(dllexport) PreProcessorNative
{
public:
PreProcessorNative();
~PreProcessorNative();
double Test(double p);
};
}
PreprocessorWrapper.h
namespace PreprocessorWrapper {
public ref class PreprocessorWrapper
{
private:
public:
PreprocessorWrapper();
double Test(double p);
};
};
PreprocessorWrapper.cpp
#include "stdafx.h"
#include "PreprocessorWrapper.h"
namespace PreprocessorWrapper{
PreprocessorWrapper::PreprocessorWrapper(){
}
double PreprocessorWrapper::Test(double p){
return p*p;
}
}
常规C ++代码:
#include "..\Preprocessor_Wrapper\PreprocessorNative.h"
/*Just with the include I get the error, even before I try to use the class*/
P.S。我已经将.obj文件路径添加到C ++项目的链接器上。
答案 0 :(得分:0)
实现您所需要的最方便的方法是创建两个项目:托管C ++ / CLI和@cicada建议的非托管C ++包装器。您也可以将相同的来源保存在同一文件中,并使用#pragma managed
和#pragma unmanaged
进行划分。 @ hans-passant是对的,你混淆了C ++和C ++ / CLI的语法。