Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following shows an example of a shared class library shared.[h,cpp] and a main.cpp module using the library. It's a very simple example and the makefile could be made much better. But it works and may help you:</p> <p>shared.h defines the class:</p> <pre><code>class myclass { int myx; public: myclass() { myx=0; } void setx(int newx); int getx(); }; </code></pre> <p>shared.cpp defines the getx/setx functions:</p> <pre><code>#include "shared.h" void myclass::setx(int newx) { myx = newx; } int myclass::getx() { return myx; } </code></pre> <p>main.cpp uses the class,</p> <pre><code>#include &lt;iostream&gt; #include "shared.h" using namespace std; int main(int argc, char *argv[]) { myclass m; cout &lt;&lt; m.getx() &lt;&lt; endl; m.setx(10); cout &lt;&lt; m.getx() &lt;&lt; endl; } </code></pre> <p>and the makefile that generates libshared.so and links main with the shared library:</p> <pre><code>main: libshared.so main.o $(CXX) -o main main.o -L. -lshared libshared.so: shared.cpp $(CXX) -fPIC -c shared.cpp -o shared.o $(CXX) -shared -Wl,-soname,libshared.so -o libshared.so shared.o clean: $rm *.o *.so </code></pre> <p>To actual run 'main' and link with libshared.so you will probably need to specify the load path (or put it in /usr/local/lib or similar).</p> <p>The following specifies the current directory as the search path for libraries and runs main (bash syntax):</p> <pre><code>export LD_LIBRARY_PATH=. ./main </code></pre> <p>To see that the program is linked with libshared.so you can try ldd:</p> <pre><code>LD_LIBRARY_PATH=. ldd main </code></pre> <p>Prints on my machine:</p> <pre><code> ~/prj/test/shared$ LD_LIBRARY_PATH=. ldd main linux-gate.so.1 =&gt; (0xb7f88000) libshared.so =&gt; ./libshared.so (0xb7f85000) libstdc++.so.6 =&gt; /usr/lib/libstdc++.so.6 (0xb7e74000) libm.so.6 =&gt; /lib/libm.so.6 (0xb7e4e000) libgcc_s.so.1 =&gt; /usr/lib/libgcc_s.so.1 (0xb7e41000) libc.so.6 =&gt; /lib/libc.so.6 (0xb7cfa000) /lib/ld-linux.so.2 (0xb7f89000) </code></pre>
 

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