How to create separate library for include in C++/Eclipse - c++

I've gotten some C++ code to work with the TinyXML parser. However, to do this I had to include the source code from TinyXML with my regular source code. I'd like to have TinyXML included as a separate library. I'm using Eclipse with the Cygwin C++ compiler. What's a good way to do this?

I assume you want to separate the library from your own project's source code... but you don't know how to build when the library is not in the same folder.
Assuming your library has precompiled *.lib and *.h files:
Move the library source code to a separate directory
Menubar "project"
Menu "properties" will open a dialog box for all the project properties there will be a list on the left.
List item "C/C++ Build" will change the GUI and show you all the options for gcc's compiler/linker/assembler ( I never do assembly... so I never do anything with the assembler ). [1]
GCC C Compiler --> Directories:
Green plus icon [2] --> Specify the path of your *.h files
Your compiler should now be happy ( but you will fail linking because the linker doesn't know what the actual definitions of each function are )
GCC C Linker --> Libraries:
Library search path (-L) --> Green plus icon --> Specify the path of your *.lib files
Libraries (-l) --> Green plus icon --> Specify the name of each library you are using
Your linker should now be happy and your code should compile
[Footnote - 1] The GUI C/C++ build pane is a wrapper for gcc's command line compiler/linker... it is just making it easier to use because it shows you everything visually.
[Footnote - 2] The '+' icon is what will tell the compiler where your libraries *.h include files are located. The compiler needs the *.h files to know what function prototypes your library has before it compiles.
Assuming you have the actual ( not compiled ) *.c and *.h:
Do the same steps above except in step 7.
At step 7. you need to make sure the library's *.c files are seen by Eclipse's "managed make". If it doesn't see the source code then you need to specify where the source is so that it will compile it.

It's basically easy. You compile your source code for the library, and construct the library with ar(1). Yes, surprise, a library is just an archive; UNIX is cool that way.
You can then include the code as a static library when you build the final code.
I don't use Eclipse all that much so I can't tell you the exact process within the IDE, but I believe what you need is to set up a separate project to build it.
Now, if what you want is to build a DLL, then you need to use some special flags. There's a nice page here.

Related

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

Xcode 3 ctime declaration issues

I am in the process of porting my c++ engine to mac, and so I used premake to generate an xcode project, which it does fine.
Box2D is built into the engine and one of its files "b2Broadphase.h" is including algorithm from the c++ standard library.
This is giving these errors: ::clock_t has not been declared and so on for all the using commands in the ctime file.
I cannot figure this out because when using premake to build a make file it runs fine and build a perfect library on OSX. Its only Xcode giving these errors.
My guess is that Xcode has not been configured to include the implementation files (.m or .cpp) or it has not been configured to link against a library which you are using. In general, you need to do two things: 1. include the headers 2. link against the libraries with the actual executable objects.
In Xcode, you do this by selecting the project (top-most item) in the file-browser panel on the left, and there is a section in the main area to choose which libraries to link to. You must specifically tell it to link against whichever libs you are using, even if you have imported their headers.
For .cpp or .m implementation files, you need to tell it to include that file in the target for compilation. This can be done either in the build settings (similar place to the lib inclusion) or else when you have a file selected, the inspector panel on the right has a little area for you to choose which targets to include the file in. (you only need to "include" implementation files like this, not .h files)

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.

DLL References in Visual C++

I have had C++ experience but not MSVC.
What I am trying to do is incorporate a .dll from an open source project into my project. The code is available and I have built it. I have the .dll as well as the .lib which as I understand it is required for C++ projects.
Now unfortunately there is no simple "Add Reference", drop my .dll into an include directory and add that to my solution. I have edited the project property pages, the C/C++ Additional Include Directories option as well as adding the .lib as an additional linker dependency. I have created an include directory for the dll and lib inside my solution tree.
My problem is when I try to include the header files from the documentation, VS output spits out error messages. Now I realize that I am using the dll/lib combo and that the .h files are not present in my solution so how do I add the proper includes? I am using QT toolkit also which is working but how I add the other header / dll from the open source library eludes me.
Can someone please point me in the right direction.
You need to do a couple of things to use the library:
Make sure that you have both the *.lib and the *.dll from the library you want to use. If you don't have the *.lib, skip #2
Put a reference to the *.lib in the project. Right click the project name in the Solution Explorer and then select Configuration Properties->Linker->Input and put the name of the lib in the Additional Dependencies property.
You have to make sure that VS can find the lib you just added so you have to go to the Tools menu and select Options... Then under Projects and Solutions select VC++ Directories,edit Library Directory option. From within here you can set the directory that contains your new lib by selecting the 'Library Files' in the 'Show Directories For:' drop down box. Just add the path to your lib file in the list of directories. If you dont have a lib you can omit this, but while your here you will also need to set the directory which contains your header files as well under the 'Include Files'. Do it the same way you added the lib.
After doing this you should be good to go and can use your library. If you dont have a lib file you can still use the dll by importing it yourself. During your applications startup you can explicitly load the dll by calling LoadLibrary (see: http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx for more info)
Cheers!
EDIT
Remember to use #include < Foo.h > as opposed to #include "foo.h". The former searches the include path. The latter uses the local project files.
The additional include directories are relative to the project dir. This is normally the dir where your project file, *.vcproj, is located. I guess that in your case you have to add just "include" to your include and library directories.
If you want to be sure what your project dir is, you can check the value of the $(ProjectDir) macro. To do that go to "C/C++ -> Additional Include Directories", press the "..." button and in the pop-up dialog press "Macros>>".
You mention adding the additional include directory (C/C++|General) and additional lib dependency (Linker|Input), but have you also added the additional library directory (Linker|General)?
Including a sample error message might also help people answer the question since it's not even clear if the error is during compilation or linking.