Add library to existing project netbeans - c++

I am adding extensions to an (another persons) existing project at my company. Now I want to import an existing library like boost to it. I am using netbeans for debugging the existing project. Now in order to import a library into netbeans usually 2 steps are followed:
Include directories
Linker-> Add Library.
However when I right click on my existing project the option of Linker->Add Library is not appearing. (Though I have included the directories as that option is there).
Can someone please guide me as to how should I add the library through linker to my existing project? My project is in C++

Assuming you are using unix/linux variants:
Directories for headers and library linking are two different things. Include directories will have the headers needed, but after compilation the actual compiled code that resides in the libraries (*.a, *.so, etc...) might also be required.
For example, if you are using pthreads, apart from the headers which you need to include, you also need libpthread.
When linking, you need to provide the flag for linking with pthread i.e: -lpthread
You can search using find or locate on a unix system to find the libraries. In my case, its in
/usr/lib/libpthread.so
Therefore,
gcc myfile.c -lpthread -o myfile
Will link myfile.c with pthread library
Whereas,
gcc -L/usr/local/lib/
Tells gcc to look under /usr/local/lib to search for the library (not the header!).
Telling netbeans where the headers are, isn't enough, it will probably give you linking errors. Telling netbeans where the libraries are, may be enough, as it might use the proper flags. If that also fails, then you have to specify both the library flags and the path.
Alternatively, you may use tools like cmake, make, etc which automate this process and provide a bit more control IMO.
See Link 1
See link 2

Related

Build a specific Boost library using a makefile

I am trying to include, the boost::filesystem library into a project.
The project has to run on different architectures, so I don't want to link to the boost libraries installed on my ubuntu machine.
Most of boost is header only, so I would just have to include it and not worry about building it. But the filesystem library is NOT header only and I have to build the binaries for it.
Doing that would be easy, just pass in the header using the -I flag, and during the linking state, pass in the location to search for the binary using -L and specify the library using -l.
But I don't want to use the system headers and binaries.
So I am keeping a copy of the boost::filesystem headers within my project, using the bcp tool that boost provides.
I am able to do this, and I have a subdirectory lib/fs/boost/filesystem.
When I try to build a cpp file, dependent on filesystem, the compiler is able to find the headers and everything works.
But during the linking state. It cannot find the binary and the build fails.
So how do I build the boost::filesystem ?
1) I found the bootstrap.sh and b2 way to do it. I am not sure how to use it with the bcp tool though. I was building the library from the boost root.
But a disadvantage this this approach, is that I am complicating things. Before running my makefile, I will have to run bootstrap and b2 to get the binaries. It doesn't seem clean and I would like to avoid this.
2) Is there a makefile rule that I can use to build the boost::filesystem. The project is build using a makefile, and if I could specify a rule to build the filesystem, and then link the resulting binaries, it would be great.
I searched online and I couldn't find a makefile to build a boost library. Can you point me at where I could find such a makefile ?
The question is verbose, but I am just doing it to make sure I am not making any wrong assumptions.
Thanks a lot.

Include a static library in a static library - CodeBlocks

I'm having an issue compiling a static library using Code::Blocks 13.12. I need to use a third party static library from within my own static library. So, I have libOtherLib.a and I'm trying to build libMyLib.a and link in libOtherLib.a. The problem is that the linker is not including libOtherLib.a during the linking phase of the build. Here is some additional information:
I am using the GNU GCC Compiler
In Project build options for the whole project (not specifically Debug or Release)
I have added libOtherLib.a in the Link Libraries list in the Linker Settings
I have added the path to libOtherLib.a in the Search directories -> Linker list
I have added the path to the .h file for libOtherLib.a in the Search directories -> Compiler list
The library compiles completely fine (produces bin/Debug/libMyLib.a with no errors)
Any help would be greatly appreciated. I have an inkling that it is related to this being a Static Library rather than an application (console or otherwise), but I'm not sure how or why. I did change the build target Type to "Console Application" in the Project Properties window and it looked like it was linking in libOtherLib.a, but it had other errors because this code is meant to be a library rather than an application.
Here is the linker command that is executed at the end of the build. libOtherLib.a is not there anywhere, that is the problem, just not sure what the solution is.
ar -r -s bin/Debug/libMyLib.a <all of my .o files>
Possible causes of the problem would also be nice - if this is mostly like the compiler, the linker, the setup or Code::Blocks itself.
When building a static library you are just putting together a bunch of object files into an entity easier to ship and use. There is no linking done when building a static library.
The unresolved references from your library are dealt with when building an application or certain shared objects. You'd just ship your library and require that users also supply the library your library deoends on when building.
If you want to include the library you are depending on in your library you can extract the object files from tgat library and include them into your library. Although technically possible it is questionable if you have the rights to do so. Also, that is normally not the way things are done and I'd recommend against doing so.
You can't link a static library in anoter static library.
However,you can do this:
Suppose MyPrograme.exe need to link static library libMyLib.a,make MyPrograme.exe also link libOtherLib.a.
Since libMyLib.a is static library,there is no need to link libMyLib.a.Just inclue the headers.
When build a static library,it will only be compiled,not be linked.
Despite all this "linking is not the right term for this" philosophy, you can patch Code::Blocks compiler configuration to support this, for example for the GCC/G++ compiler.
http://green-candy.osdn.jp/codeblocks_config.html
The idea is that you replace the "Link object files to static library" script in the "Advanced compiler options" window of GCC with:
rm -f $static_output
$lib_linker -r -s -T $static_output $link_objects
$lib_linker -r -c -T $static_output $link_options
Then you can put the relative path to your "libOtherLib.a" in the "Other linker options" editbox of your projects. This mod is not really officially endorsed but it works in my projects, so you get static libs in static libs just like in MS Visual Studio!

How do I link a library to my project in CodeBlocks & GCC without adding the library source to my project

I am writing a program that uses the hashlib++ library (or will use it) but I don't want to add all of it's source files to my project because it's huge. Is there anyway to link to the hashlib++ source files so that I can use it in my project? I've tried linking to the header directly with a simple
#include "path/to/hashlibpp.h"
But I receive a nifty error for it as soon as I attempt to call any functions from the library. For example:
undefined reference to `sha1wrapper::sha1wrapper()
I am using the Code::Blocks IDE and GCC compiler.
First you have to have the library installed on your machine, already compiled into a static or dynamic library file. You can install from source, or you may find a pre-built package available for your OS (depending on which OS you are using). You will need to know the name of the library.
In the case of hashlib++ they have provided instructions to build a static library from source in their README; see section 3.2.
In most cases, dynamic linking is the best choice. This means that the library is linked with the library at run time, instead of adding the library to your executable when it is compiled (which would make your executable file much larger).
Unfortunately, according to their README.txt, hashlib is only available as a static lib, which limits your choices.
When compiling a program on the command line using gcc, the '-l' option links in a library:
gcc -o MyProg -lhl++ MyProg.c
When using an IDE like Code::Blocks, you normally have to specify the libraries to be linked. See this answer for details on how to do this with Code::Blocks.

How to add a library to Eclipse C

I have seen several other answers in order to add a library to my C+= project in eclipse.I have tried to add the path to the linker in Miscellaneous section using -L"and the path of the folder" and -l"the name without the lib prefix in the begging and the .so at the end"
I try to add libxl library so i use -lxl (for libxl.so) and -L/home/username/libxl3.5.3.0/lib/ (which the location of the lib file).
I have also tried to give it under the Linker menu and adding the name and the path in the Libraries section.
I get error that: /usr/bin/ld does not find -lxl file and it returns error
I am using -static to linker in order to make an executable that has the all the libs included but when i do not use -static the problem with the lib resolves from build but still when i try to run the program i get error that i the program can not open shared file libxl.so cause the file does not exist.How can i fix this?
When you add the library name to a C++ project in eclipse, do not prefix it with -l. Eclipse will do this for you when it invokes the compiler. For example if you want the boost_regex library, just input boost_regex not lboost_regex. Eclipse will do the rest for you. Or in your specific case, just use xl not lxl. You don't need the - either, nor the -L before paths as erenon points out in the comment below. Note that the above applies to the method of adding libraries using the Project->Properties->C/C++ General->Paths and Symbols dialog form for adding libraries using the Libraries and Library Paths tabs.
You are trying to link statically to a shared library. In my experience I have always used *.a files rather than *.so files to employ static linkage. This other answer Static link of shared library function in gcc seems to suggest that you are not actually able to link statically to *.so files.

Compiler unable to find valid library path

I'm getting the following error:
ld.exe||cannot find -lD:\Libraries\boost_1_47_0\boost_1_47_0\stage\lib|
Even though the path is valid. Any thoughts on that?
Edit:
Thanks MichalR!
For others, in order to configure boost for gcc:
Download boost
install it according to instructions from boost site
In code::blocks in Settings/global variables add path to your boost library for base and lib fields (this should be located in stage/lib folder)
In code::blocks in Project/Build options..., linker settings tab, link libraries - add here those libraries
In code::blocks in Project/Build options...Search directories tab in compiler subtab add $(#boost) and in linker subtab add $(#boost.lib).
Done. I spent a few days on this, but it was worth it. Now I can use this great IDE with gcc compiler which at the moment of this writing is miles ahead of MS. I am not being stopped in my personal development by MS - C++11 here I come!
The params for ld are e.g.:
-larchive
-Lsearch_path
The first tells what library to look for, the latter specifies library search paths.
Perhaps you have these options mixed in your command line - it looks you have -l with a directory parameter.