Linking to a library that links to a library - c++

I'm trying to link cpgui to my library, which links to SFML. I use code::blocks so I had to make my own project for that library, and as it requires SFML I statically linked to SFML in that library and compiled it fine.
Now, when I attempt to statically link that library to my library, I get a bunch of undefined references to SFML when I compile my project. Even if I linked to SFML in both projects, what's happening?

As you guessed, you can make it simpler by adding the library files directly to the project.
Another solution as suggested by AJG85 would have been to link the library -- after taking care of conflicting dependencies.
Use relevant documentation as suggested by answer to How do I link a library to my project in CodeBlocks & GCC without adding the library source to my project

Related

manage order of static link for IOS in XCode

QUESTION: how to manage linking order in xcode?
DETAILES:
I'm new in Apple-dev and have some troubles with building my project for ios.
I'm building OGRE3d(1.10) and MyGUI(3.2) libraries from source code via cmake and ios-toolchain.cmake.
MyGUI using functions from ogre and I like them using cmake function target_link_library. And I have any .a static libraries. We will name them ogre.a and mygui.a.
Then I've create ios project and create Objective-c++ file(.mm) where tried to use mygui and ogre function. To link mygui and ogre i've drag them and drop to the root of project structure of xcode and i see them in "linked framework and libraries".
But I have linking errors, which says that ogre function can't be linked to MyGUI (undefined symbols).
As I think the reason of the errors is wrong linking order of the libraries.
So, I tried to move the order on the project struct, but nothing changed. It seems that changing order libraries in project struct doesn't change linking order. So how can i manage it?
I've seen the same question on OGRE forum.
But there is no answer :(

Linking to a library that links to a lib I'm already linking to

Forgive the convoluted title.
The setup for this problem is as follows:
I have an open source lib I have built into a bunch of .libs (VTK if you were curious)
I have a library that uses the aforementioned static lib. Lets call it Lib A.
I also have an application that uses the aforementioned library (i.e. VTK) AND also uses Lib A.
During build time, I get a linker error telling me that a function called from Lib A has already been defined in a library that is linked to the application (error: LNK2005)
Any ideas on how to fix this short of switching everything to be dynamically linked?
Alright. I figured out what I was doing wrong.
Lib A was using the statically built version of VTK while the main app was linking against a dynamic-linked version of VTK.
So the problem really was that I had the same functions defined in a .lib and a .dll which caused the linker to fail.

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.

Why doesn't Libtool want to link with a static library?

I want to build a shared library that uses ZipArchive using GNU Autotools but I'm having this problem:
Warning: linker path does not have real file for library -lziparch.
I have the capability to make that library automatically link in when
you link to this library. But I can only do this if you have a
shared version of the library, which you do not appear to have
because I did check the linker path looking for a file starting
with libziparch and none of the candidates passed a file format test
using a file magic. Last file checked: /usr/local/ZipArchive/ZipArchive/libziparch.a
The inter-library dependencies that have been dropped here will be
automatically added whenever a program is linked with this library
or is declared to -dlopen it.
If I build a static library or if I use a shared library of ZipArchive it works but the problem is that the makefile that comes with ZipArchive source code only builds a static library.
How can I force Libtool to link with a static library?
Generally, static archives are created with non-pic object files and they cannot be put into shared libraries.
What this message is telling you though, is that when a program links to YOUR library using Libtool, that -lziparch will get added to the link. So you don't need to change anything unless you're building a module for an interpreted language. In that case, you will have to build ZipArchive as a shared library. In addition, this wouldn't work on a platform like MS Windows where shared libraries (DLLs) have to resolve all their symbols at link time.
All that said, if your ziparch static lib is PIC code, you can use the -whole-archive flag when linking it to your library. This would be the least portable solution though.

How to link Boost in a dependent static library

In MS Visual C++ 2010
I had a single C++ project in my solution which used boost and worked perfectly.
I then decided to convert this project into a static library and create a new project which depends on this static library.
Now, my converted static library builds without errors and warnings (compiler and linker)
but the new project compiles but does not link.
I am getting:
1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-1_45.lib'
As a test I added the full directory path to the linker options for this library... and then it complained about
1>LINK : fatal error LNK1104: cannot open file 'libboost_date_time-vc100-mt-1_45.lib'
I have now added complete paths to all the libraries and it now builds and run.
I am not happy with this solution because:
I don't want users of the library to
have to worry about linking in
boost.
It is messy
I know an answer would be to create a DLL but is there a way to do this statically and keep the linking at my static library level.
Edit:
If I tell the .exe linker to ignore the boost libs explicitly then it all is ok except the .exe should not have to worry about boost at all.
/NODEFAULTLIB:"libboost_thread-vc100-mt-1_45.lib" /NODEFAULTLIB:"libboost_date_time-vc100-mt-1_45.lib"
Apparently you don't need the .libs, as your exe also links without them. You seem to be using boost header-only methods and classes. So just tell boost to disable auto linking by defining the preprocessor symbol BOOST_ALL_NO_LIB in your project.
If you want to make your .lib unnecessary big by including all of boost, this question seems to hold an answer (which I never really tried myself): Linking static libraries to other static libraries
When building your library, you can include the boost libraries in yours. To do so, in VisualStudio's Librarian > General property page, list your boost libraries as Additional Dependencies.
However, there may be a problem if your clients use boost themselves, and statically link to it (especially a different version than the one you are using).
Did you build boost library? There are certain libraries in Boost that needs to be compiled. In case if you haven't done that, refer to "Getting started in Windows" on how to build the Boost library.
EDIT-1: Boost can be built both as a static and dynamically loadable (dll) libraries.
EDIT-2: If you have already built Boost, then the answer by #Daniel Gehriger tells you how to add it in VS.