Archive for the ‘Eclipse’ tag
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:
