Linking to Boost libraries fails because of lib prefix - boost

I installed boost v1.47 DLL (MT, Debug) for VC9 from boostpro. My code #include's <boost/regex.hpp>. When I try to build, VC gives me LNK1104 cannot open file libboost_regex-vc90-mt-gd-1_47.lib, even though boost_1_47/lib has been added to Additional Library Dependencies (in double quotes). The problem seems to be that the name of the library in the directory is not libboost_regex-vc90-mt-gd-1_47.lib, but simply boost_regex-vc90-mt-gd-1_47.lib.
How do I get VC to omit the leading 'lib' ? Any help would be greatly appreciated.
Thanks in advance

In Boost the lib prefix denotes whether the libraries were built for static or dynamic (shared) linking. To indicate you want to dynamically link the libraries (as the name without the lib prefix implies) define the preprocessor directive of BOOST_ALL_DYN_LINK.
In Visual Studio, go to the project's settings, and select Configuration Properties > C/C++ > Preprocessor page. Add BOOST_ALL_DYN_LINK to Preprocessor Definitions.
See also this related question: https://stackoverflow.com/a/2521866/483776

Related

Boost VS2017 linking to the wrong DLL

I have a CMake file which does this:
find_package(Boost COMPONENTS system filesystem)
add_library(MyModule MODULE main.cpp)
target_include_directories(MyModule PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(MyModule Boost::system Boost::filesystem)
I'm using VS 2017 as my generator. When I generate the project file with cmake, it finds boost_system-vc141-mt-1_63.lib and I can see that it is in the linking rules of the vcxproj. However, when I try to compile I get this error:
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc140-mt-1_63.lib
Note the different generators (vc140 vs vc141). I know my compiler has output the right values because I built boost from source, so I tried to just rename vc141 to vc140, but the error stayed the same. I also confirmed that vc140 is not referenced in the project file.
What's going on? How can I force boost to link to the correct version?
When building with Visual Studio, boost has some pragma statements which do the linking for you. This is called "Auto-linking" and it over-rides any command-line arguments you may be passing to the linker.
The solution is to define BOOST_ALL_NO_LIB. This can be done in two ways:
In source code before including boost headers as #define BOOST_ALL_NO_LIB.
It could be added to your cmake file as: add_definitions("-DBOOST_ALL_NO_LIB").
As of CMake 3.5: Use the disable_autolinking imported target:
target_link_libraries(MyModule Boost::system Boost::filesystem Boost::disable_autolinking)

How to use Gurobi libraries in OMNet++

I am trying to use gurobi in OMNet++. I have included gurobi_c++.h and in order to detect this file, I have already told the compiler where it can find it and the libraries. To do so, I went to Makemake options, custom, Makefrag and added two lines: EXTRA_OBJS += -LC:/gurobi701/win32/lib and CFLAGS += -IC:/gurobi701/win32/include. The first contains the path to gurobi C++ libraries and second contains the path to header file gurobi_c++.h. I also added the path to the include file in the Includes section in Path and symbols part for GNU C++ and path to libraries to Library Paths.
Despite all these, when I compile it says undefined reference to error for all functions used from gurobi_c++.h. I can see that it detects gurobi_c++.h, but I still have those errors.
Any ideas on what may cause the problem?
contents of bin folder:
contents of lib folder:
You need to link with gurobi_c++ library file. Change EXTRA_OBJS into:
EXTRA_OBJS += -LC:/gurobi701/win32/lib -lgurobi_c++
assuming that the library is called libgurobi_c++.a or libgurobi_c++.dll.

Boost is using pthread but should use win32

I used Cmake gui and the FindBoost module to add Boost as a dependency in my visual studio 2010 c++ project. I set the parameter that tells FindBoost to use the win32 thread library instead of pthread (because I'm running on windows). When i build my code, all the calls to boost libraries compile fine, but boost itself is having a compile error. its failing on
boost\thread\pthread\mutex.hpp line 11
cannot open include file: 'pthread.h': No such file or directory
If I could make it use the win32 library, the mutex.hpp (and all the other files) dont call pthread.h
How do I build so that it uses boost\thread\win32 ?
here is the section of my cmakelists.txt
# include boost
set(BOOST_ROOT "E:/boost_1_58_0/boost_1_58_0")
add_definitions( -DBOOST_ALL_NO_LIB )
find_package(Boost COMPONENTS thread system)
if(Boost_FOUND)
message(STATUS "Boost was found, Success!")
# telling it to use win32 threads not pthreads
set(Boost_THREADAPI "win32")
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
endif()
CMake comes with a module called FindBoost which i used, and it correctly found the boost directory and set the following
Boost_INCLUDE_DIR
E:/boost_1_58_0/boost_1_58_0/
Boost_LIBRARY_DIR
E:/boost_1_58_0/boost_1_58_0/stage/lib
Boost_SYSTEM_LIBRARY_DEBUG
E:/boost_1_58_0/boost_1_58_0/stage/lib/boost_system-vc100-mt-gd-1_58.lib
Boost_SYSTEM_LIBRARY_RELEASE
E:/boost_1_58_0/boost_1_58_0/stage/lib/boost_system-vc100-mt-1_58.lib
Boost_THREAD_LIBRARY_DEBUG
E:/boost_1_58_0/boost_1_58_0/stage/lib/boost_system-vc100-mt-gd-1_58.lib
Boost_THREAD_LIBRARY_RELEASE
E:/boost_1_58_0/boost_1_58_0/stage/lib/boost_thread-vc100-mt-1_58.lib
visual studio project settings
C/C++ -> General -> Additional Include Directories:
...bunch of my other dependencies
E:\boost_1_58_0\boost_1_58_0
Linker -> General -> Additional Library Directories
E:/boost_1_58_0/boost_1_58_0/stage/lib;E:/boost_1_58_0/boost_1_58_0/stage/lib/$(Configuration);%(AdditionalLibraryDirectories)
Linker -> General -> Link Library Dependencies - No
Linker -> General -> Use LIbrary Dependency Inputs - No
Linker -> Input -> Additional Dependencies:
...bunch of my other .libs
boost_thread.lib
boost_system.lib
I fixed the error but I had try what I call the "dumb" way. The "smart" way didnt work. Smart way was reviewing several times the cmakelists code and the visual studio include library settings. i even did a clean and rebuild boost using b2 and manually set threading=multi and link=static because i thought maybe the library just wasnt built correct.
Finally i gave up and tried the dumb way: I cut and pasted the pthread folder and moved it somewhere else, so that it was no longer in the boost/thread folder, only the win32 folder.
I wish i tried it so long ago, because i didnt think that the compilation error would be due to my project's code, but rather an environment setting in the include and linking. But sure enough i got an error showing that in one of my project's files i was directly trying to include that mutex.hpp from the pthread folder. i didnt write this code (its open source) so i couldnt have exactly known, but still i want to slap myself
#include <boost/thread.hpp>
#include <boost/thread/pthread/mutex.hpp>
changing pthread to win32 made the error go away.

boost library 1.47.1 build 'lib' prefix causing LNK1104 error

I'm having difficulties generating the correct boost .lib file to compile with a VS project I've been given. It appears that after performing the complete build installation using 'b2.exe' from VS2010 command prompt I'm only able to generate the boost library files that contain the 'lib' prefix.
When I come to compile my project I'm getting the following error message:
"error LNK1104: cannot open file 'boost_signals-vc90-mt-1_47.lib'"
After going through the lib folder I can see that my boost build has only generated 'libboost_signals-vc90-mt-1_47.lib'
The boost documentation gives the following information about the lib prefix:
lib
Prefix: except on Microsoft Windows, every Boost library name begins with this string. On Windows, only ordinary static libraries use the lib prefix; import libraries and DLLs do not.
So far I've attempted the following build options for the msvc-9.0 toolset:
'build-type=complete'
'link=static,shared'
Any advice on how I may be able to generate the required .lib file would be greatly appreciated.
Many Thanks.
link=static should be used whenever you're linking to static version of boost library.
link=shared - should be used whenever you're linking dynamically to boost. It will add extra dependencies on boost dll's.
You can also use link=static,shared to build both versions - static and dynamic.
Define 'BOOST_ALL_DYN_LINK' in project controls how you link to boost.
If it's defined - it's dynamic linking, if not defined - it's static linking.

Linker error despite including the files

I am having a linker error LNK 2001 unresolved external symbol.My point is I do have the respective include directory added under Project Settings->C/C++->General ->Additional Include .I see that this include directory does have the header file which contains the function which is causing the error.
I also add all the lib folders under linker options->general->additional lib dependencies.
What could be going wrong? How can I trace what file is missing?
You need more than just the library path, you need to specify the libraries explicitly. i.e something.lib
(under Linker->Input->Additional Dependencies in VS2005)
Linker errors have nothing to do with include files. You get compiler errors from missing include files/directories. The linker needs the actual definitions aka code to work its magic and create your binary file (exe/dll/static library etc...).
Are you linking to the library of which you are #includeing the headers from?

Resources