Problem consuming DLL in VS 2012 (native C++)

Started by
4 comments, last by benstr 11 years, 6 months ago
My little "adventure" of refreshing my C++ skills with C++11 has been pretty fun, but I keep running into odd issues lol...

I'm having yet another one! I started a solution which contains a DLL project and a Win32 console application. The DLL is going to implement a small native code framework for Windows/DirectX development and the console app is just a testing application to test code and aid in debugging. But for some reason I cannot actually use/consume my DLL in my console app! :-/

In Visual Studio Pro 2008 I remember it being extremely easy... all I had to do was open the application project properties, go to "Common Properties" and click "Add new reference". Then it would show any DLL projects in the solution and I could "reference" them. Voila, it would work... just as easy as adding a reference to a C# project. But this doesn't seem to be the case with VS 2012... Doing this does NOT work. And I can't even change the reference properties like "copy local"; if I do, it just reverses any changes I make back to the default when I click "Apply"...

So I cannot seem to figure out any way to reference/consume my native DLL in my Win32 console app... what gives?! I've tried tweaking around some more project properties but to no avail. I'm not sure what's going on here and why this won't work like it used to in 2008...

Thanks,

--ATC--
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Advertisement
Under certain conditions I get this linker error:

Error 1 error LNK1104: cannot open file 'C:\Users\ATC\Documents\Visual Studio 2012\Projects\ATCWARE\bin\Debug\Win32\ATCFLib.lib' C:\Users\ATC\Documents\Visual Studio 2012\Projects\ATCWARE\src\ConsoleTest\LINK ConsoleTest

Not sure wth this is about because I'm not even outputting a LIB file, but rather a DLL... And, of course, if I remove the "reference" under "Common Properties" it tells me I have unresolved externals for any function implemented in the DLL...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Geez, sometimes I can be a major idiot lol...

It was all because I forgot to use C-linkage and add my custom _DLLEXPORT modifier to things. So the problem is now solved...

If anyone else ever has this problem, here's what you need to do:

1) Go to Configuration Manager and set the proper project dependencies (in my case "ConsoleTest" depends on "ATCFramework")
2) Under your project properties of your consuming application, go to "Common Properties" and click "Add Reference"; then reference your DLL
3) Specify C-type linkage and export your classes, structures and functions like so:

[source lang="cpp"]
extern "C"
{
typedef struct __declspec(dllexport) MY_STRUCT
{
int X, Y; // whatever
} MyStruct, *MyStructPTR;
};
[/source]

You only need to do this in your header files... not in your .c or .cpp implementation files. It's as simple as that! :-)

Have fun,

--ATC--

EDIT:

Note: Forcing C-linkage is not absolutely necessary for C++, but you are required to do so to consume something from C code. Otherwise you will get some name mangling and C code won't know what to do. So it's just good practice imo to force C-linkage in these situations. I also think it helps if you ever want to debug at the assembly/machine code level.

As an added note I don't think a function with C-linkage can return a C++ class instance. So watch what you're doing or you'll get weird linker errors that may or may not make any sense.
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
So this thread doesn't get "wasted" though, can anyone tell me how to make my DLL get copied to the output directory of the application when I build my project? If I try to change ANY options under "Common Properties" where I reference my DLL clicking "Apply" just reverses any changes I make back to the defaults. So I can't choose "Copy Local" for example...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Fixed... For some reason I couldn't get the xcopy command to work on my post-build event using VS macros... I just had to put the FULL, absolute paths in there and it works, but I'll have to reconfigure if I want to compile on another machine...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
I noticed you have spaces in your path (from the linker error). In your post-build event, did you forget to put "" around the path ("$(ProjectDir)Some\Path")?
-Benny-

This topic is closed to new replies.

Advertisement