Sascha Tayefeh's Homepage

Blogging about Information Technology

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

with 2 comments

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

Written by Sascha Tayefeh

July 6th, 2009 at 2:49 am

Posted in Coding,HowTo

Tagged with , , , , ,

2 Responses to 'Creating and Using a C++ Shared Library with Eclipse CDT Galileo and GNU C++ Compiler and Linker'

Subscribe to comments with RSS or TrackBack to 'Creating and Using a C++ Shared Library with Eclipse CDT Galileo and GNU C++ Compiler and Linker'.

  1. 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?

    Alex

    23 Feb 10 at 09:15

  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

    hannibal

    26 Aug 10 at 15:34

Leave a Reply

Switch to our mobile site