Cannot link to Boost library with MSVC: Cannot open file *.lib - c++

I'm using CLion and CMake to link with Boost and as toolchain I chose the MSVC compiler:
My architecture is configured as amd64 since I'm running a 64-bit system (the default of x86 only tries to find the 32-bit versions of Boost which I don't want).
Furthermore I compiled the 64-bit libraries using this guide. I setup my CMake file respectively:
set(BOOST_ROOT "C:/local/boost_1_69_0_b1_rc3")
set(BOOST_LIBRARYDIR "C:/local/boost_1_69_0_b1_rc3/stage/x64/lib")
set(BOOST_INCLUDEDIR "C:/local/boost_1_69_0_b1_rc3/boost")
My main.cpp compiles just fine but I'm getting a linker error:
====================[ Build | BoostTesting | Debug ]============================
C:\Users\User\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\183.4284.104\bin\cmake\win\bin\cmake.exe --build D:\Cpp\BoostTesting\cmake-build-debug --target BoostTesting --
[ 50%] Linking CXX executable BoostTesting.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~3\2017\ENTERP~1\VC\Tools\MSVC\1415~1.267\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\BoostTesting.dir\objects1.rsp /out:BoostTesting.exe /implib:BoostTesting.lib /pdb:D:\Cpp\BoostTesting\cmake-build-debug\BoostTesting.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\local\boost_1_69_0_b1_rc3\stage\x64\lib\libboost_filesystem-vc141-mt-gd-x64-1_69.lib C:\local\boost_1_69_0_b1_rc3\stage\x64\lib\libboost_system-vc141-mt-x64-1_69.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\BoostTesting.dir/intermediate.manifest CMakeFiles\BoostTesting.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'libboost_iostreams-vc141-mt-gd-x64-1_69.lib'
NMAKE : fatal error U1077: 'C:\Users\User\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\183.4284.104\bin\cmake\win\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
When using x86_amd64 as architecture it also fails to link. The other architectures don't make sense for my PC:
Using the pre-compiled binaries from here also fails to link. How to fix this linking problem?

Just setting BOOST_ROOT in CMakeLists.txt
set(BOOST_ROOT "C:/local/boost_1_69_0_b1_rc3")
and using amd64 as the architecture surprisingly did the trick now. Using MSVC with CMake is better supported with Visual Studio (quite obviously) and linking to Boost was no issue anymore. Now the build also works from CLion but Visual Studio should be preferred until CLion maybe gets better MSVC integration.

Related

Visual Studio with CMake stopped working. How do I completely reset to a working state?

I was working on a C++ CMake project which was displaying and building fine in Visual Studio. Then I did something, I'm not sure what I did, but it seemed to break CMake. Now I get the following error:
Error CMake Error at C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.24/Modules/CMakeTestCCompiler.cmake:69 (message):
The C compiler
"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: E:/DOCS/PROGRAMMING/22-02_Qt/vs_simple/VsConsole5/out/build/x64-debug/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe cmTC_c94d5 && [1/2] Building C object CMakeFiles\cmTC_c94d5.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_c94d5.exe
FAILED: cmTC_c94d5.exe
cmd.exe /C "cd . && "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_c94d5.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests -- C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1434~1.319\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_c94d5.dir\testCCompiler.c.obj /out:cmTC_c94d5.exe /implib:cmTC_c94d5.lib /pdb:cmTC_c94d5.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~1\MIB055~1\2022\COMMUN~1\VC\Tools\MSVC\1434~1.319\bin\Hostx64\x64\link.exe /nologo CMakeFiles\cmTC_c94d5.dir\testCCompiler.c.obj /out:cmTC_c94d5.exe /implib:cmTC_c94d5.lib /pdb:cmTC_c94d5.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_c94d5.dir/intermediate.manifest CMakeFiles\cmTC_c94d5.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project. C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.24/Modules/CMakeTestCCompiler.cmake 69
I get this error, even if I make a completely fresh CMake project via File > New > Project > CMake Project from within Visual Studio.
I've tried uninstalling and re-installing:
Visual Studio
Multiple different Windows SDK versions
CMake
I was hoping that doing this would return Visual Studio + CMake to a working state again but I am still getting the same error.
I also tried manually setting LIB and INCLUDE environment variables to the appropriate directories and that did get rid of the error but then the program wouldn't build, I got a bunch of errors like this:
Error (active) E1696 cannot open source file "ctype.h" VsSimple.exe - x64-Debug C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\include\cctype
I also tried deleting everything in the project folder apart from CMakeLists.txt, the .cpp file and the .h file. Unfortunately still no luck.

Qt in Clion 2020.3

I was trying the new fearture in CLion 2020.3 - creating Qt Widgets Application. When I tryed to build a simple program that CLion offered me I got this error:
====================[ Build | untitled | Debug ]================================
"C:\Program Files\JetBrains\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" --build "C:\Users\vladi\CLionProjects\Second sem\untitled\cmake-build-debug" --target untitled
[ 25%] Automatic MOC and UIC for target untitled
[ 25%] Built target untitled_autogen
[ 50%] Linking CXX executable untitled.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx86\x86\link.exe /nologo #CMakeFiles\untitled.dir\objects1.rsp /out:untitled.exe /implib:untitled.lib /pdb:C:\Users\vladi\CLionProjects\Second sem\untitled\cmake-build-debug\untitled.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console D:\Qt\5.15.2\msvc2019_64\lib\Qt5Widgetsd.lib D:\Qt\5.15.2\msvc2019_64\lib\Qt5Guid.lib D:\Qt\5.15.2\msvc2019_64\lib\Qt5Cored.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\untitled.dir/intermediate.manifest CMakeFiles\untitled.dir/manifest.res" failed (exit code 0) with the following output:
ЌҐ г¤ Ґвбп ­ ©вЁ гЄ § ­­л© д ©«NMAKE : fatal error U1077: "C:\Program Files\JetBrains\CLion 2020.3.2\bin\cmake\win\bin\cmake.exe" : возвращенный код "0xffffffff"
Stop.
NMAKE : fatal error U1077: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x86\nmake.exe" : возвращенный код "0x2"
Stop.
NMAKE : fatal error U1077: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x86\nmake.exe" : возвращенный код "0x2"
Stop.
NMAKE : fatal error U1077: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX86\x86\nmake.exe" : возвращенный код "0x2"
Stop.
Has no idea what is wrong

CUDA C++ problems with CMake on CLion IDE

I'm trying to start a project with CUDA C++ using CLion as an IDE.
I installed a fresh version of CUDA Developer Tools (v10.2) and tried to load the changes of the MakeFile.
Apparently, the CUDA compiler (nvcc) doesn't get properly linked, but I'm not sure this is the problem.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.15)
project(test_cuda LANGUAGES CXX CUDA)
find_package(CUDA)
set(CMAKE_CXX_STANDARD 17)
add_executable(test_cuda main.cu)
The result I get is the following:
"E:\Program Files\JetBrains\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - NMake Makefiles" <path_to_project>\test_cuda
-- The CUDA compiler identification is NVIDIA 10.2.89
-- Check for working CUDA compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/bin/nvcc.exe
-- Check for working CUDA compiler: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/bin/nvcc.exe -- broken
CMake Error at E:/Program Files/JetBrains/CLion 2019.2.5/bin/cmake/win/share/cmake-3.15/Modules/CMakeTestCUDACompiler.cmake:46 (message):
The CUDA compiler
"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/bin/nvcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: <path_to_project>/test_cuda/cmake-build-debug-visual-studio/CMakeFiles/CMakeTmp
Run Build Command(s):nmake /nologo cmTC_5cf15\fast && "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe" -f CMakeFiles\cmTC_5cf15.dir\build.make /nologo -L CMakeFiles\cmTC_5cf15.dir\build
Building CUDA object CMakeFiles/cmTC_5cf15.dir/main.cu.obj
C:\PROGRA~1\NVIDIA~2\CUDA\v10.2\bin\nvcc.exe -D_WINDOWS -Xcompiler=" /GR /EHsc" -Xcompiler="-Zi -Ob0 -Od /RTC1" -Xcompiler=-MDd -x cu -c <path_to_project>\test_cuda\cmake-build-debug-visual-studio\CMakeFiles\CMakeTmp\main.cu -o CMakeFiles\cmTC_5cf15.dir\main.cu.obj -Xcompiler=-FdCMakeFiles\cmTC_5cf15.dir\,-FS
main.cu
Linking CUDA executable cmTC_5cf15.exe
"E:\Program Files\JetBrains\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_5cf15.dir --rc=C:\PROGRA~2\WI3CF2~1\8.1\bin\x86\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\8.1\bin\x86\mt.exe --manifests -- C:\PROGRA~2\MICROS~1.0\VC\bin\link.exe /nologo "CMakeFiles\cmTC_5cf15.dir\main.cu.obj" #<path_to_appdata>\Local\Temp\nm8647.tmp
LINK Pass 1: command "C:\PROGRA~2\MICROS~1.0\VC\bin\link.exe /nologo CMakeFiles\cmTC_5cf15.dir\main.cu.obj /out:cmTC_5cf15.exe /implib:cmTC_5cf15.lib /pdb:<path_to_project>\test_cuda\cmake-build-debug-visual-studio\CMakeFiles\CMakeTmp\cmTC_5cf15.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib -LIBPATH:C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64 cudadevrt.lib cudart_static.lib /MANIFEST /MANIFESTFILE:CMakeFiles\cmTC_5cf15.dir/intermediate.manifest CMakeFiles\cmTC_5cf15.dir/manifest.res" failed (exit code 1112) with the following output:
CMakeFiles\cmTC_5cf15.dir\main.cu.obj : fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
NMAKE : fatal error U1077: '"E:\Program Files\JetBrains\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "<path_to_project>/test_cuda/cmake-build-debug-visual-studio/CMakeFiles/CMakeOutput.log".
See also "<path_to_project>/test_cuda/cmake-build-debug-visual-studio/CMakeFiles/CMakeError.log".
[Finished]
Where I substitute the path to the project folder with <path_to_project>.
The .cu file is just a simple hello world source file for now, since I'm trying to solve the issue with the CUDA compiler first.
I can't really find any way to solve this.
Thank you a lot in advance.
The message module machine type 'x64' conflicts with target machine type 'X86' is the crux of the problem. This indicates you have a 32-bit vs 64-bit architecture mismatch. Please be sure you use consistent project and compiler settings.
So, if you are compiling for a 64-bit architecture, make sure your project (CMake) settings are configured to build x64 targets. Also, you must be sure to use the 64-bit versions of the compilation tools (nvcc). Specifically for your case, your linker flags appear to contain /machine:X86, which would need to be removed if you are aiming for a 64-bit build. This can likely be configured through the CLion IDE in your Toolchain settings.

Visual Studio can't detect my Clang compiler installation

I'm using the latest VS 2019 16.2, which is supposed to have Clang with MSBuild support as well Clang w/ CMake of course.
I installed the CMake components as well as the Clang-cl for v142 build tools (But not the Clang Compiler component that comes with VS!) because I already have Clang compiler v8.0.0 installed in the default path in my system.
I want to use Clang (either through MSBuild or CMake) but both approaches failed.
VS simply doesn't seem to detect my compiler (although the version is supported, supposedly).
Is there a way I can manually make it detect it? May add a path in some options or reinstall clang?
I don't want to install the VS one for my specific reasons.
Severity Code Description Project File Line Suppression State
Error CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"c:/program files (x86)/microsoft visual studio/2019/community/VC/Tools/Llvm/8.0.0/bin/clang-cl.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/HP/Source/Repos/CMakeProject1/out/build/x64-Clang-Debug/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/Ninja/ninja.exe cmTC_8f083
[1/2] Building C object CMakeFiles\cmTC_8f083.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTC_8f083.exe
FAILED: cmTC_8f083.exe
cmd.exe /C "cd . && "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_8f083.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1422~1.279\bin\Hostx86\x86\link.exe /nologo CMakeFiles\cmTC_8f083.dir\testCCompiler.c.obj /out:cmTC_8f083.exe /implib:cmTC_8f083.lib /pdb:cmTC_8f083.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
RC Pass 1: command "rc /foCMakeFiles\cmTC_8f083.dir/manifest.res CMakeFiles\cmTC_8f083.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specified
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project. C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake 60

CMake generated program fails to link on Windows: tries to link to non-existent file

I am trying to compile a very simple test program on Windows and keep getting linker errors. The program to link is the following:
#include <boost/asio/io_context.hpp>
int main()
{
boost::asio::io_context context;
}
While the CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(windows-test)
SET(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.6.7 COMPONENTS system)
include_directories("${Boost_INCLUDE_DIRS}")
add_executable(windows-test main.cpp)
target_link_libraries(windows-test Boost::system)
When building this using nmake, it fails with the following output:
-- Boost version: 1.67.0
-- Found the following Boost libraries:
-- system
-- Configuring done
-- Generating done
-- Build files have been written to: Z:/windows-test/build
[ 50%] Linking CXX executable windows-test.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2017\BUILDT~1\VC\Tools\MSVC\1414~1.264\bin\Hostx64\x64\link.exe /nologo #CMakeFiles\windows-test.dir\objects1.rsp /out:windows-test.exe /implib:windows-test.lib /pdb:Z:\windows-test\build\windows-test.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console C:\local\boost_1_67_0\lib64-msvc-14.1\boost_system-vc141-mt-gd-x64-1_67.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\windows-test.dir/intermediate.manifest CMakeFiles\windows-test.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: cannot open file 'libboost_system-vc141-mt-gd-x64-1_67.lib'
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.14.26428\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
The file 'libboost_system-vc141-mt-gd-x64-1_67.lib' indeed does not exist on the system, but I don't know where it is coming from, since it does not appear on the linker command that it is executing. The linker command shows the file C:\local\boost_1_67_0\lib64-msvc-14.1\boost_system-vc141-mt-gd-x64-1_67.lib, which does exist.
Why oh why does it try to link to a missing file, that appears nowhere on the linker command? I feel way out of my depth here, since I haven't had to use Windows for almost 20 years and never before had to port to it.
Boost headers contain linker commands on Windows, so that Boost libraries are linked automatically when including the appropriate header. However, it seems your setup uses a different naming scheme for the libraries, which makes these fail to link.
You can disable the Boost auto-linking feature by defining the preprocessor macro BOOST_ALL_NO_LIB. Like this:
target_compile_definitions(windows-test PRIVATE BOOST_ALL_NO_LIB)