24

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:

  1. I don't want users of the library to have to worry about linking in boost.
  2. 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"
Cœur
  • 37,241
  • 25
  • 195
  • 267
T33C
  • 4,341
  • 2
  • 20
  • 42

3 Answers3

27

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

Community
  • 1
  • 1
user52875
  • 3,020
  • 22
  • 21
  • 1
    This answer led me to an OK solution. I defined BOOST_ALL_NO_LIB and manually added the static boost libraries to the Librarian. My .exe project now does not need to know about BOOST, which was the objective. It is a pity that the automatic BOOST linking seems to propogate through the DefaultLibs. – T33C Jan 20 '11 at 10:35
  • Special Note for `Boost.Thread` You may need to use `BOOST_THREAD_USE_LIB` option. See http://shoddykid.blogspot.in/2008/07/getting-started-with-boost.html – vivek.m Sep 19 '12 at 13:13
  • 1
    I have the same problem... I solved it using the preprocessor BOOST_ALL_NO_LIB and linking de libs explicit in my project adding the directory of boost libs and add the only used libs to Linker->Input->Aditional Dependencies. By default the boost headers mount the name of libs in header . This is can verify with using preprocessor BOOST_LIB_DIAGNOSTIC. –  Jul 23 '13 at 01:21
4

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

Daniel Gehriger
  • 7,339
  • 2
  • 34
  • 55
  • Yes, I saw this option but can't get it to work. I increased linker output to verbose and I see that the .lib project links to the boost .obj files and the .exe searches and finds the boost libs and then tries to link to them. – T33C Jan 19 '11 at 15:46
  • Can you post the verbose build output (when building the library)? The boost libraries should be on your lib command line. As stated on http://msdn.microsoft.com/en-us/library/e17b885t(v=VS.100).aspx, `LIB creates one library that contains all objects in the specified files.`. So when your .exe links to your library, it should no require boost (unless you include boost in your library's public header files). – Daniel Gehriger Jan 19 '11 at 15:55
  • 1
    +1 Thanks Daniel, the problem seems to be with the way that BOOST auto links in the libraries. They are seen as defaultlibs and these get propogated along with runtimes. Defining BOOST_ALL_NO_LIB and linking in the boost libraries in my .lib works. – T33C Jan 20 '11 at 10:37
  • There are also the more specific and sometimes more useful BOOST_WHATEVER_NO_LIB defines to steer that more granular - see http://www.boost.org/doc/libs/1_55_0/libs/config/doc/html/index.html – wonko realtime Jan 22 '14 at 09:13
  • 1
    Thank you. BOOST_ALL_NO_LIB fixed my problems too. Making a library that uses ptime and then needing to statically link the gregorian time library to 7 exe's was NOT the desired way for this library to work – std''OrgnlDave Sep 01 '15 at 19:26
1

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.

yasouser
  • 5,113
  • 2
  • 27
  • 41
  • Thanks but please read my question again. I have built boost and can link to it OK. I don't want the .exe project to have to link to boost only the .lib – T33C Jan 19 '11 at 15:44