Note that there are some explanatory texts on larger screens.

plurals
  1. POUndefined reference problem when dynamic library is used
    primarykey
    data
    text
    <p>I was reading about static and dynamic libraries. To explore more I created three files 2 <code>.cpp</code> files and 1 <code>.h</code> file</p> <p><code>demo.h</code></p> <pre><code> class demo { int a; public: demo(); demo(const demo&amp;); demo&amp; operator=(const demo&amp;); ~demo(); }; </code></pre> <p><code>demo.cpp</code></p> <pre><code>#include "demo.h" #include &lt;iostream&gt; demo::demo():a() { std::cout&lt;&lt;"\nInside default constructor\n"; } demo::demo(const demo&amp;k) { this-&gt;a=k.a; std::cout&lt;&lt;"\nInside copy constructor\n"; } demo&amp; demo::operator=(const demo&amp;k) { this-&gt;a=k.a; std::cout&lt;&lt;"\nInside copy assignment operator\n"; return *this; } demo::~demo() { std::cout&lt;&lt;"\nInside destructor\n"; } </code></pre> <p><code>main.cpp</code></p> <pre><code>#include "demo.h" int main() { demo d; demo d1=d; demo d2; d2=d; } </code></pre> <p>Now I created two object files :<code>demo.o</code> and <code>main.o</code> using <code>g++ -c demo.cpp</code> and <code>g++ -c main.cpp</code> and then created a static library using <code>ar cr demo.a demo.o main.o</code></p> <p>I also created a dynamic library using <code>g++ -shared demo.o main.o -o demo.dll</code></p> <p>Now when I use my static library(<code>g++ demo.a -o demo</code>) to create an executable everything goes fine. But when I use my dynamic library to create an executable I get an error <code>Undefined reference to main</code> I have used the following command to create an executable <code>g++ demo.dll -o demo</code>.</p> <p>When I use <code>g++ main.cpp -o demo demo.dll</code> everything goes fine, why?</p> <p>Where am I wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload