static library linkage issue using eclipse - c++

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.

Related

Adding external library (Gurobi) to Eclipse C++

I would like to use Gurobi in a C++ project I have in Eclipse. I tried multiple manuals/tutorials (including THIS on how to do the same in Visual Studio) to add the hook up the Gurobi files with Eclipse/GCC, but I just couldn't make it work. I feel like I don't understand enough how these things should work in the first place.
In my Gurobi folder I have 3 folders, that in my opinion are important: include (with .h files), lib (with .lib files and NO .so or .a files) and src (with .cpp & .h files).
include: I added the path to this folder to C/C++ Build -> Settings -> GCC C++ Compiler -> Includes -> Include paths, I guess this is for the compiler to see the .h files. I also added it to C/C++ General -> Paths and Symbols -> Includes -> Include directories, I guess this is for the linker to see the .h files.
lib: I first added the folder path to C/C++ Build -> Settings -> MinGW C++ Linker -> Libraries -> Libraries (-l), however after searching online it seems to me this option only works for libraries with standard names and extensions. It also gave me the error ld.exe: cannot find -lc:/gurobi900/win64/lib/, so I deleted it and added the two .lib file paths separately to Miscellanous -> Other objects.
I got to the point where #include "gurobi_c++.h" works, however, already Eclipse underlines a line where I am defining a GRBEnv() object, that is declared in include/gurobi_c++.h, but defined in src/cpp/Env.cpp. Also if I try to compile, I get undefined reference errors. This means neither the Linker nor the Compiler can see the definition of GRBEnv().
Is there a way to tell the linker/compiler where the .cpp files are? Is it happening automatically, based on the path of the include folder? Am I supposed to link the .lib or the .cpp files?
I would greatly appreciate even if someone just pointed out mistakes in my understanding.

Configuring a Gurobi C++ project using Eclipse? [duplicate]

I would like to use Gurobi in a C++ project I have in Eclipse. I tried multiple manuals/tutorials (including THIS on how to do the same in Visual Studio) to add the hook up the Gurobi files with Eclipse/GCC, but I just couldn't make it work. I feel like I don't understand enough how these things should work in the first place.
In my Gurobi folder I have 3 folders, that in my opinion are important: include (with .h files), lib (with .lib files and NO .so or .a files) and src (with .cpp & .h files).
include: I added the path to this folder to C/C++ Build -> Settings -> GCC C++ Compiler -> Includes -> Include paths, I guess this is for the compiler to see the .h files. I also added it to C/C++ General -> Paths and Symbols -> Includes -> Include directories, I guess this is for the linker to see the .h files.
lib: I first added the folder path to C/C++ Build -> Settings -> MinGW C++ Linker -> Libraries -> Libraries (-l), however after searching online it seems to me this option only works for libraries with standard names and extensions. It also gave me the error ld.exe: cannot find -lc:/gurobi900/win64/lib/, so I deleted it and added the two .lib file paths separately to Miscellanous -> Other objects.
I got to the point where #include "gurobi_c++.h" works, however, already Eclipse underlines a line where I am defining a GRBEnv() object, that is declared in include/gurobi_c++.h, but defined in src/cpp/Env.cpp. Also if I try to compile, I get undefined reference errors. This means neither the Linker nor the Compiler can see the definition of GRBEnv().
Is there a way to tell the linker/compiler where the .cpp files are? Is it happening automatically, based on the path of the include folder? Am I supposed to link the .lib or the .cpp files?
I would greatly appreciate even if someone just pointed out mistakes in my understanding.

Incorporate Open Source C++ Code Libraries into Xcode 5.1

