vcpkg icon indicating copy to clipboard operation
vcpkg copied to clipboard

How to compile vcpkg libraries using Clang?

Open BiliBiliFox opened this issue 10 months ago • 6 comments

Environment

  • OS: Windows 10
  • Compiler: Clang

Dear vcpkg team,

I am currently using vcpkg to manage dependencies for my project, and I must say it is an incredibly useful tool that has greatly enhanced my productivity. However, I have encountered an issue with compiler compatibility that I hope you can assist me with.

Issue Description:

My project requires compilation with the Clang compiler due to bugs I have encountered with MSVC, which prevent successful compilation. When I switch to using Clang, the compilation proceeds without issues. This has led me to conclude that the bugs are indeed with MSVC.

The problem arises when I attempt to link my project. Since my project is compiled with Clang, and the libraries installed via vcpkg are compiled with MSVC by default, I am experiencing ABI incompatibility issues.

My question is: Is there a way to configure vcpkg to use the Clang compiler for building libraries? This would ensure that both my project and the dependencies are compiled with the same compiler, thus avoiding ABI compatibility issues.

If this is not currently a supported feature, I would like to suggest considering adding support for the Clang compiler. This would make vcpkg more versatile and accommodating to a wider range of project requirements.

Thank you very much for your time and assistance!

Feel free to modify and expand on this draft based on your specific situation. Good luck with your issue!

BiliBiliFox avatar Apr 08 '24 09:04 BiliBiliFox

Not part of the vcpkg team, but saw your question. You need to create a toolchain file and triplet that point to clang. You can look at the community ones as a template. I'm successfully using it for clang currently although its officially (as far as I can tell) even less supported than the community triplets, so YMMV with how successful you are. You're going to need to manually set CXX and CC in there for clang also, or some packages will fail during configuration.

diehard2 avatar Apr 08 '24 17:04 diehard2

See: https://github.com/Neumann-A/my-vcpkg-triplets/blob/master/x64-win-llvm.cmake for an example how to use clang-cl. You can probably adjust use clang instead.

Neumann-A avatar Apr 08 '24 18:04 Neumann-A

I would like to share what I have done.

  • Mare sure clang-cl.exe is installed. in my case, it's at C:\Program Files\LLVM\bin\clang-cl.exe

  • Download the overlay-triplets as motioned above, in my case, to D:\github\my-vcpkg-triplets-master

  • Use the triplets

D:\github\vcpkg> .\vcpkg.exe install libiconv --overlay-triplets=D:\github\my-vcpkg-triplets-master --triplet=x64-win-llvm

felixf4xu avatar Apr 27 '24 05:04 felixf4xu

.\vcpkg.exe install libiconv --overlay-triplets=D:\github\my-vcpkg-triplets-master --triplet=x64-win-llvm

better use

.\vcpkg.exe install libiconv --overlay-triplets=D:\github\my-vcpkg-triplets-master --host-triplet=x64-win-llvm

Neumann-A avatar Apr 27 '24 05:04 Neumann-A

I recognize your environment is windows, but these may be gotchas for you also. I've found I needed to add

set(ENV{CC} "/usr/bin/clang") set(ENV{CXX} "/usr/bin/clang++")

on Linux, since there's a subset of ports that depend on those environmental variables to build.


For anyone else who comes across this, there was one more gotcha for LLVM on Linux

Depending on how clang was installed, I've also had to manually set the linker in toolchains.

set(CMAKE_LINKER "/opt/rh/gcc-toolset-13/root/usr/bin/g++") set(CMAKE_C_COMPILER_LINKER "/opt/rh/gcc-toolset-13/root/usr/bin/ld") set(CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

This is needed if you're on a redhat based system (centos/rocky/alma/rhel) and you're using a toolkit. Clang will use the highest level toolkit you have for everything except the linker. That defaults to /usr/bin/ld and you can get binutils version conflicts.

diehard2 avatar Apr 27 '24 15:04 diehard2

set(ENV{CC} "/usr/bin/clang") set(ENV{CXX} "/usr/bin/clang++")

on Linux, since there's a subset of ports that depend on those environmental variables to build.

You should name these ports, to get them fixed. While CC and CXX are expected to (usually) have effect on CMake (i.e. input), non-CMake build systems are expected to follow the result of CMake toolchain configuration (i.e. output). With vcpkg and CMake there can be a custom chainloaded toolchain file which sets CMAKE_<LANG>_COMPILER directly, Then CC and CXX must be overridden before running other build systems.

dg0yt avatar Apr 29 '24 05:04 dg0yt

We hope your question was answered to your satisfaction; if it wasn't, you can reopen with more info.

MonicaLiu0311 avatar May 22 '24 02:05 MonicaLiu0311