c++ - 没有 GCC 或 MSVC 的 Clang

标签 c++ clang

我想在 Linux 上使用没有 GCC 的 clang,在 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.

最佳答案

适用于 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编译时指定它

关于c++ - 没有 GCC 或 MSVC 的 Clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56468430/

相关文章:

c++ - 代码的后半部分以某种方式影响前一部分

C++ 转换问题

python - 安装枕头错误

c++ - Profiler 不显示我的代码

c++ - 使用没有内联代码的 dll 时出现 LNK2028 和 LNK2019 错误

Clang 静态分析器找不到 stdio.h

c - 向参数为 0 的函数发送参数时的行为不一致

c++ - 编译 boost C++11 clang mac 找不到 cstddef

c++ - 如何将结构数组中的成员数据传递给函数?

c++ - 如何在 Linux(和 OSX)上查询分配的内存量?