I have prepared a very simple static link library project(projectlib.lib),only include lib.h and lib.cpp,i want to expoor a class named c_lib1,there are 2 ways I tried:
(1) Declare class c_lib1 in lib.h and define c_lib1 in lib.cpp.
(2) Define class c_lib1 in lib.h directly:
class c_lib1
{
public:
int m_a, m_b;
int m_add(int a, int b)
{
return a + b;
};
};
Then generate projectlib.lib. Next in an application project (.exe), I use this static library projectlib.lib,in the application project, use the above (1) method, can not identify class c_lib1, but the use of (2) method can compile through, this is why ah?
Using method (1), in lib.h:
int add(int a, int b);
class c_lib1;
in lib.cpp:
int add(int a, int b) { return a + b; }
class c_lib1
{
public:
int m_a, m_b;
int m_add(int a, int b)
{
return a + b;
};
};
I build the solution and get the projectlib.lib.
In an application project (.exe), I use this static library projectlib.lib,
#include "lib.h"
int main(int argc, char **argv)
{
c_lib1 m_clib1;
int aaa;
aaa = m_clib1.m_add(1, 2);
int ddd = add(1, 4);
return 1;
}
Severity Code Description Project File Line Suppression State
Error C2079 'm_clib1' uses undefined class 'c_lib1'
But when I use method (2), it's all OK.
class c_lib1; is just a forward declaration. Classes must be defined before they can be used.
the member functions definitions may be in a separate source, however.