I was reading about static and dynamic libraries. To explore more I created three files 2 .cpp
files and 1 .h
file
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
#include "demo.h"
int main()
{
demo d;
demo d1=d;
demo d2;
d2=d;
}
Now I created two object files :demo.o
and main.o
using g++ -c demo.cpp
and g++ -c main.cpp
and then created a static library using ar cr demo.a demo.o main.o
I also created a dynamic library using g++ -shared demo.o main.o -o demo.dll
Now when I use my static library(g++ demo.a -o demo
) to create an executable everything goes fine. But when I use my dynamic library to create an executable I get an error Undefined reference to main
I have 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 fine, why?
Where am I wrong?