Stefan Falk
Stefan Falk

Reputation: 25387

Eclipse C/C++: Using static/shared library project in executable project

I think the title almost hits the point.. What I am trying to do is write a server. Here is the thing:

I want to separate separateable parts of the server into different projets. For example I wanted to create a Project "ServerNetworkStuff" and "ServerGameLogicStuff" into two projects which are static or shared libraries..

Now I want to create another Project "Server" that uses these two Projects as library.

Eclipse Projects:
- ServerNetworkStuff (static library)
- ServerGameLogicStuff (shared library)
- Server (using ServerNetworkStuff, ServerGameLogicStuff)

Is that even possible? Or is there any equivalent solution which doesn't force me to reinvent the wheel?

Thank you for your help!

EDIT:

If I add a reference to the active mode under "Project > Properties > C/C++ General > Paths and Symbols > References" it doesn't work. The compiler can't find the header files.. if I add the path to the header files I get "undefined reference" errors.

PARTLY SOLUTION:

*Okay it compiles now... but execution doesn't work at the moment..

What I did was first creating my projects "Server" (executable) and ServerNetwork (shared lib). After adding a ServerNetwork reference to Server there were a few things to do left.

I had to change my includes from

#include <include/ServerThread.hpp>

to

#include "ServerThread.hpp"

without meaning any shared libraries I am using in the project. Just changed it for the references of my own classes.

In my project Server that wants to use ServerNetwork I needed to add -lServerNetwork and -fPIC as parameter for g++.

And additionally the folder which the .so-file contains must be added to the library path (which Eclipse should do automatically if you add the specific project as reference).*

Upvotes: 1

Views: 3899

Answers (1)

Enoah Netzach
Enoah Netzach

Reputation: 777

The Reference in Eclipse only works with open projects. I found the fastest way is to add or symlink the headers needed in the system path (/usr/local/include or similar) or just add the path to it, and doing the same with the library.

If you don't want to do that (which should be the best option), you still can add includes and libraries:

  • the -I path in Project Properties -> C/C++ General -> Path and Symbols -> Includes,

  • the -L libs' paths in Project Properties -> C/C++ General -> Path and Symbols -> Library Paths,

  • the -l libs in Project Properties -> C/C++ General -> Path and Symbols -> Libraries.

Or by hand: the -I path in Project Properties -> C/C++ Build -> Settings -> [CGG/C/C++] Compiler -> Includes, and the -L -l libs in Project Properties -> C/C++ Build -> Settings -> C++ Linker -> Libraries.

Inclusion

Now you can #include either with <> or "" syntax (is a good practice to reserve the former to system libraries).

Iussues

You should have execution problems if you don't move/copy/symlink the libraries in default paths, for example with OSX, the solution is to export the non-default path just before execution (e.g. export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:/non-default/lib/path for OSX).

Upvotes: 1

Related Questions