As I work on a c++ application, I realize I am making a lot of classes and functions that could be used in other projects. So I'd like to put all this code in a separate net beans project that can be "included" into other projects. (with code completion etc)
I've tried creating a new "static library" project, then I added the project to my main project (by going to preferences->link->libraries and adding my "library project"), but the code completion feature does not find the .h file of my library project when i try to #include it, the project also won't build.
What is the correct way to do this?
Creating a static library and adding it to Linker->Libraries is correct.
But another small step is needed: add directory with shared *.h files to project properties -> C Compiler (or C++ Compiler) -> Include Directories.
Also take a look at Subprojects sample: File -> New Project -> Samples -> C/C++ -> Subproject Application.
Related
I've been programming in Python for over a year now but am just learning C++ and am unfamiliar with how to go about using external libraries, CMake and github for that matter. I'm trying to use an external library called cpr - https://github.com/whoshuu/cpr. So far I've followed the instructions in the 'Usage' section of that link up to, but not including, the "add_subdirectory(cpr)" bit.
So far I've got the source code for cpr in the Visual Studio project folder of my C++ project. In the project properties I've then added into Include Directories (under VC++ Directories) "$(SolutionDir)site_libs\cpr\include" and I've added the same thing into Additional Include Directories (under C/C++ -> All Options). This means that the following code compiles just fine:
#include <cpr/cpr.h>
int main(int argc, char** argv) {
auto r =
cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
cpr::Authentication{"user", "pass"},
cpr::Parameters{{"anon", "true"}, {"key", "value"}});
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text; // JSON text string
}
However this code isn't working when it comes to building, due to link errors. I'm pretty sure the thing I'm missing is the actual .lib file for it to find where these functions etc are defined (nothing for cpr is set in the Linker -> Input property). So I'm wondering - how do I create this .lib file / is that even the right thing to do / what is this "add_subdirectory(cpr)" and where/how do I run it... basically what do I do to make this whole thing work..?! I've tried compiling cpr with CMake but it throws a load of errors about 'CMakeLists.txt' not present in certain folders.
Apologies if I've used any incorrect terminology here, only been learning C++ for a couple of days now. Any help massively appreciated!
If the library does not come as a binary distribution (that is with the .lib already built) you are going to need to build it as a separate project from the code you want to use the library, that step will build the .lib file. If a .sln is included with the distribution use that otherwise you may well have to create your own (or add it as a project to an existing solution).
Once you have a .lib add the directory under the VC++ directories of the project settings and add the actual .lib file name under Linker->Input on the Additional Dependencies line.
To build the library if the distribution does not include the needed VS files you will need to create a project at minimum (it can be part of the solution for your program), right click on the solution node in solution explorer and select Add->New Project, from there select Visual C++->Windows Desktop and either Dynamic Link Library or Static Library as you desire.
Go to the Project menu and select Project Dependencies, change your program to depend on the new project, this will set build order so your program project builds after the library.
You may need to disable the use of pre-compiled headers, right click on the new project node select Properties, go to C/C++->precompiled headers and change Precompiled Headers->Precompiled Header to Not using precompiled headers.
Next add the header and source files to the project and attempt building.
If this succeeded you will have a .lib suitable for use in the additional dependencies of your program project as already described.
I am using Eclipse CDT as IDE to develop my application which uses an external library for which I have access to the header files and libraries.
How can I setup my project properties such that I can get code completion (I have code completion for files which belong to my project, but not to the ones which are part of this external library).
I tried the following approach which unfortunately fails.
Project->Properties->C/C++ general->Path and symbols -> Includes
In Includes, I added the path to the header files.
Unfortunately, this does not seem to work.
So how can I setup my project such that, if I instanciate an object (from this external library) in my project, I can get code completion and see all available public methods?
thanks for your valuable help.
This works for me: Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> C++ -> CDT User Setting Entries -> Add
I am creating 2 projects at the moment. The first one is an executable application. The second one is a Dynamic Library. Is there a way to link the Dynamic Library to the application in Netbeans so when I run the application I can load in the Dynamic Library. I know I could just copy the built files over, but that is a pain in the ass as I need to test if it's working every minute.
Does anyone know how to do this? I'm pretty sure it's possible as it would be so useful in many cases.
Yes it's possible:
Application Project -> right click -> Properties -> Linker
Libraries -> ... -> Add Project -> select your library project (-> check Build and select the Configuration if necessary)
Add the proper include directory at C or C++ Compiler settings
Properties -> Related Projects -> ... -> at your library project there
Not sure if step #4 is required.
If you build your application project, the library project will get build too.
An alternative is to point the project that uses the shared library to the directory where netbeans places the .so it generates from the shared library project. In other words, project 1 creates a shared library, project 2 uses it. So in netbeans right click on project2, choose
properties->linker->libraries (click "...")
then click on "add library" and navigate to the folder of project 1 that is the actual netbeans project folder -- in it there will be a "dist" directory, with children, something like "/dist/Debug/linux-x86/.so" choose that .so file
note, project 1 should be created as a netbeans "C Dynamic Library" project, in which case it will automatically pre-pend "lib" in front of the project name when it generates the .so, so that the .so file's name automatically starts with "lib"..
After that, you can update and build the two projects independently, and project 2 will always see the latest build of project 1.
Sean
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).
I recently tried making app windows using C in VC++ 2010.
I changed some additional dependency settings for my project. But after doing this, every time I create a new C++ project VC++ gives me what is to be believed C external dependency lib files despite giving cCand C++ extern dependency libs.
What have I done and how do I fix it? As its starting to do my head in to be honest.
The "External Dependencies" folder in your project contains a list of headers that the C++ IntelliSense engine has determined are used by your project.
The "External Dependencies" folder in no way impacts how your project is built (you can even disable the folder, under Tools -> Options -> Text Editor -> C/C++ -> Advanced -> "Disable External Dependencies Folder"). It is there for informational purposes.
If you include a C++ header (<vector>, for example), then right click your project and select "Rescan Solution," you should see the header file <vector> and all of its dependencies appear in the list. (You probably don't have to use "Rescan Solution," but I'm not 100% sure on what events cause the C++ IntelliSense engine to pick up new header files.)