I would like to get CMake generate Visual Studio projects in a way that PreferredToolArchitecture is set to x64, so that when building in Visual Studio, x64 compiler and linker are used. The VS project file PropertyGroup for a configuration should have the PreferredToolArchitecture XML element according to this answer, e.g. as below
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<CharacterSet>MultiByte</CharacterSet>
<PlatformToolset>v140</PlatformToolset>
<PreferredToolArchitecture>x64</PreferredToolArchitecture>
</PropertyGroup>
CMake doc indicate that passing -Tv140,host=x64 to the cmake command line should choose the x64 compiler.
However I do not see the PreferredToolArchitecture XML element in the generated project files.
How can I detect in CMakeLists.txt, that Visual Studio Clang toolset is used, as opposed to clang on Linux? When Visual Studio Clang toolset is used, I need to specify extra flags specific to Visual Studio headers (such as _CRT_SECURE_NO_WARNINGS).
Further info:
The toolset is Visual Studio Community 2019 Preview, version 16.10.0 Preview 3.0 (with C++ Clang Tools for Windows 10.0.0).
To build I am using Visual Studio Codium 1.56.2 with plugins CMake 0.0.17 and CMake Tools 1.7.2.
The VSCode CMake plugin autodetects Visual Studio Clang toolset, and when I select it, it builds with following command:
D:\dev_tools\cmake\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE "-DCMAKE_C_COMPILER:FILEPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin\clang.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\Llvm\x64\bin\clang.exe" -Hd:/dev/exemine -Bd:/dev/exemine/build -G Ninja
The build works correctly. I just need to detect when this toolset is used from CMakeLists.txt and specify few extra options.
Note: As a workaround, I am using following code:
if(MSVC OR (CMAKE_CXX_COMPILER MATCHES "clang.exe$"))
add_compile_definitions(WIN32)
...
The official LLVM 4.0 build for Windows integrates with Visual Studio up to Visual Studio 2015. Unfortunately it still doesn't support Visual Studio 2017.
When you try to set the Platform Toolset of a project to LLVM-vs2014, it pops up an error.
Do you know any way to make it work?
Update
In 2018, LLVM 6.0 officially still doesn't support integration with Visual Studio 2017 (version 15.X.X), only with the Visual Studio 2015 (version 14.X.X) toolset.
It requires some msbuild targets that only ship with the C++ v140 toolset, and VS 2017 only installs the v141 toolset by default. If you open the VS 2017 installer, find the checkbox for the v140 toolset and install that then the right C++ msbuild targets will be available and the thing will work.
Finally, I found a brilliant GitHub repo with the required MSBuild platform toolsets which integrates LLVM clang 5.0.0 into Visual Studio 2017. After following the instructions of the README file, you will have two new platform toolsets LLVM-vs2017 and LLVM-vs2017_xp. Problem solved.
Update
I made a fork which is updated for LLVM 6.0.0 and provides better integration by providing include and library paths of LLVM/clang.
Thanks to Royi, who realized that the original .prop files are explicitly tailored for LLVM 5.0 and it misses adding the proper lib (
$(LLVMInstallDir)\lib) and include ($(LLVMInstallDir)\lib\clang\6.0.0\include) folders.
The LLVM project now explicitly supports Visual Studio 2017 via https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.llvm-toolchain
I'm a newbie with the LLVM technology and I'm using a Visual Studio extension, Clang Power Tools. They have a settings page from where you can install LLVM (all versions >= 4.0). After that you are free to apply clang compile or tidy with code modernization(this is what I appreciate the most) by using the extension buttons from VS toolbar. In this way you don't need to configure anything.
Update
Open the extension settings and select the LLVM page from the top. On the LLVM page, you'll see all the supported LLVM versions and at the right of each version the install button. Install any version you need. On the bottom of the page is a dropdown that allows you to select what version to use in case you installed multiple versions.
The feature is explained step by step in this blog post
LLVM/Clang now has an updated patch that allows you to use it with VS2017. But they still don't directly support VS2017. I asked on the LLVM developer mailing list for them to update their support for VS2017, so hopefully they'll do it. If they listen to what I said.
I have figured out how to integrate LLVM Clang 7.0 with Visual Studio 2017 update 15.5.6. v1913 with full support for PDB based debugging using the latest LLVM builds.
i.e., lld-link /DEBUG:GHASH; clang-cl -mllvm -emit-codeview-ghash-section flag to clang-cl.
It is a three step process.
Install latest llvm
Update the toolset.props, toolset.targets in VS to support latest clang
Select the new toolset to use for building your C/C++ or other lang project
As of Visual Studio 2017 update 15.4.5 the Microsoft "experimental" Clang C2 no longer works. Thus, the above fixes are necessary to use clang to compile code that has full PDB (not just CodeView /Z7 limited) debuggability. This also now becomes a more complete suite for portability testing cross-platform builds since you can build and PDB debug for windows using all LLVM components from the clang compiler front end to the LLVM codegen backend and LLVM linker.
Cheers, David
Check out January 09, 2018 http://planet.clang.org/
Look at the "Try it out!" section:
If you're already using clang-cl and lld-link on Windows today, you can try this out. There are two flags needed to enable this, one for the compiler and one for the linker:
To enable the emission of a .debug$H section by the compiler, you will need to pass the undocumented -mllvm -emit-codeview-ghash-section flag to clang-cl (this flag should go away in the future, once this is considered stable and good enough to be turned on by default).
To tell lld-link to use this information, you will need to pass the /DEBUG:GHASH to lld.
You just need to pass the -mllvm -emit-codeview-ghash-section flags in either your c++ projects "Command Line:Additional Options" area, or place them directly in the "toolset.props" file that you created in C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Platforms\Win32\PlatformToolsets\LLVM-vs2017 or
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Platforms\x64\PlatformToolsets\LLVM-vs2017.
The key is that in adding those cli options you're telling clang to emit debug information that the lld (aka lld-link) will understand and use to produce fully populated PDB files. Not the limited ones it made prior to the Jan 09, 2018 drops of LLVM 7.0.
toolset.targets: (any version)
<Project ToolsVersion="14.1"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(VCTargetsPath)\Microsoft.CppCommon.targets" />
</Project>
toolset.props: (Win32 version)
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(VCTargetsPath)\Platforms\$(Platform)\PlatformToolsets\v141\Microsoft.Cpp.$(Platform).v141.props" Condition="Exists('$(VCTargetsPath)\Platforms\$(Platform)\PlatformToolsets\v141\Microsoft.Cpp.$(Platform).v141.props')"/>
<Import Project="$(VCTargetsPath)\Platforms\$(Platform)\PlatformToolsets\v141\Toolset.props" Condition="Exists('$(VCTargetsPath)\Platforms\$(Platform)\PlatformToolsets\v141\Toolset.props')"/>
<PropertyGroup>
<LLVMInstallDir>$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\LLVM\LLVM)</LLVMInstallDir>
<LLVMInstallDir Condition="'$(LLVMInstallDir)' == ''">$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\LLVM\LLVM)</LLVMInstallDir>
<ExecutablePath>$(LLVMInstallDir)\msbuild-bin;$(ExecutablePath)</ExecutablePath>
<LibraryPath>$(LLVMInstallDir)\lib\clang\7.0\lib\windows;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<!-- remove the implicit vcxxx.pdb path to avoid rebuilds every time as clang-cl only supports /Z7 -->
<ProgramDataBaseFileName></ProgramDataBaseFileName>
<!-- Set the value of _MSC_VER to claim for compatibility -->
<AdditionalOptions>-m32 -fmsc-version=1913 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
</ItemDefinitionGroup>
</Project>
For x64, change -m32 to -m64
p.p.s., I have also enabled Microsofts ARM and ARM64 compilers for building native Windows-10-ARM apps (not UWP modern-com-junk). But, as yet, I have not done enough digging through the clang sources to properly configure something similar for ARM to what -m32 and -m64 do for Intel code-gen.
See these articles:
http://pete.akeo.ie/2017/05/compiling-desktop-arm-applications-with.html
https://www.theverge.com/2017/12/5/16737288/microsoft-windows-10-qualcomm-arm-laptops-launch
https://wiki.winehq.org/ARM
I'm compiling Berkley's Caffe to a static library, MT, MSVC 12 (VS2013) and linking it to a dll.
It works fine.
Now I'm trying to compile it with Intel C++ 2015 compiler - the static lib build fine, but the dependent dll fails with the following linker error:
fatal error LNK1104: cannot open file
'libboost_thread-iw-mt-s-1_58.lib'
There isn't such library in my Boost build indeed, but I don't know where this dependency came from. Except for the compiler I didn't change any other setting, and I can't find that Boost lib in either Caffe of my dll's Linker->Input->Additional dependencies.
How can I fix this?
Thanks!
Found the solution here: https://software.intel.com/en-us/articles/intel-c-compiler-for-windows-fatal-link-error-lnk1104-when-using-intel-c-compiler-with-boost-libraries
Copied:
When building an application that uses the Boost libraries with the
Intel® C++ Compiler, you may get linker errors like the ones shown
below due to incorrect libraries being linked to the application:
fatal error LNK1104: cannot open file
'libboost_thread-iw-mt-1_33_1.lib' fatal error LNK1104: cannot open
file 'libboost_thread-iw-1_33_1.lib' ... The root cause is missing
Boost libraries for the Intel® C++ Compiler.
The preferred solution is to recompile all required Boost libraries
with the Intel® C++ Compiler (libraries with the infix "iw" are
created because of this). However, this is not mandatory. The
libraries provided for the different Microsoft Visual Studio* versions
are safe to use as well. Perform the following steps to use them
instead:
Open the Boost configuration file "auto_link.hpp". Search for 1
elif defined(__ICL) 2 3 4
// Intel C++, no version number: 5 6
# define BOOST_LIB_TOOLSET "iw" Change "iw" depending on which Microsoft Visual Studio version you're using:
"vc71": Microsoft Visual Studio .NET 2003 "vc80": Microsoft Visual
Studio 2005 "vc90": Microsoft Visual Studio 2008 "vc100": Microsoft
Visual Studio 2010 "vc110": Microsoft Visual Studio 2012 "vc120":
Microsoft Visual Studio 2013 "vc140": Microsoft Visual Studio 2015
Rebuild your application to resolve the linker errors.
Until yesterday I used Visual Studio 2008 and CMake (in combination with CPack) to build my project under Windows - that worked fine. But now I switched to the 2010 edition and ran into a (an old) problem: The loved background console (Because Windows thinks we have a fancy console application):
I have a CMake decision to avoid this window:
if(MSVC)
target_link_libraries(client window core ${QT_QTMAIN_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY})
set_target_properties(client PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
else()
target_link_libraries(client window core ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY})
endif()
This works for Visual Studio 2008, but not for 2010 (The /SUBSYSTEM is useless). So my question is: Does anyone have experience with this or solved it in CMake ? I don't want to change my main function to WinMain (Have the same codebase for Unix/Linux/OS X and Windows) or change the SUBSYSTEM settings in Visual Studio (That's not the idea behind CMake)
So after a cup of coffee I came to the following solution. Debug and Release build don't open a background console. Take a look at the WIN32 tag and the LINK_FLAG for Debug/Release/RelWithDebugInfo:
if(MSVC)
add_executable(client WIN32 ${SRC_CLIENT} ${HDR_UI_CLIENT} ${HDR_RSC_CLIENT})
target_link_libraries(client server ${QT_QTMAIN_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY})
set_target_properties(client PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
set_target_properties(client PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS")
set_target_properties(client PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS")
set_target_properties(client PROPERTIES RELWITHDEBINFO "/SUBSYSTEM:WINDOWS")
set_target_properties(client PROPERTIES MINSIZEREL "/SUBSYSTEM:WINDOWS")
else()
add_executable(client ${SRC_CLIENT} ${HDR_UI_CLIENT} ${HDR_RSC_CLIENT})
target_link_libraries(client server ${QT_QTGUI_LIBRARY} ${QT_QTCORE_LIBRARY})
endif()
If you use a modern version of CMake (2.8.11 or later), the ${QT_QTMAIN_LIBRARY} library will automatically be linked in for WIN32 executables, and not otherwise, if you use the IMPORTED targets.
http://www.cmake.org/cmake/help/git-master/module/FindQt4.html
You should not need to add the /subsystem yourself at all. That's what WIN32 does. If you can produce a minimal testcase, it's a bug.