The problem about the class exported from static link library

Dec 26, 2016 at 2:42am
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?
Dec 26, 2016 at 8:50pm
> use the above (1) method
but the code you've posted is for the (2) method.

we need to see your actual code, the building command used and the error messages.
Dec 27, 2016 at 7:35am
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.
Dec 27, 2016 at 2:29pm
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.
Dec 29, 2016 at 4:16pm
Thanks ne555
Topic archived. No new replies allowed.