Lee White
Lee White

Reputation: 3739

How reference a .lib file (native C++) in a Visual C++ project?

I have a project in which I need to make a Visual C++ wrapper for a native C++ SDK, so that it can eventually be used in C#.

The SDK consists of .h files that I am correctly including (no compilation errors with these) and some .lib files which need to be referenced. And that seems to be the tricky part.

As I explained in this question, I get errors whenever I try to call a function that's part of the library. The errors look like this:

error LNK2080: unresolved token (0A000027) "public: static class vhtIOConn *__clrcall vhtIOConn::getDefault(enum vhtICRConn::DeviceType)"
error LNK2019: unresolved external symbol "public: static class vhtIOConn * __clrcall vhtIOConn::getDefault(enum vhtIOConn::DeviceType)"

As I understand, C++/CLR expects __clrdecl while native C++ offers __clrcall.

What is the proper way to go about this? I have read in many places that this is possible, but I haven't seen any practical working examples. Note that I am using Visual Studio 2010.

Upvotes: 2

Views: 657

Answers (1)

Hans Passant
Hans Passant

Reputation: 942237

You can tell what's going wrong from the linker error, note the __clrcall in the error message. That indicates that the compiler thinks that your native C++ is being compiled to MSIL, not to native code. Which compiles just fine, any compliant C++03 code can be compiled to MSIL but dies when you try to link it. You have to let it know, you can simply do so with a #pragma:

#pragma managed(push, off)
#   include "foo.h"
#pragma managed(pop)

Upvotes: 1

Related Questions