I read about static and dynamic libraries. To learn more, I created three files: 2 .cppand 1 .hfile
demo.h
class demo
{
int a;
public:
demo();
demo(const demo&);
demo& operator=(const demo&);
~demo();
};
demo.cpp
#include "demo.h"
#include <iostream>
demo::demo():a()
{
std::cout<<"\nInside default constructor\n";
}
demo::demo(const demo&k)
{
this->a=k.a;
std::cout<<"\nInside copy constructor\n";
}
demo& demo::operator=(const demo&k)
{
this->a=k.a;
std::cout<<"\nInside copy assignment operator\n";
return *this;
}
demo::~demo()
{
std::cout<<"\nInside destructor\n";
}
main.cpp
int main()
{
demo d;
demo d1=d;
demo d2;
d2=d;
}
Now I created two object files: demo.oand main.ousing g++ -c demo.cppand g++ -c main.cpp, and then created a static library usingar cr demo.a demo.o main.o
I also created a dynamic library using g++ -shared demo.o main.o -o demo.dll
Now that I use my static library ( g++ demo.a -o demo) to create an executable, everything is going well. But when I use my dynamic library to create an executable, I get an error message. Undefined reference to main
I used the following command to create an executable g++ demo.dll -o demo.
When I use g++ main.cpp -o demo demo.dll, everything goes well, why?
?