2

Can you remind me of how to make linking between C++ and C++/CLI?

I have it like this:

"hashtable" is a native code file with header where i do sample BIG RAM allocation, it compiles ok.

"BPSW" is mixed code file with header, which I use as glue between native and managed code. It compiled ok before I referenced native function Allocate();

Also, managed and native files have their precompiled header files (assume they were just autogenerated).

Hashtable.h

#include <vector>
#include <iostream>

using namespace std;

namespace Allocation
{
    void Allocate();
}

Hashtable.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();
    }
}

Build log looks like this:

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 ==========
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
sdd
  • 721
  • 9
  • 23
  • 1
    According to your build log, 'hashtable' is a seperate dll, but you don't export `Allocate` function, and I guess you don't link to dll in your 'BPSW' project – Lol4t0 May 18 '13 at 09:22
  • Can you tell me how to do it? Proper linking I mean. I`ve looked to Hashtable/Debug folder - there are lots of .obj files, no dll. I`m confused. Can you give me some reference to tutorial or like it? It's a shame, but I`m still no good in linking. Maybe I should read something to get thorough understanding. – sdd May 18 '13 at 09:26
  • See [this](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) guide. Keywords to you are: 'library', 'importing/exporting' – Lol4t0 May 18 '13 at 09:28
  • Thank you very much! I`d do this research myself before asking here, but I was too confused and in a hurry. I`ll try not to exploit SO like this anymore. – sdd May 18 '13 at 09:37
  • You know, I`m still having trouble - even after reading the article. – sdd May 18 '13 at 12:19
  • Did you change your code? Can you edit your question according your changes? – Lol4t0 May 18 '13 at 13:23
  • Well, this isn't going anywhere. Why don't you just punt the problem and use *one* project instead of two. There isn't any good reason for the native C++ code to live in a separate DLL. – Hans Passant May 18 '13 at 15:26
  • Thanks, I eventually did it. Now it`s only a matter of redirecting requests to hashtable into native code in C++/CLI. You can consider this topic closed! – sdd May 18 '13 at 16:47

0 Answers0