<p>I'm trying to write a small class library for a C++ course.</p>

<p>I was wondering if it was possible to define a set of classes in my shared object and then using them directly in my main program that demos the library. Are there any tricks involved? I remember reading this long ago (before I started really programming) that C++ classes only worked with MFC .dlls and not plain ones, but that's just the windows side.</p>

[{'answer_id': 58061, 'author': 'Kristopher Johnson', 'author_id': 1175, 'author_profile': 'https://Stackoverflow.com/users/1175', 'pm_score': 5, 'selected': True, 'text': '<p>C++ classes work fine in .so shared libraries (they also work in non-MFC DLLs on Windows, but that\'s not really your question).  It\'s actually easier than Windows, because you don\'t have to explicitly export any symbols from the libraries.</p>\n\n<p>This document will answer most of your questions: <a href="http://people.redhat.com/drepper/dsohowto.pdf" rel="nofollow noreferrer">http://people.redhat.com/drepper/dsohowto.pdf</a></p>\n\n<p>The main things to remember are to use the <code>-fPIC</code> option when compiling, and the <code>-shared</code> option when linking.  You can find plenty of examples on the net.</p>\n'}, {'answer_id': 58099, 'author': 'Adam Pierce', 'author_id': 5324, 'author_profile': 'https://Stackoverflow.com/users/5324', 'pm_score': 2, 'selected': False, 'text': '<p>As I understand it, this is fine so long as you are linking .so files which were all compiled using the same compiler. Different compilers mangle the symbols in different ways and will fail to link.</p>\n\n<p>That is one of the advantages in using COM on Windows, it defines a standard for putting OOP objects in DLLs. I can compile a DLL using GNU g++ and link it to an EXE compiled with MSVC - or even VB!</p>\n'}, {'answer_id': 58171, 'author': 'Flame', 'author_id': 5387, 'author_profile': 'https://Stackoverflow.com/users/5387', 'pm_score': 3, 'selected': False, 'text': '<h1>My solution/testing</h1>\n\n<p>Here\'s my solution and it does what i expected.</p>\n\n<h2>Code</h2>\n\n<p><em>cat.hh</em> : </p>\n\n<pre><code>#include &lt;string&gt;\n\nclass Cat\n{\n    std::string _name;\npublic:\n    Cat(const std::string &amp; name);\n    void speak();\n};\n</code></pre>\n\n<p><em>cat.cpp</em> :</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n\n#include "cat.hh"\n\nusing namespace std;\n\nCat::Cat(const string &amp; name):_name(name){}\nvoid Cat::speak()\n{\n    cout &lt;&lt; "Meow! I\'m " &lt;&lt; _name &lt;&lt; endl;\n}\n</code></pre>\n\n<p><em>main.cpp</em> :</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include "cat.hh"\n\nusing std::cout;using std::endl;using std::string;\nint main()\n{\n    string name = "Felix";\n    cout&lt;&lt; "Meet my cat, " &lt;&lt; name &lt;&lt; "!" &lt;&lt;endl;\n    Cat kitty(name);\n    kitty.speak();\n    return 0;\n}\n</code></pre>\n\n<h2>Compilation</h2>\n\n<p>You compile the shared lib first:</p>\n\n<pre><code>$ g++ -Wall -g -fPIC -c cat.cpp\n$ g++ -shared -Wl,-soname,libcat.so.1 -o libcat.so.1 cat.o\n</code></pre>\n\n<p>Then compile the main executable or C++ program using the classes in the libraries:</p>\n\n<pre><code>$ g++ -Wall -g -c main.cpp\n$ g++ -Wall -Wl,-rpath,. -o main main.o libcat.so.1 # -rpath linker option prevents the need to use LD_LIBRARY_PATH when testing\n$ ./main\nMeet my cat, Felix!\nMeow! I\'m Felix\n$\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58058', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5387/']