Eclipse CDT and shared libraries

I had some trouble getting Eclipse CDT (C/C++ Development Toolkit) to work across separate projects. My setup is Eclipse 3.3.1 with CDT 4.0.1 on Windows with the MinGW toolchain. I have two separate projects in Eclipse: One is a shared library and the other is an executable. The executable uses the shared library so naturally it needs to read the header files from the other project in order to sucessfully compile and link. It would also be nice if it was possible to use the Run As Local C/C++ Application feature without the need to copy the shared library file to the directory where the executable file is. I set out on a quest to find the answers.

The first thing you have to do is right-click on the executable project in the Project Explorer, and then go to Properties -> Project References. There you have to select the shared or static library project you want to link to. Then click OK. If you try to build the project it still won’t work because it cannot find the required files.

Let’s open the project properties window again, but this time go to C/C++ General -> Paths and symbols -> References. Make sure that you select the shared/static library under all your configurations. The standard setup contains the Debug and Release configurations which you can switch between with the dropdown list at the top of the window. To make sure that you always link against the Release version of the library when you build the Release version of your executable you can expand the library reference node and select Release instead of the default [Active] selection. The same thing goes for building against the Debug version of the library when you’re building the Debug version of the executable. Click Apply when you’re done.

You might think that everything should be good to go by now, but we have a bit more to do first. In the project properties window go to C/C++ Build -> Settings -> Tool settings -> MinGW C++ Linker -> Libraries. Click the document with the green plus sign (Add button) right next to where it says Libraries (-l). In the input box that appears you should type the name of the library without its file extension. If your library file is “useful.dll” or “useful.so” then you should only type “useful”. Don’t forget that you have to do this for both the Release and Debug configurations if you don’t want any nasty surprises later on. Click OK when you’re done.

At least now it should compile, but you would maybe like to run it from within Eclipse as well? First you have to create a so called run configuration for your executable. Right-click on the executable in the Project Explorer and go to Run As… -> Local C/C++ Application. This will actually launch the program, but it will also automatically create a run configuration for it. The program will terminate without any error messages because it couldn’t find the library file. On the top menu go to Run -> Open Run Dialog…, select the run configuration for your program (if it isn’t already selected, obviously) and go to the Environment tab. Click New…, name the new environment variable “Path” and give it the value “${env_var:Path};${workspace_loc:/useful/Debug}”. Substitute “useful” for the name of your shared library. This will modify the system environment “Path” variable by adding the full path to the library so that the executable will find it.

If you want to use the Release version of the library then just substitute “Debug” from the environment value with “Release”. Unfortunately I haven’t found a way to detect which build configuration is active so you have to manually change this value if you switch between Debug and Release. An alternative is to create a separate run configuration for Debug and Release. This can easily be done by right-clicking on the run configuration and clicking Duplicate.

NOTE: This post has been imported from my old it’s learning ePortfolio blog.

Advertisements

2 Responses to “Eclipse CDT and shared libraries”

  1. rodrigomgsys Says:

    The last part didn’t work on Linux:

    … “Path” and give it the value “${env_var:Path};${workspace_loc:/useful/Debug}” …

    I changed as follow and worked perfect:
    “LD_LIBRARY_PATH” and give it the value “${workspace_loc:/useful/Debug}”

    It’s the best tutorial I found about configure multiple projects.
    Thx a lot

  2. masaya (@masaya_ICT) Says:

    This post saved my time :)


Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: