linker error using qmake with boost thread in windows MSVC - windows

I am using QtCreator on Windows with boost library. right now I am stuck with linking boost threading library against my Application.
In my C:\boost_1_47_0\bin.v2\libs\thread\build directory I see only msvc-10.0 no mingw So I need to choose MSVC2010 as My build configuration instead of mingw.
on compilation its reporting error: LNK1104: cannot open file 'libboost_thread-vc100-mt-gd-1_47.lib'
I can find There is boost_thread-vc100-mt-gd-1_47.lib here in libs directory but no libboost_thread-vc100-mt-gd-1_47.lib (check the extra lib on left)
in my .pro
win32:INCLUDEPATH += C:\boost\include\boost-1_47
win32:CONFIG(debug): LIBS += C:\boost_1_47_0\bin.v2\libs\thread\build\msvc-10.0\debug\threading-multi\boost_thread-vc100-mt-gd-1_47.lib

Related

linking boost on Windows asks for versioned lib

We have a large cmake based C++ project for Linux where we build boost ourselves via cmake exernal project.
The project used to build also on Windows for the classical Intel compiler. But I have no access to this old running configuration.
I use Intel oneAPI 2023.0 with LLVM based icx compiler (clang 15?) and a current MSVC community edition.
I built boost (1.81.0) without a target (as I still struggle with openAPI target) with --layout=system, hence lib names like libboost_atomic.a
In our C++ we do not let CMake search for boost but add the boost libs via target_link_libraries().
When I compile our application with icx I get a link error
Linking CXX executable ....\bin\cfs.exe LINK: command
"C:\PROGRA~2\Intel\oneAPI\compiler\latest\windows\bin\icx.exe /nologo
#CMakeFiles\cfs.dir\objects1 /Qoption,link, /machine:x64
/INCREMENTAL:NO /Qoption,link,/subsystem:console -Qiopenmp
/Qoption,link,/LIBPATH:C:\PROGRA~2\Intel\oneAPI\compile
r\latest\windows\bin\intel64\ifort.exe
/Qoption,link,/LIBPATH:C:\Users\fabia\code\master\release_icx\lib
[....] ....\lib\libboost_log_setup.lib
....\lib\libboost_serialization.lib [.....] /link
/out:....\bin\cfs.exe /implib:. ...\bin\cfs.lib
/pdb:C:\Users\fabia\code\master\release_icx\bin\cfs.pdb /version:0.0
/MANIFEST /MANIFESTFILE:....\bin\cfs.exe .manifest"
I get
fatal error LNK1104: file
"libboost_serialization-clangw16-mt-x64-1_81.lib" not found.
I have no idea where the string clangw16-mt-x64-1_81 comes from.
You can link the libraries present in the boost library path to your project either from the Command Prompt or within the Visual Studio IDE. Please refer to the below link for more details.
https://www.boost.org/doc/libs/1_81_0/more/getting_started/windows.html
If you do not find any binaries, you can add the macro BOOST_ALL_NO_LIB which tells the config system not to automatically select which libraries to link. Please refer to the below link for more details.
https://www.boost.org/doc/libs/1_81_0/libs/config/doc/html/index.html
Or
You can use the target Boost::disable_autolinking to disable automatic linking. Please refer to the below link for more details.
https://cmake.org/cmake/help/latest/module/FindBoost.html

How to prevent CMake from installing .lib files

I am cross-compiling a JNI DLL on Windows using MSVC + CMake and I noticed that it is producing a .lib file on install. I am using add_library(mylibname SHARED ...) and install(TARGETS mylibname DESTINATION ${CMAKE_INSTALL_LIBDIR}) to create and install the shared library. Is there a way to prevent this file from being installed as it is not needed for JNI libraries at runtime?

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)

Qbs does not add Qt libraries when linking application

I installed VS2015 and QtCreator 3.5 and compiled Qt5.5 from source as a binary distribution is not available for the visual c++ compiler shipped with VS2015.
I tried to compile my project which was developed using the visual c++ compilers from VS2012/VS2013 and QtCreator 3.3x with Qt 5.2 and Qbs - on a different machine - from QtCreator and with my new setup neither QtCreator nor Qbs from command line are able to link the application.
I narrowed the problem down using the Qbs helloworld example
import qbs
CppApplication {
type: "application" // To suppress bundle generation on Mac
consoleApplication: true
files: "main.cpp"
Depends {
name: "Qt"
submodules: [
"core"
]
}
}
Which gives the following output when building with qbs from the command line
compiling main.cpp
linking halloqbswelt.exe
ERROR: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64\link.exe /nologo /DEBUG /PDB:halloqbswelt.pdb /OSVERSION:5.02 /SUBSYSTEM:CONSOLE,5.02 /MANIFEST /MANIFESTFILE:D:\projekte\halloqbswelt\Qt5-debug\halloqbswelt.Qt5.842abbdf\intermediate.halloqbswelt.exe.manifest D:\projekte\halloqbswelt\Qt5-debug\halloqbswelt.Qt5.842abbdf\.obj\3a52ce780950d4d9\main.cpp.obj .lib /OUT:D:\projekte\halloqbswelt\Qt5-debug\halloqbswelt.Qt5.842abbdf\intermediate.halloqbswelt.exe /LIBPATH:D:\projekte\3rd_party\qt\qtbase\lib
LINK : fatal error LNK1104: cannot open file '.lib'
ERROR: Process failed with exit code 1104.
The following products could not be built for configuration Qt5-debug:
halloqbswelt
Rather cryptic so I'll highlight the problem from the above output
[...] .obj\3a52ce780950d4d9\main.cpp.obj .lib /OUT: [...]
So the empty .lib file is the problem.
Comparing the linker statement with that from the other machine, the empty .lib is at the same position where Qt5Core.lib should be.
Thus tried to investigate why Qbs does not resolve the paths proberly; I looked at the core.qbs module but couldn't find anything different from the file on the machine with the working setup.
Finally I decided to recompile the Qt library and setup the Qbs/QtCreator profiles from scratch. Done and it worked, until I restarted QtCreator, now neither (Qbs from command line, nor QtCreator with its own profile) works anymore.
Has somebody experienced such a behavior?
As said in the comment, http://article.gmane.org/gmane.comp.lib.qt.qbs/682 gave me the right starting point.
Qbs relies on pri/prl files of each submodule from which the dependencies are built (as far as I understood). These pri files are overwritten or some other bad stuff happens when installing Qt built from source in a non-prefix environment (which is the default under Windows) (https://bugreports.qt.io/browse/QTBUG-42959).
So building Qt with a prefix and installing the sources is solves the problem, at least for me.

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.

Resources