当我使用clang编译一个简单的hello world程序时,在elf64文件的注释中我仍然找到与GCC相关的信息。为什么?我使用的是 clang 而不是 gcc。
我使用的是ubuntu 16.04。
username@ubuntu:~$ clang++ -stdlib=libc++ test.cpp
username@ubuntu:~$ objdump --full-contents --section=.comment a.out
a.out: file format elf64-x86-64
Contents of section .comment:
0000 4743433a 20285562 756e7475 20352e34 GCC: (Ubuntu 5.4
0010 2e302d36 7562756e 7475317e 31362e30 .0-6ubuntu1~16.0
0020 342e3429 20352e34 2e302032 30313630 4.4) 5.4.0 20160
0030 36303900 636c616e 67207665 7273696f 609.clang versio
0040 6e20342e 302e3020 28746167 732f5245 n 4.0.0 (tags/RE
0050 4c454153 455f3430 302f6669 6e616c29 LEASE_400/final)
0060 00 .
username@ubuntu:~$
Run Code Online (Sandbox Code Playgroud)
测试.cpp是:
#include <iostream>
int main(int argc, char* argv[])
{ …Run Code Online (Sandbox Code Playgroud) 当我尝试使用 LLDB 检查 std::string 变量时,出现“错误:摘要字符串解析错误”。
#include <iostream>
#include <string>
int main() {
std::string a{"123"};
std::cout << a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Process 4492 stopped
* thread #1, name = 'main', stop reason = breakpoint 1.1
frame #0: 0x00005555555551e9 main`main at main.cpp:6:1
3
4 int main() {
5 std::string a{"123"};
-> 6 std::cout << a << std::endl;
7 return 0;
8 }
(lldb) v a
(std::string) a = error: summary string parsing error
Run Code Online (Sandbox Code Playgroud)
附加信息:
Process 4492 stopped
* thread #1, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Cython 中同时编译 C 和 C++ 源代码。这是我当前的设置:
-setup.py
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
import os
language = "c++"
extra_compile_flags = ["-std=c++17"]
os.environ["CC"] = "clang++"
ext_modules = [
Extension(
name="Dummy",
sources=["mydummy.pyx", "source1.cpp","source2.c"],
language=language,
extra_compile_args=extra_compile_flags,
)
]
ext_modules = cythonize(ext_modules)
setup(
name="myapp",
ext_modules=ext_modules,
)
Run Code Online (Sandbox Code Playgroud)
- 编译命令:
python3 setup.py build_ext --inplace --verbose
Run Code Online (Sandbox Code Playgroud)
在日志中我收到以下消息:
clang++ -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I. -I/usr/include/python3.6m -I/usr/include/python3.6m -c /path/source2.c -o build/temp.linux-x86_64-3.6/./path/source2.o -std=c++17 -O3
clang: …Run Code Online (Sandbox Code Playgroud) 以下代码:
class A
{
friend class B;
int m;
};
class B
{
friend void f(A* p) { p->m = 32; }
};
Run Code Online (Sandbox Code Playgroud)
在 clang 版本 9.0.0-2 中编译,但不在 g++ 版本 9.2.1 中编译
friend-test.cpp: In function ‘void f(A*)’:
friend-test.cpp:10:28: error: ‘int A::m’ is private within this context
10 | friend void f(A* p) { p->m = 32; }
| ^
friend-test.cpp:5:7: note: declared private here
5 | int m;
| ^
Run Code Online (Sandbox Code Playgroud)
哪个编译器是对的?
在 Mac OS Mojave 10.14.6 上使用 VS Code 处理一个奇怪的包含问题。
#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/ajm/Projects/restaurant/cpp/sim/src/main.cpp).C/C++(1696)
cannot open source file "wchar.h" (dependency of "iostream")
Run Code Online (Sandbox Code Playgroud)
请注意,这种情况是在我将 MacOS CommandLineTools 更新为xcode-select --install.
我的编译器路径在 VS Code 中是
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
Run Code Online (Sandbox Code Playgroud)
我的 includePath 是
${workspaceFolder}/**
${BOOST_ROOT}/**
Run Code Online (Sandbox Code Playgroud)
当我添加/Library/Developer/CommandLineTools/usr/include/c++/v1/**到我的 includePath 并将编译器路径更改为 时/usr/bin/g++,STL 包含问题消失了,我得到了这个:
cannot open source file "boost/lambda/lambda.hpp"
Run Code Online (Sandbox Code Playgroud)
请注意,当我从命令行构建程序时,它运行得很好;一切都按其应有的样子被发现。
这是我的 Makefile:
PROG = sim
CC = g++
CPPFLAGS = -g -Wall -I$(SDIR) -I$(ODIR) -I$(BOOST_ROOT)
ODIR …Run Code Online (Sandbox Code Playgroud) 我正在 Travis CI 中运行构建,在编译过程中出现此错误。我尝试指定 C++ 编译器并尝试使用 g++,但这导致了更多错误。
$ clang -I ./jsoncpp/include/ -L ./jsoncpp/build/debug/lib -std=c++14 -v test.cpp -o buildtest.exe
...
./IBMWatson.h:79:44: error: cannot pass object of non-trivial type 'std::string'
(aka 'basic_string<char>') through variadic function; call will abort at
runtime [-Wnon-pod-varargs]
curl_easy_setopt(curl, CURLOPT_PASSWORD, apikey); /* Par...
^
./IBMWatson.h:81:39: error: cannot pass object of non-trivial type 'std::string'
(aka 'basic_string<char>') through variadic function; call will abort at
runtime [-Wnon-pod-varargs]
curl_easy_setopt(curl, CURLOPT_URL, url); /* Sets Regio...
^
./IBMWatson.h:104:102: warning: result of comparison against a string …Run Code Online (Sandbox Code Playgroud) 出于某种原因,在clang 中仍然缺乏预期的CTAD:std::initializer_list
std::initializer_list l{1,2,3}; // error in clang
Run Code Online (Sandbox Code Playgroud)
添加如下所示的用户定义指南可以解决此问题:
namespace std {
template<class T>
initializer_list(const initializer_list<T>&) -> initializer_list<T>;
}
Run Code Online (Sandbox Code Playgroud)
但是是否允许为std::类型添加用户定义的 CTAD 指南?
我正在尝试使用 clang 中的模块,并希望将标准库包含为模块而不是包含。
目前我这样做
#include <iostream>
#include <string>
Run Code Online (Sandbox Code Playgroud)
看来您在 msvc 中应该能够导入标准库,例如
import std.core;
Run Code Online (Sandbox Code Playgroud)
然而,当使用 clang 时,这似乎没有实现,或者以另一种方式实现。
我的问题是:是否可以像微软建议的那样导入 stl-includes,或者是否可以将标准库包含映射到模块 somhow。
注意:我无法使用的原因#include <...>或者#import <...>是因为其他错误可能会导致其自身问题。所以我认为import std.core如果可能的话,获取或类似是现在要走的路。
ModernesCpp还提到了 std.core。
我偶然发现了模板的问题,clang++我不知道如何解决。
我希望使用以类和函数作为参数的模板化函数。但是当函数是重载函数时,我得到的错误candidate template ignored: couldn't infer template argument 'F'是完全合理的。
例如,示例程序
#include <iostream>
#include <math.h>
template<class T, typename F>
T foo(F f,T x){
return f(x);
}
int main(void) {
std::cout << "foo(sinf,0.34) is " << foo(sinf,0.34) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
完全按照我想要的方式工作。现在,如果我使用重载函数(例如,sin而不是 )sinf,则无法推断模板参数。如何更改 的函数模板foo以帮助编译器解析 的类型F?
我有以下函数可以旋转 char 向量:
void rotate()
{
std::ranges::rotate(_right, _right.begin() + 1);
}
Run Code Online (Sandbox Code Playgroud)
_right 定义为:
std::vector<char> _right;
Run Code Online (Sandbox Code Playgroud)
尝试用 clang 15 编译它,它抱怨(https://godbolt.org/z/7ovTfxe31):
no matching function for call to '__begin'
Run Code Online (Sandbox Code Playgroud)
原因似乎如下:
in instantiation of template type alias 'iterator_t' requested here
requires contiguous_iterator<iterator_t<_Derived>>
Run Code Online (Sandbox Code Playgroud)
但我假设向量是一个连续的容器。代码使用 GCC 编译并运行。
这是我的问题: