Skip to main content
GameDev.net gamedev.net
🔒 Locked

Visual C++ DLL Link Error

Started by JacksonEMG May 24, 2009 at 8:58 AM 2 replies 750+ views
Original Post
JacksonEMG
JacksonEMG
So what I'm trying to do I'm not sure it can totally be done. It's been awhile since I've done any C++. Anyway, I have created a DLL application project in Visual Studio C++ 2008 Express Edition. In the project, I have a class, say:

// Foo.h
#ifndef FOO_H
#define FOO_H

#include "stdafx.h"

using namespace std;

class Foo {
public:
	Foo(void) {}
	~Foo(void) {}
	void bar();
};
#endif

And the implementation file:

// Foo.cpp
#include "stdafx.h"

void Foo::bar() {
	// ...
}

Then when I create another regular Win32 Project within the same Solution and call the bar() method like so (I'm using UnitTest++ in this project):

// FooTest.h
#include "UnitTest++.h"
#include "Foo.h"

TEST(TestLinker) {
	Foo foo;
	foo.bar();
}

Then I get the following error:

error LNK2001: unresolved external symbol "public: void __thiscall Foo::bar(void)" (?bar@Foo@@QAEXXZ)

However, if I move the implementation of the bar method into the Foo header, everything works. Then Foo looks like so:

// Foo.h
class Foo {
public:
	Foo(void) {}
	~Foo(void) {}

	void bar() {
		// ...
	}
};

I suspect that, if this is even possible, it is a Linker option issue. I've tried adding project that contains Foo's Debug folder to the "Additional Library Dependences" option in the Linker -> Input settings for the project using this class, but this has not worked. Is this even possible to do? When creating a DLL in C++, am I allowed to use implementation files? Is there missing code or additional setup required within the DLL project? Another example of the steps I used to create the DLL are here. I appreciate any insight that anyone can offer on this. Thanks!
SiCrane
SiCrane
You need to define your Foo class as __declspec(dllexport) in the DLL and __declspec(dllimport) in the application.
JacksonEMG
JacksonEMG
Thanks for the replies, looks like I need to read up more on using DLLs.

I'll try exporting the function and see how that goes.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.