What is the best approach to simplify linking to my library? - c++

I want to build a library which is easy to use and link to. Therefore, I do not want the user of my library to bother with building and linking against the libraries my library uses. What is the best approach of managing and building/linking the dependencies for your own library in order to assure the most simple usage of it?
E.g.: My library uses CURL and OpenSSL. Building and linking on Windows with those dependencies is a pain. I want to avoid this pain to the users of my library.
I use CMake for building.
Related to: Combine libraries and Linking static libraries to other static libraries

Related

Does a statically linked libcurl still use dynamically linked libraries?

*Disclaimer: I am a newbie who is trying to learn about how to use third party libraries. *
I’m looking at using libcurl for a project and I want to statically link it to my project so that it is included in my final executable.
Some documentation online says:
By default, configure will setup the build files so that the following make file command will create both shared and static versions of libcurl. You can change that with the —disable-static and —disabled-shared options to configure.
If you instead want to build with static versions of third party libraries instead of shared libraries, you need to prepare yourself for an uphill battle....
Are they talking about the libraries that libcurl uses? I guess what I’m asking is: even if I build libcurl and link statically, will libcurl still be linking dynamically to 3rd party libraries?
Is that what this is saying?
The thing is, a static library is really nothing more than an archive of object files. Linking with a static library is the same as linking with the separate object files themselves. That means that any 3rd party dependency that the library have also becomes a dependency for your application. And you must link with those 3rd party libraries and dependencies when you link your application.
Now for libcurl itself, depending on how you configure and build libcurl, it may depend on some 3rd party libraries that can't be linked statically to your application.
So to answer your question: It depends.
It mostly depends on how you configure it for building, and what 3rd party libraries you tell it to use, and if those 3rd party libraries are available as static libraries (and then on to their dependencies and so on).

cmake build a shared library that contains all its dependencies

I have built a shared library that depends on a bunch of other libraries. But when loading that shared library, it needs the dependent libraries to be present in order to run properly. I want this shared library to be portable and hence want it to contain all the dependencies in itself. Is there a way in cmake to do this or what is the best solution here?
Actually this is not related with CMake, but with concepts of linking. You should link with static version of all your dependent libraries.

Distribute a transparent static library

I want to distribute a closed source C++ static library, let us call it MyLib. This library uses Boost.
What is the best way to give the ability for the developers who are going to use MyLib to link against whatever they want configuration of boost.
In other words, They may already be using a static version of Boost or a shared version of it linked to their program. So, i want to leave the option to them.
P.S. I am building my library with CMake and distributing a CMake package for it for the final developers.

Linker dependencies when mixing static and shared libraries

I have a following question about linking on Linux:
Suppose I have a class Foo that uses Qt. To build this class I'd have to use qmake to generate Makefile.
Later on I want to use this class Foo for a Perl module, which is a shared library. However, to build it I have to use Perl's MakeMaker to generate Makefile of it's own.
The way I'm doing it right now is that I build class Foo as a static library, and when building Perl module's shared library I'm linking it against Foo's static library.
The problem is that when building Perl module's shared library I have to link it against all those Qt libraries that Foo's static library is linked against.
So the question is:
Does this approach even make sense?!
Is it possible to build Foo's static library in a way that I wouldn't have to specify all it's dependencies when building the Perl module's shared library? (Because it is somewhat hard to add all those dependencies to module's Makefile)
Would it be any different if Foo's library was shared, not static?
1) You can't link a static library against a shared library.
2) You would not need to explicitly link against Foo's dependencies if Foo itself was a DSO
3) You can easily modify the Makefile.PL LIBS section to add extra linker dependencies.
4) Qt is a downright pain to link statically anyway. You're better off just doing the whole dynamic shebang unless you have specific version dependencies and OS/platform limitations. Hint: Even if you do have a 'static' build of Qt, this 'static' build won't include things which it might decides need to be present as loadable modules. Been there.
5) I believe there's a CPAN module (somewhat recent) which provides Qt4 bindings. I've never used it and don't know its status, but it may be worth checking out.
But your best bet is to make Foo a dynamic library.. everyone's happy then.
1) It depends on what your objective is.
2) If it's about minimizing the hassle building it, you could include Qt's static libraries as static to your Foo lib. All that matters is that the symbols you reference in Foo can be found at runtime. That way you won't need to include Qt libs in your PerlMake.
3) If it's about minimizing executable size you'd have to go for shared libraries. Then you should build everything as a shared-lib.
Building statically has the advantage to be independent of installed shared libraries on the target platform, while having the disadvantage of executable-size bloat and non re-usability of libraries (more memory needed to load two same executables).
Linking against shared has the advantage of smaller code size but at the same time the proper shared libs have to be installed on the target platform.

Building a new library on top of another library (SDL and Eclipse)

I am working on a project with SDL. I feel like a lot of my code could/should be reusable for any other projects I might want to take on.
I was going to start a library project, but I don't know how to go about linking to the SDL libraries.
Usually, I go to Project Settings and edited Mingw32 Linker to include the libraries (ming32, SDLmain, and SDL). But the linker does not show up in the settings.
So my questions:
Is there no linker settings because building a library is only a compiling operation?
And in general, is it possible to build a library on top of the existing libraries? That is, write my library using SDL functions and stucts. Or would I have to get the source code and rebuild entirely with my code included?
Also, advice on shared vs. static in this instance?
(Also, any advice for where to go about learning more about compilers and linkers and such. I got through data structures in school but no farther)
As an introduction, you have to distinguish very well static library from dynamic ones, they are completely different beasts... said that, to your questions:
Is there no linker settings because building a library is only a compiling operation?
I guess you are creating a static library in this case. A static library is simply the collection in one single object file of all the individual object files (i.e., the .o files produced by the compiler) that make up your source tree. No more, no less.
With a static library you don't need to specify which are the dependencies, since it is understood that it is when compiling the final executable that your library will be linked with all the other libraries that it depends upon. Therefore it is only at that time (final executable build) that any missing symbol will be detected, and all other libraries must be available.
A shared library (also dynamic library), is an executable file that embeds all the static libraries that it depends upon. It can also have external dependencies with other shared library, which would not be embedded.
And in general, is it possible to build a library on top of the existing libraries? That is, write my library using SDL functions and stucts. Or would I have to get the source code and rebuild entirely with my code included?
It is perfectly possible, both for static and dynamic libraries.
Also, advice on shared vs. static in this instance?
In this instance it is not possible to advice, because you don't specify enough information.
Look at this: When to use dynamic vs. static libraries, and this, to have a sort of guideline.
(Also, any advice for where to go about learning more about compilers and linkers and such. I got through data structures in school but no farther)
I think that the two links above give you plenty of information. If you want to further go into details, you could start from this wikipedia article and browse from there.
The library you are building can have external dependencies. That means that you can link it with SDL or any other external libraries that you like.
I think this page explains all your other questions: DLL Creation in MingW