How to link Boost in a dependent static library - c++

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.

Related

Interesting LNK1104 error when using the static version of boost::filesystem

I'm trying to test out some code which makes use of boost::filesystem for various things.
I built the static version of boost 1.51 for vc11, and the intended libraries came out as expected.
Here are the flags I used:
b2 --with-filesystem --build-type=complete --build-dir=.\build link=static runtime-link=static
And here's the list of files it generated:
libboost_filesystem-vc110-mt-s-1_51.lib
libboost_filesystem-vc110-mt-sgd-1_51.lib
libboost_filesystem-vc110-s-1_51.lib
libboost_filesystem-vc110-sgd-1_51.lib
libboost_system-vc110-mt-s-1_51.lib
libboost_system-vc110-mt-sgd-1_51.lib
libboost_system-vc110-s-1_51.lib
libboost_system-vc110-sgd-1_51.lib
I then created a new Win32 DLL project and added the proper include/lib directories.
But when I try to compile, I get the following output:
LNK1104: cannot open file 'libboost_filesystem-vc110-mt-gd-1_51.lib
But this file does not exist because (afaik) it IS NOT the static version of the library...
Which is odd because I never asked to link against the DLL version anywhere in my project!
So why is my project complaining about a library which I never asked to use?
Do the boost headers automatically try to link against their corresponding libs?
Is there some sort of preprocessor flag I should be setting to tell boost I want to use the static, single-threaded version of boost::filesystem?
UPDATE:
I have been informed in the comments that boost does in fact try to auto-link for compilers which support it, via <boost/config/auto_link.hpp>...
After several attempts to properly configure these headers to use the static, /MDd (debug) & /MD (release) versions of boost, I am still getting this error.
So if anyone could tell me how to properly configure OR disable this feature, I will mark it as the answer.
Also, am I correct in assuming that libboost_filesystem-vc110-s-1_51.lib and libboost_filesystem-vc110-sgd-1_51.lib are the proper /MD and /MDd libs?
Thanks!
To disable auto-link you need to define BOOST_ALL_NO_LIB. From the boost documentation:
// BOOST_ALL_NO_LIB: Tells the config system not to automatically select
// which libraries to link against.
// Normally if a compiler supports #pragma lib, then the correct library
// build variant will be automatically selected and linked against,
// simply by the act of including one of that library's headers.
// This macro turns that feature off.
But don't do that - it will not solve your problem. Auto-link is usually right, i.e. you are either linking to the wrong libraries or you have incorrectly configured your preprocessor macros. If linker wants to link to the shared libraries I would guess that you defined BOOST_ALL_DYN_LINK or BOOST_FILE_SYSTEM_DYN_LINK and BOOST_SYSTEM_DYN_LINK. Remove it and it should link just fine.

The library file name with "lib" head can't be recognized in codeblocks and vs9 compiler

