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
- File -> New -> Project
- C/C++ -> C++ Project
- Shared Library -> Empty Project (remember to give it a name. Here I use
"testlib") - Create a class for Testing: File -> New -> Class. Name it “
TestClass“. Also create a simple public method: prototype
void testWrite(void);
withinTestClass.hand implement something like
voidTestClass::testWrite(void) { std::cout << "From Shared Lib" << std::endl; }
withinTestClass.cpp. Remember to includeiostreamsomewhere - Only if you would like to create a 64bit build: Advanced Settings -> GCC C++ Compiler -> Miscellaneous -> All Options: Add
-fPIC - Ctrl+B for build. There should be no errors
Step II: Create some executable that uses the dynamic library
- File -> New -> Project
- C/C++ -> C++ Project
- Executable -> “Hello World C++ Project”
- Name it “UseDLL”
- Next -> Next -> “Advanced Settings”
- GCC C++ Compiler -> Directories ->Add (Button to the right top)
- Workspace -> testlib (or enter:
${workspace_loc:/testlib}) - Should look like this:

- GCC C++ Linker -> Libraries
- Add Library (-L):
${workspace_loc:/testlib/Debug} - Add Library search PATH (-l):
${workspace_loc:/testlib/Debug} - Should look like this:

- Finally select “Paths and Symbols” from the left -> References and select “testlib”:

- However, this counts for “debug”. Repeat step 10 to 13 for “release” choosing “release” from the upmost tab and replacing “debug” by “release”
- OK -> Finish
- Ctrl+B should build, however, let’s include the library and do something:
- From the Project Explorer, DoubleClick on UseDLL -> src -> UseCPP.cpp and
include "TestClass.h" - Also add two lines within main() that create the TestClass object and call its testWrite() method:
TestClass ti; - ts.testWrite();
Step III: Run (debug) the executable from within Eclipse
- First, you must set the environmental variable LD_LIBRARY_PATH: From the Project Explorer Tab, choose UseDLL -> Right-Click -> Debug As -> Debug Configurations
- Environment -> New
Name = LD_LIBRARY_PATH
Value =${workspace_loc:/testlib/Debug} - Apply -> Close
- Press “F11″ key for Debug -> Select “Use configuration specific setting” -> “Standard Create Process Launcher” -> OK
- Now the debug view should appear
- Set a break point (Ctrl+Shift+B) at the UseDLL.cpp line that contains “
ts.testWrite();“:

- Press “F8″ to “Resume”
- When the above mentioned line is reached, press “F5″ to “Step Into” the method
- Voila! You’re within the code of you dll:

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
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