General Development

DLL Creation... – jestermax

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
ok, i'm using dev-c++ to make a DLL for some code. i've been trying to get just a simple demo going but murphy is at it again with his laws.

heres the export definition:


#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

heres my class, defined in dll.h:


class DLLIMPORT DllClass
{
public:
void Hello(){
std::cout << "Hello";
}

void Hello2();
};


now, i am able to instantiate the object in another project which i imported the library to. i can call Hello() and it'll work.
Now my problem is, for some reason when i call Hello2() the program has a linker error. Hello2() is defined as follows in dll.cpp in the dll project:


void DllClass::Hello2(){
std::cout << "hello2";
}

the dll project compiles fine and life is good on that side, but when i try to call code that is defined in a .cpp file, it hates me.

I have read somewhere that you CAN put code in a .cpp file in a DLL so someone please tell me why this is not working.

[This message has been edited by jestermax (edited July 18, 2006).]

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
What exactly is the linker error? You are correct. This should not be a problem. Did you do a complete REBUILD of the code? It's quite strange for Hello to work, but not Hello2. As long as they are both public, there should be no problem.

I am more familiar with VS.NET, so I can't quite help with dev-c++ configuration.

Be sure to check the .lib file/directory your client project is looking at? Is it possible you added Hello2, compiled, and then left the .lib file in some build directory? Do you need to copy the new .lib file to some other location where your client project is actually looking for the .lib file?

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
the client project and the dll project are in the same folder. i always make sure to clean the project files out when i make changes to a program.
The compiled DLL is in the same folder and i added that folder to the Library Directories section (the client wouldn't compile without me adding it there).

the only difference between Hello() and Hello2() is that Hello2() is in a .cpp file:

<BLOCKQUOTE><table width=80% border=0 bgcolor="#FFFFF" CELLPADDING="2" CELLSPACING="2"><TR><TD><font size="3" face="Courier" color="#000000"><pre>
#include "dll.h"

void DllClass::Hello2(){
std::cout << "hello2";
}
</pre></font></TD></TR></TABLE></BLOCKQUOTE>

maybe i'll try it with VS 8 tomorrow sometime


whoops, forgot to add the linker error:

Undefined reference to <some compiler jibberish> DllClass6Hello2Env'

[This message has been edited by jestermax (edited July 18, 2006).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
UGHHHH i just read a posting on this site:
http://support.realsoftware.com/listarchives/realbasic-plugins/2005-01/msg00007.html

at the bottom the guy says you can't export c++ classes. can someone please tell me whats going on here? it works fine if the code is in the same header file but dies if its in a .cpp file. is this normal? or did i just mess up?

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
have a look at:

http://www.projectnresource.com/phpbb/viewtopic.php?t=14

main things to check out are that you are actually including the .lib file in your project setting or through #pragma comment(lib, "dllName.lib"), also that you are including the header file which has the "__declspec(dllimport)" of your class...

Just sounds like your either not including the .lib file in the project settings or else you don't have the "__declspec(dllimport)" of your class...

------------------
"But it is God who judges: He brings one down, he exalts another." - Psalm 75:7

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
oh... lol... Posted within 2 mins of each other, didn't see your last message... if your on msn, give me a buzz at dartsman (at) hotmail.com I could help you out more...

I've got my engine which I'm currently working on (classes and all) wrapped up nicely in a dll... working fine too...

------------------
"But it is God who judges: He brings one down, he exalts another." - Psalm 75:7

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
argh... again stopped by the whole "service temporarily unavailable" thingo... stupid spam threads, I totally blame them grrr...

anywho, check out:

http://www.projectnresource.com/phpbb/viewtopic.php?p=627#627

------------------
"But it is God who judges: He brings one down, he exalts another." - Psalm 75:7

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
YES!!! the flying dartsman figured it out. i was adding the library improperly to the project.
i had:
-Project.lib
when it should be:
libProject.a

haha, and as i'm writing this post i just realized why it worked if the code was in the header file. The projects were in the same directory and so it was including the header file that already contained the code. So it wasn't even bothering with the DLL.

Anyways, it works now and if you're using dev-c++ then thats what you need to do.