简体   繁体   English

尝试将C ++项目转换为与CLR兼容的静态库

[英]Trying to convert a C++ project to a static library compatible with CLR

So I've managed to compile a project I was developing into a .lib, successfully, using the /clr flag. 因此,我已经成功使用/ clr标志将正在开发的项目编译为.lib。 What I'm now struggling to do is create a CLR Class Library wrapper for it, to allow me to use it with .NET. 我现在正在努力做的是为其创建CLR类库包装,以允许我将其与.NET一起使用。

I'm getting the following errors when I try compile my CLR Class Library: 尝试编译CLR类库时出现以下错误:

Renderer.obj : error LNK2028: unresolved token (0A000017) "public: __thiscall cMain::cMain(void)" (??0cMain@@$$FQAE@XZ) referenced in function "private: void __clrcall Renderer::GraphicsBox::NewGraphicsBox(int,int,int,int)" (?NewGraphicsBox@GraphicsBox@Renderer@@$$FA$AAMXHHHH@Z)

Renderer.obj : error LNK2019: unresolved external symbol "public: __thiscall cMain::cMain(void)" (??0cMain@@$$FQAE@XZ) referenced in function "private: void __clrcall Renderer::GraphicsBox::NewGraphicsBox(int,int,int,int)" (?NewGraphicsBox@GraphicsBox@Renderer@@$$FA$AAMXHHHH@Z)

I've added the relevant entries to the include directories, as well as the relevant entries to the lib directories. 我已将相关条目添加到include目录中,并将相关条目添加到lib目录中。

Code looks like this: 代码如下:

stdafx.h stdafx.h

#pragma once

#include "cMain.h"

Renderer.h 渲染器

#pragma once

#include "Stdafx.h"



using namespace System;

namespace Renderer {

    public ref class GraphicsBox
    {
        GraphicsBox();
        ~GraphicsBox();

        void NewGraphicsBox(System::Int32 scrw, System::Int32 scrh, System::Int32 posx, System::Int32 posy);
        // TODO: Add your methods for this class here.
    };
}

Renderer.cpp 渲染器

#include "stdafx.h"

#include "Renderer.h"

pragma comment(lib "DX11test.lib")

using namespace Renderer;

GraphicsBox::GraphicsBox()
{

}

GraphicsBox::~GraphicsBox()
{

}

void GraphicsBox::NewGraphicsBox(System::Int32 scrw, System::Int32 scrh, System::Int32 posx, System::Int32 posy)
{
    cMain *base;

    bool result;

    base = new cMain;
    if (!base)
    {
        throw runtime_error("Failed at base = cMain");
    }
}

And the class I'm trying to reference: 我想参考的课程是:

cMain.h cMain.h

#ifndef _CMAIN_H_
#define _CMAIN_H_

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <vector>
#include <map>
#include <string>

#include "InputHandler.h"
#include "cGraphics.h"
#include "cVehicleObject.h"
#include "cVehicleModel.h"
#include "cTerrainModel.h"
#include "cLight.h"

using namespace std;

public class cMain
{
public:
    cMain();
    cMain(const cMain& other);
    ~cMain();

    bool Initialize(int scrWidth, int scrHeight);
    void Shutdown();
    void Run();

    LRESULT CALLBACK MessageHandler(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam);
private:
    bool Frame();
    void InitializeWindows(int& scrw, int& scrh);
    void CreateNewWindow(int& scrw, int& scrh, int posx, int posy);
    void ShutdownWindows();

    void ErrorDump(vector<string> errors, string filename);
    void ErrorDump(string error, string filename);

    bool SetUpLights();
    bool SetUpObjects();

    LPCWSTR m_applicationName;
    HINSTANCE m_hinstance;
    HWND m_hwnd;

    InputHandler* m_input;
    cGraphics* m_graphics;

    cObject::GameObjects m_gameObjects;

    vector<cLight> m_lights;
};

static LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam);
static cMain* ApplicationHandle = 0;

#endif

My instincts tell me it's something to do with the usage of the Windows API within the project I'm trying to set up as a lib, but I'm inexperienced in linking things together so I honestly have no idea. 我的直觉告诉我,这与我试图将其设置为lib的项目中Windows API的使用有关,但是我没有将事物链接在一起的经验,因此我真的不知道。

I want to convert the whole thing to be entirely CLR friendly, I don't want to leave any stone unturned. 我想把整个事情变成对CLR友好的,我不想丢下任何石头。 Thanks for the help 谢谢您的帮助

Linker errors has nothing to do with include path of your program, it is related to implementation of the function. 链接器错误与程序的包含路径无关,它与函数的实现有关。 In your posted example I don't see cMain.cpp , so may be you forget to implement it or you just forget to implement cMain::cMain() 在您发布的示例中,我看不到cMain.cpp ,因此可能是您忘记了实现它,或者只是忘记了实现cMain::cMain()

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

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