我一直在尝试构建 clang 但在运行 36 小时后失败并出现以下错误:
[3840/4138] Linking CXX shared library lib/libLTO.so.7
FAILED: lib/libLTO.so.7
: && /usr/local/bin/clang++ -fPIC -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wno-comment -Wstring-conversion -fdiagnostics-color -g -Wl,-z,defs -Wl,-z,nodelete -fuse-ld=gold -Wl,--version-script,/home/tehreem/clang-llvm/build/tools/lto/LTO.exports -shared -Wl,-soname,libLTO.so.7 -o lib/libLTO.so.7 tools/lto/CMakeFiles/LTO.dir/LTODisassembler.cpp.o tools/lto/CMakeFiles/LTO.dir/lto.cpp.o -Wl,-rpath,"\$ORIGIN/../lib" lib/libLLVM-7.so && :
Run Code Online (Sandbox Code Playgroud)
然后有一长串未定义的参考错误。如果有人想让我发布它们,请告诉我。这些是我在构建时使用的标志:
cmake -G Ninja ../llvm -DLLVM_USE_LINKER=gold -DLLVM_PARALLEL_LINK_JOBS=1 -DLLVM_LINK_LLVM_DYLIB=true -DLLVM_USE_SPLIT_DWARF=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_CXX_COMPILER=clang++
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?还有36小时?如果我不将 CXX 编译器设置为 clang,它通常会在 4-5 小时内失败并出现相同的错误。以下是错误的完整详细信息:https : //pastebin.com/EycPC437
我已经在我的系统上安装了 Clang 7.0。我使用 GNU 7.3 构建的。幸运的是它成功了一次。但是,如果我再试一次,它也会继续失败。
我想使用 llvm 控制流完整性,我使用了 Ubuntu 14.04 附带的 clang+llvm 二进制打包版本 7.0.1。当我使用带有选项 '-fsanitize=cfi -flto -fvisibility=hidden' 的 clang++ 时,出现以下错误:
/usr/bin/ld: /home/username/llvm-7.0.1/bin/../lib/LLVMgold.so: error loading plugin: /home/username/llvm-7.0.1/bin/../lib/LLVMgold.so: cannot open shared object file: No such file or directory
Run Code Online (Sandbox Code Playgroud)
知道如何在不使用启用黄金的选项编译源代码的情况下解决这个问题吗?
我刚刚从https://github.com/llvm/llvm-project.git安装了 clang++ 和 libc++ 。尝试运行时:
clang main.cpp -stdlib=libc++ -lc++abi
Run Code Online (Sandbox Code Playgroud)
其中main.cpp:
#include <iostream>
int main() {
std::cout << "main>" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
-v 选项的输出:
clang version 9.0.0 (https://github.com/llvm/llvm-project.git 40046bc8430f0b90d76cef9e6cc62ccc2abcb0b0)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.3.0
Found …Run Code Online (Sandbox Code Playgroud) 我们目前正在调查我们程序中可能的未定义行为,该行为由 clang7 UBSan 与 boost 1.69.0 中的 boost::program_option 结合标记。我们已经创建了以下可以编译和运行的工作示例clang++ -std=c++17 -fsanitize=undefined -fno-omit-frame-pointer -lboost_program_options debug.cpp && UBSAN_OPTIONS=print_stacktrace=1 ./a.out
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main() {
std::string test_string = "";
po::options_description desc("test");
desc.add_options()
("string", po::value<std::string>(&test_string))
;
constexpr char *argv[] = {"test", "--string", "test"};
constexpr int argc = sizeof(argv) / sizeof(char*);
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
std::cerr << "Before notify" << std::endl;
po::notify(vm);
std::cout << "string -> " << test_string << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我们得到以下输出:
/usr/include/boost/any.hpp:249:17: runtime error: …Run Code Online (Sandbox Code Playgroud) LLVM 的 clang/clang++ 允许您为整个代码区域指定属性。语法如下:
// the following works fine under clang:
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void f(){}
#pragma clang attribute pop
Run Code Online (Sandbox Code Playgroud)
在此示例中,该函数f将在支持 SSE4.2 指令集的情况下进行编译。这将适用于attribute push和之间的所有函数attribute pop。
如果我们添加一个命名空间呢?
namespace joe {
#pragma clang attribute push(__attribute__((target("sse4.2"))), apply_to=function)
void g() {}
#pragma clang attribute pop
}
Run Code Online (Sandbox Code Playgroud)
它失败了'error: expected unqualified-id'。显然attribute,编译器不需要关键字。
鉴于命名空间的常见程度,这个问题有点出乎意料。当然,可以避免命名空间来解决这个问题,但这有点不雅。
有更好的解决方法吗?
我已经用所有最近的 LLVM clang++ 编译器(最高 8.0)验证了这个问题。
我只是想向某人解释编译代码和解释代码之间的区别,当我收到一个
main.cpp:1:10: fatal error: 'iostream' file not found
Run Code Online (Sandbox Code Playgroud)
调用 g++ main.cpp 获取简单的 hello world C++ 文件时。
我仔细研究了一下,发现......
JM:Desktop user$ which g++
/usr/local/bin/g++
JM:Desktop user$ ls -al /usr/local/bin/g++
lrwxr-xr-x 1 user admin 47 4 Dez 2018 /usr/local/bin/g++ -> /Library/Developer/CommandLineTools/usr/bin/c++
JM:Desktop user$ ls -al /Library/Developer/CommandLineTools/usr/bin/c++
lrwxr-xr-x 1 root wheel 5 3 Feb 20:29 /Library/Developer/CommandLineTools/usr/bin/c++ -> clang
Run Code Online (Sandbox Code Playgroud)
... g++ 与 clang 而不是 clang++ 相关联,因此我将其称为 C 编译器。
我刚刚删除了开发人员工具并重新安装了它们 - 同样的事情。
这是正常现象还是我的系统出了问题?有什么意义吗?我错过了什么?
谢谢您的帮助!
我在 x64 Linux 上使用 clang 9.0.1 和 gcc 9.2.1,两者都带有--std=c++17(或--std=c++2a)。
Gcc 可以在没有任何错误或警告的情况下构建以下示例,而 clang++ 会报告error: constexpr if condition is not a constant expression这两if constexpr行。(顺便说一句,在我的 MacBook 上,Apple clang-11.0.0 也报告了同样的错误。)
MCVE:
#include <utility>
enum class TopicType {
MarketData = 'M',
Timer = 'T',
};
template<class Topic>
struct TopicBase {
constexpr static TopicType type() { return Topic::type; };
const Topic& topicImp;
explicit TopicBase(const Topic &t) : topicImp(t) {}
};
struct MarketDataTopic {
static constexpr TopicType type{TopicType::MarketData};
}; …Run Code Online (Sandbox Code Playgroud) int a;
int a=3; //error as cpp compiled with clang++-7 compiler but not as C compiled with clang-7;
int main() {
}
Run Code Online (Sandbox Code Playgroud)
对于 C,编译器似乎将这些符号合并为一个全局符号,但对于 C++,这是一个错误。
文件 1:
int a = 2;
Run Code Online (Sandbox Code Playgroud)
文件2:
#include<stdio.h>
int a;
int main() {
printf("%d", a); //2
}
Run Code Online (Sandbox Code Playgroud)
作为用 clang-7 编译的 C 文件,链接器不会产生错误,我假设它将未初始化的全局符号“a”转换为外部符号(将其视为被编译为外部声明)。作为使用 clang++-7 编译的 C++ 文件,链接器会产生多重定义错误。
更新:链接的问题确实回答了我的问题中的第一个示例,特别是“在 C 中,如果在同一翻译单元中较早或较晚找到实际的外部定义,则暂定定义仅作为声明。” 和'C++ 没有“暂定定义”'。
至于第二种情况,如果我 printf a,那么它会打印 2,所以显然链接器已经正确链接了它(但我以前假设编译器将临时定义初始化为 0 作为全局定义,并且会导致链接错误)。
事实证明,int i[];两个文件中的暂定定义也与一个定义相关联。int i[5];也是 .common 中的一个暂定定义,只是向汇编器表达了不同的大小。前者称为类型不完整的暂定定义,而后者是类型完整的暂定定义。
C 编译器发生的情况是,int a在 .common 中将其设为强绑定弱全局,并在符号表中 …
在以下代码中,clang 抱怨不存在转换运算符:
class A{
public:
operator int () const {return i;}
A(int i_): i(i_) {}
template<class B>
friend auto operator * (const B& l, A&& r)
-> decltype(l * int(std::move(r))){
return l * int(std::move(r));
}
int i;
};
Run Code Online (Sandbox Code Playgroud)
所有 clang 版本 6-9 都会发生这种情况,请参阅https://godbolt.org/z/ELuRHp。错误信息如下:
<source>:11:19: error: cannot convert 'typename std::remove_reference<A &>::type' (aka 'A') to
'int' without a conversion operator
-> decltype(l * int(std::move(r))){
^~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
相同的代码用 gcc 7-9 编译就好了,所以我想知道这是否是 clang 中的错误或者代码不正确。
编辑:由@daniel-langr 提供的相同错误的较短版本,可以在这里找到https://godbolt.org/z/Krnh27。原始代码示例是我们代码库中类的简化版本,因此不必要的decltype.
在 ubuntu 20.04 上,当我使用 clang-8 或 clang-9(clang 版本 9.0.1-12)编译包含对 libm 的引用的简单代码时,它将失败并显示错误“未定义的引用__pow_finite”
#include <math.h>
int main()
{
double x=1, y=1;
x = pow(x,y);
}
Run Code Online (Sandbox Code Playgroud)
clang-9 -lm test.c -ffast-math
/usr/bin/ld: /tmp/test-9b1a45.o: in function `main':
test.c:(.text+0x2a): undefined reference to `__pow_finite'
readelf -Ws /lib/x86_64-linux-gnu/libm.so.6| grep pow_finite
626: 000000000002ed90 65 IFUNC GLOBAL DEFAULT 17 __pow_finite@GLIBC_2.15
Run Code Online (Sandbox Code Playgroud)
gcc 没问题。知道这里有什么问题吗?
C++ 也有同样的问题:
#include <cmath>
int main()
{
double x=1, y=1;
x = pow(x,y);
}
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上使用了-lm,我只是忘记放入文本。如果我不添加它,则是另一个错误。
$ clang-9 test.c
/usr/bin/ld: /tmp/test-3389a6.o: in function `main':
test.c:(.text+0x25): undefined …Run Code Online (Sandbox Code Playgroud) clang++ ×10
c++ ×7
clang ×6
c ×3
llvm-clang ×2
boost ×1
c++17 ×1
g++ ×1
llvm ×1
macos ×1
ubsan ×1
ubuntu-14.04 ×1
ubuntu-20.04 ×1