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

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).*

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

Related

VS2019 linux cannot find additional dependencies

I am doing a remote C++ build from VS2019 to a machine with Centos. When I input the library to the option additional dependencies, the vs2019 always shows g++ : error : libTest.a: No such file or directory , the file libTest.a is a library I wrote to test my solution.
You should properly configure you VSLinux environment.
First, add additional include directory on Properties -> C/C++ -> General -> Additional include directory. Add paths of directories for additional header files for your library.
Second, add additional library directory on Properties -> Linker -> General -> Additional library directory. Add directories where your static/shared libraries are.
Third, put your name of libraries on Properties -> Linker -> Input -> Library dependency. Add the name of your library. Remember, you should remove prefix lib and the extension of the library.
The exact name of the option may differ since I'm not using VS2019 in English.

How do I add a library to my NetBeans C++ project?

I want to use the boost library in order to use some of its functions but I don't know how to do so and cannot seem to find an answer.
I'm currently using NetBeans 8.0.1 ,any help is appreciated.
You have to add two things:
(a) Library header
Go to …
Project Properties -> C++ Compiler -> Include Directories
add the header's directory path there.
(b) Library binaries
Go to …
Project Properties -> Linker -> Additional Libraries Directory
and add the directory path- that's where your library binaries are.
Next add your library at:
Libraries.

How to use my library in C++?

In Eclipse I have created two libraries. One of them is shared another one is static. I have compiled them in Eclipse and as a result a Debug folder was created (for both libraries) and these folders contain make-files as well as object files (*.o) and dependency reference file (*.d). In addition to that, the static library contains an *.a file.
Now I create a new project and what to use these library in this project. Normally, when I use a library I type #include <libraryname>. But if I use #include <mylibraryname> it does not work (I get unresolved inclusion). And this is not surprising because Eclipse should somehow know where my library is located. So, my question is how can I inform Eclipse about the locations of my libraries.
ADDED As recommended I do the following sequence "Project -> Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries". Then, in the "libraries(-l)" filed I add "StaticList" (because I have "libStaticList.a" file) and in the "Library search path (-L)" filed I give the full name of the directory where my "libStaticList.a" is located. Then I click Apply and OK. But it does not help. Eclipse does not like #include <StaticList>. It complains: "Unresolved Inclusion..".
#includeing headers only makes the compiler aware that the functions in those headers exist. The actual implementation of those functions needs to be linked in by the linker. That's where the library (.a) files that you built come in. Check out this thread for an example on how to link in your libraries using Eclipse.
I think you need to #include "yourlibrary.cpp" (between double quotes) instead of < >.

How can I include Lua library in VS 2008

I'm not sure about this location.
In Properties > Linker > General > Additional Dependencies
I have to include some library in Additional Library Directories, haven't I?
or I have to install some program in my computer?
The problem is I can't to use luaL_dofile, but I can use it before my computer was formatted.
That's why I'm not sure about library. I compile and run my code after my
computer was formatted suddenly. I'm pretty sure that my code has no problems
because it can run before my computer was formatted.
Well, then, obtain the library from where you get it. Place it in some place that you know it will not be moved (or install it if an installer is provided), and do this:
Select menu item Project -> Properties -> Configuration Properties -> Linker ->Input.
Now add the name of the library in the "Additional Dependencies" edit control.
You need to do two things:
Add the library directory to Libraries directory
Add the library name (without path, but with extension) to Linker->Input->Additional Dependencies

undefined reference to 'readline'

I'm trying to use the readline library in my c++ project using netbeans, so I've installed libreadline6-dev and add it to my project Properties -> Linker -> Additional Library directories -> and add (usr/include/readline).
But while compiling I get this error : undefined reference to 'readline'.
I found that I need to add also -lreadline to somewhere, but I have no idea where to put it.
Any ideas.
Thanks
You are on the good way, the problem you got is about the linker.
You have to specify the library(readline) here:
Project properties -> Build -> Linker -> Libraries.
and you also need to specify the path of the library:
Properties -> Linker -> Additional Library directories
But, like nos said, it is not the include directory.The include directory is the headers of the library(.h).
The linker needs a file with the extension .a on UNIX, it should be under a lib directory (ex: /usr/lib depends on where the library is installed).
If it is not working be sure that the file [library_directory]/libreadline.a exists
You also need to link ncurses with your project because readline uses it.