How to use my library in C++? - 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 < >.

Related

How can I install and use C++ libraries on eclipse?

I want to use libosmium library. Could someone please tell me how do I set up this library after I download it?
All I need to know is the standard way of installing external libraries. I can't really find very clear instructions online.
I'm coding with C++ using the eclipse IDE version 4.18.0.
For includes: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Compiler >> Includes
you must add the include path for the external library (where the header reside). This information is needed by the Eclipse indexer (code completion etc.) and the compiler
For libraries: right click on your project and go to Properties>>C/C++ Build>>Settings>>GCC C++ Linker >> Libraries you must add the library search path (option -L) and the library you want to link against (option -l). This info is needed for the linker.
Source: http://wiki.eclipse.org/CDT/User/FAQ#How_do_I_add_an_external_library_to_my_C.2B.2B_project.3F
Eclipse-CDT Setting Pictures
Include path settings:
Library & library search path settings
You can also use pkg-config plug in
https://marketplace.eclipse.org/content/pkg-config-support-eclipse-cdt
Here is a link to a similar question with answers: Problems importing libraries to my c++ project, how to fix this?
As specified in the other answer, you can usually add a library by looking at the project properties:
Properties>>C/C++ Build>>Settings>>GCC C++ Compiler>>Includes
However if you're using a makefile project, the Makefile itself must know about the location of libraries. In this case, in order to avoid an "Unresolved inclusion" warning in the header, you may also want to tell eclipse where the header files are. This way the autocomplete & "Open Declaration" will work. This is found in:
Properties>>C++ General>>Preprocessor Includes Paths, Macros etc.
In my case I couldn't figure out how to add a custom configuration and updated the default configuration. I added a path for the Android NDK under the GNU C++ -> CTD User Setting Entries

How to use an external library for C++ in VS2017

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.

How to add static libraries to a Visual studio project

I am trying to add static libraries to my project. To add the static library I am following Microsoft's instructions: http://msdn.microsoft.com/en-us/library/ms235627.aspx.
My problem is I that am not able to see the dependent library while adding the reference to my project.
In the tutorial mentioned above, they have mentioned that the dependency (ie. static library), should be added to the solution.
The tutorial you have provided refers to a case in which you create your own static library - in this case you may want to add it to your solution and thus make it an integral part of the solution; but I assume you are trying to add external libraries (not created by you, e.g. downloaded from the net) - that is why you got stuck.
On Property Pages, go to C/C++->General->Additional Include Directories and provide the path, where the header file of the library that you want to use is located.
Then go to Linker->General->Additional Library Directories and specify the path, where your .lib file is located.
Finally, go to Linker->Input->Additional Dependencies and add the name of the file containing your library together with its extension (e.g. example_library.lib).
That is all. Now you should be able to use the library. Remember to #include it in your files and use the right mode (release/debug) and the right version for your platform (x64/win32). You may have to repeat the steps given above both for release and debug versions of your app.
I am just extending the answer given by KjMag. It's a great answer, except that it misses the part where we tell the linker which external libraries to add.
In Visual Studio, go to Property Pages >> Linker >> Input >> Additional Dependencies. Here we can add the required libraries.

static library linkage issue using eclipse

I have two projects, one that creates static library and the other one that is using it.
The first one, create a file called liboutputdevice.a.
When I build it, everything goes OK.
Then I have my second project that uses the library above,
and including #include "outputdevice.h"
and calles to a function:
initdevice("sdfs");
Which is declared on outputdevice.h and ipmlemented in the static library above.
The tester project linked to the first project by going to: Cross g++ Linker, libraries and library search path.
When I'm building that project (The tester, second one), the first one is getting an error, with Undefined reference to 'pthread create'
Suddenly...
(although I included the -lpthread in the compile process of the first project).
Someone can tell what it the problem?
You have to tell your compiler/IDE where the include files are. Notice, that library files (usually with a .lib extension) and include files (.h or .hpp) are not the same. While libraries have to be added to the linker (as you did correctly), the include files must be accessible from the project base directory as well.
If the main path of the includes is not the same as of the project, you have to tell the compiler, were it should look for additional includes.
Usually this is done by the -I option of the compiler call, e.g.
g++ main.cpp -I path/to/other/includes/dir
You can also set it up directly in the project preferences of your IDE.

How to compile a static library? ("-static-lib..." equivalent?)

I am using VS2010 and
I have a C++ project that is referencing and using an external C library (dll) by having various entries in the VC++ Directories and Linker sections of the project properties.
Right now my project is building but when it starts, a message box appears :
The program can't start because ExternalCLibrary.dll is missing from your computer. [...]
I would like to know how to do in Visual Studio 2010 the the equivalent of
adding "-static-libgcc -static-libstdc++" to your compiler flags.
It seems to be the solution according to:
The program can't start because libgcc_s_dw2-1.dll is missing
Load your project in Visual Studio.
Right click your project and
choose Properties.
Locate the "Linker" portion of the tree on the
left.
Choose All Configurations and All Platforms from the drop down
menus at the top of the dialog.
Put your additional static library
dependencies in the Input -> Additional Dependencies field, semicolon delimited.
If the libs are not on your lib search path, make appropriate
entries in the General -> Additional Library Directories field, semicolon
delimited.
Apply, save, compile, run.
You can't use dll as a static library ( that's why they are called Dynamic-link library ). In order to compile a static library, you'll the source code of that library. Once you have the source code, go to Project settings, General->Configuration Type and set it to Static Library(.lib). Then in your program, you'll need to add that library by putting the library name in Linker->Input->Additional Dependencies
The two flags passed to gcc as per your question tell gcc to link the runtime library statically to an executable or shared library/dll. This is unlikely to be the problem with your issue as the part of the error message you quoted suggests that ExternalCLibrary.dll isn't being built properly.
If the DLL exists, use a tool like dependency walker to determine which dependency of your DLL can't be loaded; that's the likely culprit.
If ExternalCLibrary.dll doesn't exist then you need to find out where you are supposed to get it from, but if your project builds and it's listed in the project as a dependency then my guess is that it's an issue with the loader not being able to find a dependency of this DLL at runtime.
This process is simple, however you need to be aware of several things. The first thing, if your lib is written in C, in the header files of every source containing C functions us the following.
#ifdef __cplusplus
extern "C" {
#endif
// C functions
#ifdef __cplusplus
}
#endif
Once you've done that, compile the library into a static library using the ar rcs [YOUR OBJECT FILES]. The final thing to do is us the c++ compiler link the library with the object files from your project. Now flags are required to link the library.