【问题标题】:Clang without GCC or MSVC没有 GCC 或 MSVC 的 Clang
【发布时间】:2019-06-05 21:44:23
【问题描述】:

我想在 Linux 上使用不带 GCC 而在 Windows 上不带 MSVC 的 clang。我的理解是,这在过去是不可能的,原因是:

  1. LLVM 链接器不可用。
  2. 兼容的 STL 不可用。

但是,我的印象是 v8.0.0 应该可以实现这一点,因为现在有一个 viable linker 和一个兼容的 STL library

不幸的是,在执行此方案时,我在 Linux 和 Windows 上遇到了不同的错误(请参阅下面的更多信息)。

你能帮我解决这个问题吗?不幸的是,我发现通过各种搜索找到明确的答案有点困难。

感谢您的帮助!

测试代码:test.cpp

#include <vector>
#include <iostream>

int main(void) {
    std::vector<int> const v{ 1, 2, 3 };

    std::cout << "Hello world\n";
    std::cout << v[0] << "\n";
    return 0;
}

Linux 输出

以下输出来自适用于 Linux 的 Windows 子系统,但在正确安装 Linux 时行为是相同的。

clang -v /mnt/c/Temp/test.cpp -I/mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/include/c++/v1
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8.0.1
 "/mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/bin/clang-8" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -v -resource-dir /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/lib/clang/8.0.0 -I /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/lib/clang/8.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /mnt/c/Code/v3/Common/cpp/Clang/8 -ferror-limit 19 -fmessage-length 200 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/test-284194.o -x c++ /mnt/c/Temp/test.cpp -faddrsig
clang -cc1 version 8.0.0 based upon LLVM 8.0.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/include/c++/v1
 /usr/local/include
 /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/lib/clang/8.0.0/include
 /usr/include
End of search list.
In file included from /mnt/c/Temp/test.cpp:1:
In file included from /mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/include/c++/v1/vector:273:
/mnt/c/Code/v3/Common/cpp/Clang/8/Tools/Clang/v8.0.0/Ubuntu/wsl/include/c++/v1/__config:206:12: fatal error: 'features.h' file not found
#  include <features.h>
           ^~~~~~~~~~~~
1 error generated.

Windows 输出

clang -v c:\temp\test.cpp
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\bin
clang: warning: unable to find a Visual Studio installation; try running Clang from a developer command prompt [-Wmsvc-not-found]
 "C:\\Code\\v3\\Common\\cpp\\Clang\\8\\Tools\\Clang\\v8.0.0\\Windows\\DefaultEnv\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-msvc19.11.0 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -momit-leaf-frame-pointer -v -resource-dir "C:\\Code\\v3\\Common\\cpp\\Clang\\8\\Tools\\Clang\\v8.0.0\\Windows\\DefaultEnv\\lib\\clang\\8.0.0" -internal-isystem "C:\\Code\\v3\\Common\\cpp\\Clang\\8\\Tools\\Clang\\v8.0.0\\Windows\\DefaultEnv\\lib\\clang\\8.0.0\\include" -internal-isystem "C:\\Code\\v3\\Common\\cpp\\Clang\\8\\Tools\\Clang\\v8.0.0\\Windows\\DefaultEnv\\include\\c++\\v1" -internal-isystem "C:\\Code\\v3\\Common\\cpp\\Common\\Libraries\\C++\\Catch2\\v2.7.0" -internal-isystem "C:\\Code\\v3\\Common\\cpp\\Common\\Libraries\\C++\\GSL\\v2.0.0\\include" -fdeprecated-macro -fdebug-compilation-dir "C:\\Code\\v3\\Common\\cpp\\Clang\\8" -ferror-limit 19 -fmessage-length 200 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11 -std=c++14 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Users\\brownell\\AppData\\Local\\Temp\\test-9329b0.o" -x c++ "c:\\temp\\test.cpp" -faddrsig
clang -cc1 version 8.0.0 based upon LLVM 8.0.0 default target x86_64-pc-windows-msvc
#include "..." search starts here:
#include <...> search starts here:
 C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\lib\clang\8.0.0\include
 C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\include\c++\v1
 C:\Code\v3\Common\cpp\Common\Libraries\C++\Catch2\v2.7.0
 C:\Code\v3\Common\cpp\Common\Libraries\C++\GSL\v2.0.0\include
End of search list.
In file included from c:\temp\test.cpp:1:
In file included from C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\include\c++\v1\vector:274:
In file included from C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\include\c++\v1\iosfwd:96:
C:\Code\v3\Common\cpp\Clang\8\Tools\Clang\v8.0.0\Windows\DefaultEnv\include\c++\v1\wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
              ^~~~~~~~~
1 error generated.

【问题讨论】:

  • PSA:The STL is effectively dead。 C++ 使用“标准库”。
  • 这可能是 WSL 的一个怪癖。您是否尝试过 Linux 安装,甚至是虚拟机或容器中的 Ubuntu? Vagrant 非常适合这种快速测试。
  • @paxdiablo 在我的机器上使用 clang(Apple LLVM 版本 10.0.1 (clang-1001.0.46.4))。
  • @paxdiablo 你能详细说明一下吗?即使使用-Wall,clang 对此代码也没有任何问题。你指的是main(void)吗?
  • @tadman - 感谢您的关注 - 以下是您问题的一些答案: - 这不是 WSL 的怪癖 - 我看到 Linux 在 VM 和 docker 中运行时的行为相同。 - 使用 clang 作为 GCC 和 MSVC 的前端时,此测试的工作正常。我只在没有支持工具链的情况下尝试使用 clang 时才看到这个问题。

标签: c++ clang


【解决方案1】:

Linux 版

在 linux 上,clag 将默认使用 gnu 的标准库实现,即libstdc++。要使用 llvm 的实现 - libc++ - 你首先需要安装它。例如,在 ubuntu 上:sudo apt install libc++-9-dev libc++abi-9-dev。您可能需要添加苹果存储库。关注these instructions 了解更多系统和最新版本。然后需要在编译时明确指定:-stdlib=libc++。请看https://libcxx.llvm.org/docs/UsingLibcxx.html

对于链接安装 lld (sudo apt install lld-9) 然后在使用 -fuse-ld=lld 编译时指定它

【讨论】:

  • 不幸的是,这些建议并未解决原始问题中提到的具体问题(尽管,我的直觉是它们将解决我在修复原始问题后遇到的下一组问题) .我仍然遇到与缺少包含文件相关的预处理器问题(尽管我看到错误的文件在 Linux 和 Windows 上有所不同)。
  • 如果您按照说明进行操作,标题或链接应该不会有任何问题。 clang 可能找不到 libc++ 的位置,在这种情况下您可以显式指定它,如链接文章中所示
猜你喜欢
  • 2018-07-12
  • 1970-01-01
  • 2017-10-17
  • 2015-07-20
  • 2015-02-06
  • 2015-03-25
  • 2022-12-01
  • 2019-04-09
相关资源
最近更新 更多