I am new to programming in Xcode and I want to be able to use open source code in my future c++ projects (e.g. machine learning/ computer vision libraries). Additionally, I never really understood the process involved in linking and using any c++ libraries besides the standard library, and I was hoping someone could outline the process or point me in the right direction.
I would really appreciate some help starting with the downloading of the 3rd party source code. I have been searching around finding bits and pieces of the process, but none of the posts seemed to be specifically directed to my problem, so please let me know if I have done something wrong already.
I began by downloading the dlib library. I located the source code with cmake, I clicked configure and generate without changing any options and this gave me an Xcode project. I then built the project for running and this produced the file libdlib.a in the associated debug folder. I then dragged the file into the link with binary libraries. My archive file is now located in the project, but I'm not sure what to do from here.
How can I get Xcode to recognize this with the include function?
e.g. #include < dlib > , #include < dlib/matrix.h > , using namespace dlib
What other steps do I need to take to be able use the functions/objects defined by the library?
Do I need to include any source code besides the archive file in my project?
Will this process roughly be the same for any library I download, or does it need to be specifically tailored to each?
Is there a specific location on the computer where I should put these libraries? or do I just need to identify their location in Xcode?
After following this process, will Xcode "know" all the associated functions from the library?
e.g. if I start typing, will it fill in the rest of the name, or notify you the necessary arguments for each function?
Thanks for the help,
Jake
The C and C++ build process is as follows:
preprocessing
compilation
assembly
linking
The steps you need to configure in order to use a third party library are the preprocessing step, for the library headers, and the linking step, so that references to library entities in your program will be linked to the relevant code in the library binaries.
#include directives are handled by the preprocessor, which process a single .cpp file at a time and among other things replaces those #include directives with the contents of the named file. To do this it must know where to get the file from, which it gets from the "include search paths".
Some search paths are preconfigured for you, such as the ones for the standard library. For any other headers you will have to add the appropriate search path. On the command line this usually uses the -I flag with an argument.
clang -E -I /path/to/wherever/you/put/the/library/headers main.cpp
In your Xcode project's build settings you will find a place to add include search paths.
Just as you must link together the separate object files so that calling the function foo() in main.cpp will run your definition of foo() from foo.cpp, you have to link to whatever third party libraries you want to use. A .a file is a library archive file that is little more than a bundle of .o files. The linker knows how to handle this so you don't need to un-bundle anything, you simply need to tell the linker to link the .a file in your executable.
There's more than one way to do this, but the most common is to set the library search paths to include the directory where the .a is located, and to tell the linker to look for a library with a particular name. Setting the library search paths uses the -L flag, and as long as the .a file follows the usual naming convention (libXXX.a) then you tell the linker to search for and use the library with the flag -lXXX.
clang -L /path/to/libraries -ldlib main.o foo.o bar.o
You can also just pass the library like another object file:
clang main.o foo.o bar.o /path/to/libraries/libdlib.a
And again in your Xcode project settings there's an entry for library search paths. Adding the .a file to the "link with binary libraries" area should result in the -l flag being properly added, so you shouldn't have to add that separately.

Linking a static library causes errors in the linked library

I have a small library project that uses OpenGL (glfw and glew). Now, the project compiles fine, but when I create a new project and statically link the library project, VS starts to throw errors in the library project. Why is that?
More specifically, I get this error:
error C1083: Cannot open include file 'GL/glew.h': No such file or directory (file: trenums3d.h)
The project setup is like this: There's the library project 'Foo', which is compiled into a static library ('Foo.lib'). The application project 'Bar' links 'Foo' (I added the folder where Foo.lib resides to Bar's 'Additional Library Directories', as well as the source folder of 'Foo' to Bar's 'Additional Include Directories'). If I compile only the library project, everything works just fine, but compiling the whole solution give me the aforementioned error.
This isn't a proper answer to your question, but just an explanation of the steps required for building an application in a compiled language.
Building a project containing multiple files is a three-step process:
Creation and editing of source and header files
Compilation of the source files (this step contains many sub-steps). This step creates object files of all translation units
Linking of all object files and libraries to form the final executable
Error like the one shown in your question is emitted in the second step. Linking with libraries happens in a completely different step, and is usually done by a different program than the compiler.
To answer your question, if linking with a static library also requires linking with the other libraries that the static library depend on, then the answer is normally yes. Static libraries only contain the function in the actual libraries, you can look at a static library more as a collection or archive of object files. Static libraries does not contain any information about other libraries they depend on.
And as for your problem, with the pre-processor error, it's because you include a header file from your static library, and that header file in turn includes some header files. But the pre-processor doesn't have the secondary included header files in its default search path, so you need to add it.
This still have nothing to do with linking any library, this is a pure pre-processor issue, and is handled in step two in my list above.
I suspect the header files of your static library look like somewhat this:
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "GL/glew.h"
// ...
#endif
If you include this header file from another library or application, the compiler will open this file and will see that it needs to open GL/glew.h as well in order to be able to "understand" the definition of your class.
This means you need to supply at least the header files of glew. The only way to get rid of this is if you manage to only reference glew files from your .cpp files but not from your .h files. In some cases, forward declarations can be used, but not sure if this will work for glew.
Concerning the linker settings: In case your glew library is built statically as well, you may or may not have to supply that library file and link to it from your project. This depends on how you setup your linker for your own static library. If you have troubles in this step, create a new question.

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