|
|
|
|
1>error LNK2001: unresolved external symbol "private: static char * PathSocket::cBuffer" (?cBuffer@PathSocket@@0PADA) error LNK2001: unresolved external symbol "private: static unsigned int * PathSocket::socket" (?socket@PathSocket@@0PAIA) error LNK2001: unresolved external symbol "private: static void * PathSocket::hIDTask" (?hIDTask@PathSocket@@0PAXA) |
|
|
|
|
|
|
|
|
any suggestions? also could someone explain me WHY is there static, and what does dllimport/dllexport does? also there are NO tutorials with variables inside dll >.< I've been following this http://msdn.microsoft.com/en-us/library/ms235636.aspx and the video I already lost, however there's no
in the video, why do we have to use that? |
__declspec(dllexport)
is the simplest way of asking the compiler to export the function or class into a dll file, __declspec(dllimport)
tells the compiler that it can find this function inside a dll file...__declspec(dllexport)
so that when you hit compile it transfers the working code into a dll, when you're not working on the project, and instead you are just including the header DLL will be defined as __declspec(dllimport)
, so this tells the compiler to import the working functionality from the dll.extern int num1;
You have to define the variable outside of the header otherwise when you include the header you'll get an error saying "multiply defined (something or other)"... The source file is only used once and this is on the compilation of your dll, so this would be a perfect place to define the variable for exportation.DLL extern int num1;
in your header, and int Demo::num1;
in your cpp.namespace Demo
, this way I can keep my code separated from other things easily, but if I wish to declare any functions I have to write DLL in front of all of there declarations.
|
|
|
|
|
|
Demo::print1();
is the simplest way of asking the compiler to export the function or class into a dll file |
Basically __declspec(dllexport) is the simplest way of asking the compiler to export the function or class into a dll file, __declspec(dllimport) tells the compiler that it can find this function inside a dll file... |
This is why it's good to use a macro for them because when you're working on the project DLL will be defined as __declspec(dllexport) so that when you hit compile it transfers the working code into a dll, when you're not working on the project, and instead you are just including the header DLL will be defined as __declspec(dllimport), so this tells the compiler to import the working functionality from the dll. |
You are getting the Unresolved External errors because in the header you're asking the compiler to export 5 functions, but in the source file you've only defined 2 of them... Leaving you with 3 missing (or "unresolved external") functions/variables. Everything that you declare in a header for exportation (whether to a dll, exe, lib, etc) must be defined somewhere. |
Dll's with variables inside? Well I've just said that everything must be declared and defined for exportation... Same with variables. You can declare a variable in the header using the "extern" keyword to indicate that a variable exists, but isn't in this file. extern int num1; You have to define the variable outside of the header otherwise when you include the header you'll get an error saying "multiply defined (something or other)"... The source file is only used once and this is on the compilation of your dll, so this would be a perfect place to define the variable for exportation. SO, DLL extern int num1; in your header, and int Demo::num1; in your cpp. |
As for the static functions, this is an easy one XD |
|
|
#pragma once
doing ?#ifdef DEMO_EPORTS
(if DEMO_EXPORTS is defined? if it means this, where did I define DEMO_EXPORTS in the code, so the compiler knows to export it?)actually, those 3 undeclared are variables, not functions, so there shouldn't be any error |
so I need to treat variables just like functions, and declare them in cpp too? (e.g. (check my source code) int PathSocket::iSockets;) |
|
|
double myClass::myStaticVar_ = 1.2345; // You can initialise it here too
(I'm not using DLL tho, but .h + .lib, does this mean a .dll wont work, and so I have to use export/import to make it work?) |
so when I'm creating a DLL it will dllexport, and when I'm using a DLL from another application, it will dllimport, right? |
actually, those 3 undeclared are variables, not functions |
actually I dont need static functions (what is static function btw? I know only what is static variable, not a function)[/quote If you're using the namespace method, or the class you're exporting is designed to be instanced and the function is working on an instance then no you don't need static. Static let's classes share variables, you said you know about this, but a function by default is also limited to a specific class instance just like it's variables, a function can only operate on the static functions do not need an instance and hence cannot work on non static variables because they require a specific instance. I hope that ain't too confusing? 5 [quote] what is#pragma once doing ? |
#pragma comment(lib, "PathSocket.lib")[code] okay, how can the compiler include PathSocket.lib before it compiles it? |
Honestly I'm not entirely sure what you mean by this, if you mean you're not using the macro then why not just add it in? Can save a lot of effort replacing dllexport with dllimport. |
|
|
If you're using the namespace method, or the class you're exporting is designed to be instanced and the function is working on an instance then no you don't need static. Static let's classes share variables, you said you know about this, but a function by default is also limited to a specific class instance just like it's variables, a function can only operate on the static functions do not need an instance and hence cannot work on non static variables because they require a specific instance. I hope that ain't too confusing? |
|
|
Remember the #ifdef PROJECT_EXPORTS? There's an #else with this meaning that the lib is only included if you're including the header instead of working on this project. Now assuming that you're not going to include the header in the first place until you've finished working on |
so my question is: is the dllexport/dllimport (and the macro) only needed for the DLL ? |
so basically when I use static I'm setting the function to work only in noninstanced member, e.g. calling Class::Function() from application |
|
|
a, if DEMO_EXPORTS is defined, it will include lib - where should I define DEMO_EXPORTS in the code? and in what code should I define it? (DLL or app code (when using DLL)) |
|
|
b, when Im using DLL in an app, it will include the lib, HOWEVER when I compile DLL (project properties > general > config type > dynamic library) it WONT make a .lib, so I need to compile both, dynamic AND a static library? |
Right, well firstly if your project is called "PathSocket" and you're using MSVS (I did mention this, for any other compiler you'll have to check there definitions) you'll have PATHSOCKET_EXPORTS automatically defined for you while ever you're working on "PathSocket.sln" (if you don't use MSVS .sln is just there project file extension) |
#pragma comment(lib, "PathSocket.lib"),
should I include .lib with my .dll everytime I want to use it, or is the .lib IN the .dll?
|
|
|
|
so basically a difference between static and dynamic lib is that with static I have to rebuild whole program, but with dynamic I just have to replace .dll, right? |
so the PATHSOCKET_EXPORTS consist of a name of solution (or project?) and a _EXPORTS suffix and is hidden somewhere in the project, right? (so I cant see it in source) |
/D |
/D "MIN(x, y) ((x < y) ? x : y)" |
DLL class PathSocket{
, whenever I've done this on my compiler the console log reads something like "__declspec(dllexport) ignored in front of class for no defined variable"};
there is no variable being declared (don't worry, you don't want one here)class DLL PathSocket{
|
|
|
|
|
|
|
|
|
|
|
|
DLL class <name>{
on my phone because I constantly do this in my own code and it takes me like half an hour to figure out why (or as soon as I remember to read the actual output log rather than the error log)