coding, howto

Creating and Using a C++ Shared Library with Eclipse CDT Galileo and GNU C++ Compiler and Linker

This is meant to be a walkthrough rather than a tutorial, thus, this is no good for absolute C++ / Eclipse beginners (you may get lost too soon).

Step I: Create the Shared Library

  1. File -> New -> Project
  2. C/C++ -> C++ Project
  3. Shared Library -> Empty Project (remember to give it a name. Here I use "testlib")
  4. Create a class for Testing: File -> New -> Class. Name it “TestClass“. Also create a simple public method: prototype
    void testWrite(void);
    within TestClass.h and implement something like
    void TestClass::testWrite(void) { std::cout << "From Shared Lib" << std::endl; }
    within TestClass.cpp. Remember to include iostream somewhere
  5. Only if you would like to create a 64bit build: Advanced Settings -> GCC C++ Compiler -> Miscellaneous -> All Options: Add -fPIC
  6. Ctrl+B for build. There should be no errors

Step II: Create some executable that uses the dynamic library

  1. File -> New -> Project
  2. C/C++ -> C++ Project
  3. Executable -> “Hello World C++ Project”
  4. Name it “UseDLL”
  5. Next -> Next -> “Advanced Settings”
  6. GCC C++ Compiler -> Directories ->Add (Button to the right top)
  7. Workspace -> testlib (or enter: ${workspace_loc:/testlib})
  8. Should look like this: Eclipse CDT Library 001
  9. GCC C++ Linker -> Libraries
  10. Add Library (-L): ${workspace_loc:/testlib/Debug}
  11. Add Library search PATH (-l): ${workspace_loc:/testlib/Debug}
  12. Should look like this:
    Eclipse CDT Library 002
  13. Finally select “Paths and Symbols” from the left -> References and select “testlib”:
    Eclipse CDT Library 003
  14. However, this counts for “debug”. Repeat step 10 to 13 for “release” choosing “release” from the upmost tab and replacing “debug” by “release”
  15. OK -> Finish
  16. Ctrl+B should build, however, let’s include the library and do something:
  17. From the Project Explorer, DoubleClick on UseDLL -> src -> UseCPP.cpp and include "TestClass.h"
  18. Also add two lines within main() that create the TestClass object and call its testWrite() method:
    TestClass ti;
  19. ts.testWrite();

Step III: Run (debug) the executable from within Eclipse

  1. First, you must set the environmental variable LD_LIBRARY_PATH: From the Project Explorer Tab, choose UseDLL -> Right-Click -> Debug As -> Debug Configurations
  2. Environment -> New
    Name = LD_LIBRARY_PATH
    Value = ${workspace_loc:/testlib/Debug}
  3. Apply -> Close
  4. Press “F11” key for Debug -> Select “Use configuration specific setting” -> “Standard Create Process Launcher” -> OK
  5. Now the debug view should appear
  6. Set a break point (Ctrl+Shift+B) at the UseDLL.cpp line that contains “ts.testWrite();“:
    Eclipse CDT Library Debugging Breakpoint 004
  7. Press “F8” to “Resume”
  8. When the above mentioned line is reached, press “F5” to “Step Into” the method
  9. Voila! You’re within the code of you dll:
    Eclipse CDT Library Debugging - Jump into a method 005
Advertisements
Standard

7 thoughts on “Creating and Using a C++ Shared Library with Eclipse CDT Galileo and GNU C++ Compiler and Linker

  1. Alex says:

    Thank you very much for this article.
    One question: LD_LIBRARY_PATH setting seems to be applied to both Debug and Release configurations, and Debug library version is always executed. Can this be solved?

  2. Thank for your post,
    It is ok in LINUX, but not work in MAC OS X(x86_64).
    I build to be successful but when run, having the error:
    “dyld : not loaded …”
    Can you help me ?
    Thanks

  3. Dragan says:

    Thanks for the walkthrough, however, I have one big doubt.
    When I use
    #include “TestClass.h”
    I can’t build UseDLL project.

    Here are the errors:
    undefined reference to `TestClass::TestClass()’
    undefined reference to `TestClass::testWrite()’
    undefined reference to `TestClass::~TestClass()’
    undefined reference to `TestClass::~TestClass()’

    However if i use
    #include “TestClass.cpp”
    instead of
    #include “TestClass.h” , my project builds successfully.

    Of course prototypes of functions of the shared object are stated in TestClass.h, and their definition ( constructor, destructor and testWrite() ) in TestClass.cpp.

    Do you have any clue why is this happening?

    Thanks in advance.

  4. I was running in to a few issues mainly around using the correct -L and -l options . Which was compounded by Eclipse’s many GUI’s and project types.

    Finally this post helped me . Thanks

  5. I use this on my openCV project

    g++ -fPIC -shared Sample1.cpp -o libSample1.so `pkg-config –libs opencv` `pkg-config –cflags opencv` -I/usr/lib/jvm/java-8-oracle/include -I/usr/lib/jvm/java-8-oracle/include/linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s