0
votes

I created a basic C++/CLI library, it has two classes. One is supposed to be the unmanaged wrapper that will be called through by regular C++ implementation. The CLI project builds correctly but when I try to call it I get the error: "C1190: managed targeted code requires a '/clr' option". I don't want to use the clr option on the C++ implementation. Am I doing something wrong?

C++/CLI code:

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;
    }
}

Regular C++ code:

#include "..\Preprocessor_Wrapper\PreprocessorNative.h"
/*Just with the include I get the error, even before I try to use the class*/

P.S. I already added the .obj file path to the linker on the C++ project.

1
You completely reversed PreprocessorWrapper.cpp (fine without /clr) and ProcessorNative.cpp (/clr required). - Hans Passant

1 Answers

0
votes

The most convenient approach to accomplish what you need is to create two project: managed C++/CLI and unmanaged C++ wrapper as @cicada suggested. You can also keep same sources in same files and divide them with using #pragma managed and #pragma unmanaged. And @hans-passant is right, you mixed up the syntax of the C++ and C++/CLI.