试图编译这段代码:
const int a = 1;
auto lambda = [&]() {
&a;
};
lambda();
Run Code Online (Sandbox Code Playgroud)
在clang ++上一切都很好,但是g ++给出了一个错误:
错误:左值作为一元'&'操作数
我没有找到解释这种行为的任何东西.这是g ++中的错误吗?或者clang ++会错过什么吗?
我正在使用Xcode 5.1.1运行MacOS X Mavericks,包括命令行工具.我正在使用Xcode提供的clang ++编译简单的C++程序,版本信息是:Apple LLVM版本5.1(clang-503.0.40)(基于LLVM 3.4svn)
我发现如果我尝试运行以下命令
clang ++ -o hello.out hello.cpp
我收到以下错误:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in hello-2ad0da.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in hello-2ad0da.o
"std::cout", referenced from:
_main in hello-2ad0da.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<<<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in hello-2ad0da.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
如果我将命令更改为
clang ++ -o hello.out -stdlib = libstdc …
我正在使用 C++11 线程构建一个应用程序,但我似乎无法让它在 MacOSX 10.9 上与 clang++ 一起工作。这是我能找到的导致问题的最简单示例:
#include <thread>
#include <iostream>
class Functor {
public:
Functor() = default;
Functor (const Functor& ) = delete;
void execute () {
std::cerr << "running in thread\n";
}
};
int main (int argc, char* argv[])
{
Functor functor;
std::thread thread (&Functor::execute, std::ref(functor));
thread.join();
}
Run Code Online (Sandbox Code Playgroud)
这在 Arch Linux 上使用 g++(版本 4.9.2)和以下命令行编译并运行良好:
$ g++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
Run Code Online (Sandbox Code Playgroud)
它还可以使用 clang++(版本 3.5.0,也在 Arch Linux 上)编译并运行良好:
$ clang++ -std=c++11 -Wall -pthread test_thread.cpp -o test_thread
Run Code Online (Sandbox Code Playgroud)
但在 …
我正在编写一个输出 C++ 代码的语法翻译器,但遇到了一个有趣的问题。假设我有两个文件:ln.x和ln.cpp. 在ln.x:
abc
Run Code Online (Sandbox Code Playgroud)
在ln.cpp:
#line 1 "ln.x"
(
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 GCC 编译它时,它会在ln.x以下位置打印相应的行:
ln.x:1:1: error: expected unqualified-id at end of input
abc
^
ln.x:1:1: error: expected ‘)’ at end of inpu
Run Code Online (Sandbox Code Playgroud)
但是,Clang 只是打印同一文件的行:
ln.x:1:2: error: expected unqualified-id
(
^
ln.x:1:2: error: expected ')'
ln.x:1:1: note: to match this '('
(
^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
有没有办法让 Clang 像 GCC 一样打印文件的行?
以下面的 constexpr 为例:
#include <iostream>
constexpr int fib(const int i)
{
if (i == 0) return 0;
if (i == 1) return 1;
return fib(i-1) + fib(i-2);
}
int main(){
std::cout << fib(45) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
尽管是 constexpr,但它不会在编译时进行评估。
我学到的强制编译时评估的技巧如下:
#include <iostream>
#include <type_traits>
#define COMPILATION_EVAL(e) (std::integral_constant<decltype(e), e>::value)
constexpr int fib(const int i)
{
if (i == 0) return 0;
if (i == 1) return 1;
return fib(i-1) + fib(i-2);
}
int main(){
std::cout << COMPILATION_EVAL(fib(45)) << '\n';
} …Run Code Online (Sandbox Code Playgroud) 请使用以下源代码:
struct Foo {
Foo(){}
};
Foo f;
Run Code Online (Sandbox Code Playgroud)
使用时clang++,它会为构造函数创建一个符号:
clang++ -c foo.cpp
nm -C foo.o | grep Foo
0000000000000000 W Foo::Foo()
Run Code Online (Sandbox Code Playgroud)
但是,在编译时g++,它会为构造函数创建多个符号:
g++ -c foo.cpp;
nm -C foo.o | grep Foo
0000000000000000 W Foo::Foo()
0000000000000000 W Foo::Foo()
0000000000000000 n Foo::Foo()
Run Code Online (Sandbox Code Playgroud)
为什么会g++在同一个目标文件中创建重复的弱符号?
我唯一的理论是它与内联有关,但这只是猜测.
我应该注意,受损的名称如下所示:
g++ -c foo.cpp; nm foo.o | grep Foo
0000000000000000 W _ZN3FooC1Ev
0000000000000000 W _ZN3FooC2Ev
0000000000000000 n _ZN3FooC5Ev
Run Code Online (Sandbox Code Playgroud)
因此,尽管未重整名称是相同的,ZN3FooC1Ev并且ZN3FooC2Ev是不同的.
(Ubuntu 16.04.1)
默认情况下,在16.04.1上,clang为5.4选择gcc工具链。不幸的是,我有一个要求 5.0之前的ABI 的库,并且我无权访问源代码,也没有实现者发布新版本。我一直在尝试使用--gcc-toolchain选项,但是无法正常工作。(ctrbegin.o和crtend.o在链接上没有正确的前缀。)
$ clang++-3.8 -v -print-search-dirs
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/6.0.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.4.0
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/6.0.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.4.0
Found candidate …Run Code Online (Sandbox Code Playgroud) 这是我的第一个 cmake 文件。我有一个带有 clang 和 g++ 的 linux 系统。还安装了 libc++。我在 Mac (xcode) 上开发但部署到 linux。我正在编写一个 cmake 文件,我可以在其中选择 clang 或 g++ 和 libc++ 或 libstdc++。所以有 4 种可能的组合。
我想出了如何选择编译器并在其上强制使用 c++11,但我无法弄清楚如何指定标准库。有什么建议?
这是我到目前为止:
## cmake ###
cmake_minimum_required (VERSION 3.5)
#set project directories
set(ProjectDirectory ${CMAKE_SOURCE_DIR}) #.../Project/
set(BuildDirectory ${ProjectDirectory}/Build)
set(ProductDirectory ${ProjectDirectory}/Products)
set(sourceDirectory ${ProjectDirectory}/Source)
#print project directories
message(${ProjectDirectory})
message(${BuildDirectory})
message(${ProductDirectory})
#configure cmake
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ProductDirectory})
set(CMAKE_VERBOSE_MAKEFILE on)
#compiler and standard library settings
set(CMAKE_CXX_COMPILER "clang++")
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -stdlib=libc++")
#libstdc++ #linux
#libc++ #OS X …Run Code Online (Sandbox Code Playgroud) 以下代码编译g++ -std=c++11但不编译clang++ -std=c++11.
用g++ -std=c++11 main.cpp和编译clang++ -std=c++11 main.cpp.我正在使用GCC 4.8和Clang 6.0.0(主干).
#include <iostream>
#include <vector>
enum Dir { LEFT, RIGHT };
int main(int argc, char** argv) {
// Interesting line: Notice the single ':'
std::vector<Dir> dirs = { Dir:LEFT, Dir:RIGHT };
for (auto v: dirs) {
std::cout << v << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为了完整性和可搜索性:
$ clang++ -std=c++11 main.cpp
main.cpp:7:29: warning: use of GNU old-style field …Run Code Online (Sandbox Code Playgroud) 我必须使用不同版本的clang编译相同的代码.由于代码包含了每个clang版本都不支持的一些c ++ 17特性,我想在编译期间检查它们是否受支持.据我所知,clang的功能检查宏是正确的方法.
我的问题特别出现在std :: launder上.
我创建了这个最小的例子:
#include "iostream"
#if __has_builtin(__builtin_launder)
void test() {
int i = 42;
std::cout << "Should compile: " << std::launder(&i) << std::endl;
}
#else
void test() {
int i = 42;
std::cout << "Should not even compile: " << std::launder(&i) << std::endl;
}
#endif
int main(){
test();
}
Run Code Online (Sandbox Code Playgroud)
如果我正在编译它(clang版本6.0.0,libc ++)使用clang++ -std=c++1z -stdlib=libc++ -Wall -pedantic test3.cpp && ./a.out
输出是:
Should not even compile: 0x7fff75116f64
虽然显然支持std :: launder,但内置检查不起作用.由于它与评论中的检查相同llvm:实施std :: wash,我假设检查是正确的. …