This environment is win7, codeblocks,vs9,boost1.52,
I have installed boost library with python library.It generates some library files.Such as "libboost_python-vc90-mt-gd-1_52.lib".
Then I write a test code to compile and link.But an error has occurred: can not open file "boost_python-vc90-mt-gd-1_52.lib".
The VS compiler don't recognized a library file name with "lib" head?How to do it better.
Adding a lib prefix at the start of the name is a Linux thing. On Windows there is a .lib at the end instead.
If the library is named "libboost_python-vc90-mt-gd-1_52", you have to give exactly that name to the linker.
First of all, Boost uses auto-linking under Windows. Thus, you only need to provide the location of the boost libraries, but not the names themselves.
Second, Boost uses different filenames for statically and dynamically linked libraries. The ones with lib prefix are for static linking, the ones without for dynamic linking.
By default, Boost uses static linking on Windows. Defining BOOST_ALL_DYN_LINK (there are library specific macros as well if you only want to dynamically link certain libraries) will enable dynamic linking.
From your error message, it seems that your project has BOOST_ALL_DYN_LINK enabled (either in the project settings, as a #define or in <boost/config/user.hpp>. If you prefer static linking, make sure to change/remove this setting.
Either way, make sure that the respective binaries are available and that their location (just the directory) is known to the linker.

g++ Linking vs VisualC++ Linking

I have been trying to get this working for a while now and am unable to find an answer elsewhere, here is my problem.
When I make a static library in Visual C++ any dependencies that this library uses are carried on to the executable program that I link the library to. Here is an example
Test.lib depends on
- SDL
- OpenGL
TestApp.exe links Test.lib
In Visual C++ I do not have to add the dependencies for Test.lib in my TestApp executable, they are carried over and it works great.
However when I port my code to Linux g++ (With the Code::Blocks IDE), if I make a Static Library which is .a in g++, and I make a TestApp that links the library, it gets undefined references to the dependencies.
Is g++ able to do this, and if so what am I missing to have the dependencies carried over to my executable?
As for my settings for my library I simply use the IDE's Static Library setting.
With Microsoft's compiler, header files can have library dependency information in them (source files, too, but this is typically done in headers); this gets compiled into the object file, and the linker understands and applies that information. That can be handy: you don't have to remember long, funky names, and if you compile two source files with incompatible options you may get a library name conflict that the linker will complain about. Most compilers and linkers don't do this kind of thing, and you have to tell the linker explicitly which libraries you want to link with.
Static libraries do not statically link with other libraries. Does that sound right?
However you can pack many object files together with a tool called ar.
What happens on windows is probably because you have the (SDL, opengl32) dlls somewhere in a system env path.

Linking with boost .lib

I'm getting a link time error fatal error LNK1104: cannot open file 'libboost_serialization-vc80-mt-gd-1_44.lib' I recently downloaded boost which came with boost_serialization-vc80-mt-gd-1_44.lib but the compiler seems to be looking for one starting with libboost so i renamed it. but then it fails to find .dll at runtime. I tried to rename the dll but doesn't seem this helps.
While loading dll: An unhandled non-continuable STATUS_DLL_NOT_FOUND exception was thrown during process load
There are many different versions of each boost library: multi-threaded, single-threaded, static, dynamic, etc. The ones starting with "lib" are the static libraries, the ones without "lib" are the lib files used to link to the DLL versions. You can see this by looking at the lib file size.
If you are auto-linking boost, it means that the linker is looking for the static version of serialization. If you don't have such a file, it means you didn't build it. Another alternative is to use the installer available at http://www.boostpro.com. It allows you to select which boost libraries (static, dynamic, ST, MT, VS Version, etc) to download and install.
The other option is to disable auto-linking: see this post on the boost mailing list: http://lists.boost.org/boost-users/2005/12/15697.php

Boost autolinks libraries which are not built by Boost, but the intended ones are built

I am developing a Math application which can be extended by writing python scripts.
I am using Qt 4.6.3 (built as static library, debug and release versions) and Boost 1.43.0 (built as static library, runtime-link also set to static, multi-threaded version, debug and release). Everything is built with MSVC++2008. Boost built the following libraries:
libboost_python-vc90-mt-s-1_43.lib
libboost_python-vc90-mt-s.lib
libboost_python-vc90-mt-sgd-1_43.lib
libboost_python-vc90-mt-sgd.lib
My project compiles, but gives the following error during the linking phase:
1>Linking...
1>LINK : fatal error LNK1104: cannot open file 'boost_python-vc90-mt-gd-1_43.lib'
Why is it not selecting one of my compiled libraries?
I think the s in the library names stands for static, but then the auto-linking feature seems to select a dynamic library, and I want it all linked statically in one executable.
The same happens with the regex library: I have the same 4 regex libraries compiled and a quick test shows this linking error:
1>LINK : fatal error LNK1104: cannot open file 'libboost_regex-vc90-mt-gd-1_43.lib'
What to do?
You can define BOOST_ALL_NO_LIB. This prevents automatic linking of boost libraries and you must then link the required boost libs manually.
If 's' stands indeed for static (I don't know all those modifiers by heart), define the BOOST_ALL_DYN_LINK symbol while compiling (add it to the command line options). It tells boost to link to the DLL libraries. Alternatively, compile/install static boost libraries.
The problem is fixed, during the compilation of the boost libraries, I selected the link=static option. Which creates static libraries. I also selected runtime-link=static option, and this was wrong!
The solution for this problem was compiling boost with runtime-link=shared. Now some extra libraries are added, with the correct filenames, so the linker can find them. At first the compiler still searches for the dll library (boost_python-vc90-mt-gd-1_43.lib, instead of libboost_python-vc90-mt-gd-1_43.lib), everything else from boost links automatically to a static library, but because boost.python has a different auto-linkage set up, when you provide BOOST_PYTHON_STATIC_LIB, it finally links to the right library and it works!