2

你能提醒我如何在 C++ 和 C++/CLI 之间建立链接吗?

我有这样的:

“hashtable”是一个带有标题的本机代码文件,我在其中对 BIG RAM 分配进行示例,它可以编译。

“BPSW”是带有标题的混合代码文件,我将其用作本机代码和托管代码之间的粘合剂。在我引用本机函数 Allocate(); 之前它编译正常;

此外,托管文件和本机文件具有预编译的头文件(假设它们只是自动生成的)。

哈希表.h

#include <vector>
#include <iostream>

using namespace std;

namespace Allocation
{
    void Allocate();
}

哈希表.cpp

#include "stdafx.h"
#include "Hashtable.h"

namespace Allocation
{
    void Allocate()
    {
        // test RAM allocation in native code
        vector<int>* v = new vector<int>( 250 * 1000000 );

        cout << "Ready" << endl;

        int a;
        cin >> a;

        delete v;
    }
}

BPSW.hxx

#pragma once

#pragma unmanaged
#include <vector>

#pragma managed

namespace BPSW
{
        public value class Wrapper
        {
        public:

            static void AllocateHashtable();
        };
}

BPSW.cxx

#pragma once

#pragma unmanaged
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include "../Hashtable/Hashtable.h"
using namespace std;

#pragma managed
#using <System.dll>
#include "BPSW.hxx"

//namespace Allocation
//{
//  void Allocate();
//}

namespace BPSW
{

        // some more code
        // not interesting here

    void Wrapper::AllocateHashtable()
    {
        ::Allocation::Allocate();
    }
}

构建日志如下所示:

1>------ Build started: Project: Hashtable, Configuration: Debug Win32 ------
1>  Hashtable.cpp
1>  Hashtable.vcxproj -> C:\Users\Denis\documents\visual studio 2010\Projects\NivalApp\Debug\Hashtable.dll
2>------ Build started: Project: BPSW, Configuration: Debug Win32 ------
2>BPSW.obj : error LNK2028: unresolved token (0A000327) "void __cdecl Allocation::Allocate(void)" (?Allocate@Allocation@@$$FYAXXZ) referenced in function "public: static void __clrcall BPSW::Wrapper::AllocateHashtable(void)" (?AllocateHashtable@Wrapper@BPSW@@$$FSMXXZ)
2>BPSW.obj : error LNK2019: unresolved external symbol "void __cdecl Allocation::Allocate(void)" (?Allocate@Allocation@@$$FYAXXZ) referenced in function "public: static void __clrcall BPSW::Wrapper::AllocateHashtable(void)" (?AllocateHashtable@Wrapper@BPSW@@$$FSMXXZ)
2>C:\Users\Denis\documents\visual studio 2010\Projects\NivalApp\Debug\BPSW.dll : fatal error LNK1120: 2 unresolved externals
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

0 回答 0