我正在研究调试/内存工具.我想显示来自C++的符号,问题是它们非常冗长.目前我正在使用__cxa_demangle,但由于包含默认模板参数,这通常会产生超过500个字符的巨大字符串.
clang++ 当它报告符号时,可以清楚地做出聪明的事情,有什么方法可以利用它吗?
举个简单的例子,我们来看看:
std::map<std::string const, unsigned int, std::less<std::string const>, std::allocator<std::pair<std::string const, unsigned int> > >::find(std::string const&)
Run Code Online (Sandbox Code Playgroud)
显然可以报告为:
std::map<std::string const, unsigned int>::find(std::string const&)
Run Code Online (Sandbox Code Playgroud)
..如果我有一个足够聪明的工具.很明显,如果没有额外的知识(比如最初使用的包含 - 我可能会得到这些),这很难做到,但我会感激任何指针.
到目前为止,我一直指向libcxxabi,但除了没有解析树的公共接口(这本身不会阻止我)之外,似乎我必须努力确定哪些参数是默认值.如果我能以某种方式欺骗我这样做,那就太好了.
我下载了gtest 1.6,并用clang ++编译.
我得到了libgtest.a,然后将其复制到了/usr/local/lib/libgtest_clang.a.
当我使用简单的C++代码进行测试时,一切正常,但是,当我尝试在测试代码中使用向量时,我在构建过程中收到了这些错误消息.编译工作正常.
Undefined symbols for architecture x86_64:
"std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::find(wchar_t const*, unsigned long, unsigned long) const", referenced from:
testing::AssertionResult testing::(anonymous namespace)::IsSubstringImpl<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >(bool, char const*, char const*, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&) in libgtest_clang.a(gtest-all.o)
...
Run Code Online (Sandbox Code Playgroud)
这是我用于构建的命令行.
clang++ -DGTEST_USE_OWN_TR1_TUPLE=1 -std=c++11 -stdlib=libc++ main.cpp test_a.cc \
-L/usr/local/lib -I. -lgtest_clang -o t
Run Code Online (Sandbox Code Playgroud)
这是测试中的测试代码和代码.
#include <limits.h>
#include <time.h>
#include <gtest/gtest.h>
#include <list>
#include …Run Code Online (Sandbox Code Playgroud) 让 clang++ 使用 GCC 的 libstdc++ ( -stdlib=stdc++)是很容易的,但是我该如何做相反的事情呢?在 OS X Mavericks 上,c++ 系统库是 libc++,这意味着基本上不能使用 libstdc++(如果你与其他 c++ 库如已经用 libc++ 编译的 boost 混合)。所以,粗略地说,这意味着 G++ 不可用......除非我可以要求它使用 libc++ 而不是 libstdc++。
谢谢。
我正在使用以下脚本g++-libc++在带有 MacPorts 的 Mac OS X 上在 libc++ 之上运行 g++(因此-mp名称。
#! /bin/sh
clangxx=clang++-mp-3.5
gxx=g++-mp-4.9
libcxx_includes=$($clangxx -print-search-dirs |
perl -ne 's{^libraries: =(.*)}{$1/../../../} && print')
exec $gxx -std=c++11 \
-isystem ${libcxx_includes}/include/c++/v1 \
-nostdinc++ -nodefaultlibs \
-lc -lc++ -lc++abi -lgcc_s.10.5 \
-Wl,-no_compact_unwind \
"$@"
Run Code Online (Sandbox Code Playgroud) 考虑以下示例:
#include <iostream>
#include <type_traits>
struct A
{
//A() = default; // does neither compile with, nor without this line
//A(){}; // does compile with this line
int someVal{ 123 };
void foobar( int )
{
};
};
int main()
{
const A a;
std::cout << "isPOD = " << std::is_pod<A>::value << std::endl;
std::cout << "a.someVal = " <<a.someVal << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这可以用g ++编译,但不能用clang ++编译,尝试使用以下命令: clang++ -std=c++11 -O0 main.cpp && ./a.out
从clang编译错误:
main.cpp:19:13:错误:const类型'const A'对象的默认初始化需要用户提供的默认构造函数
我从这个Stack Overflow问题中了解到,非POD类获得默认构造函数.这甚至不是必需的,因为变量具有c ++ …
情况1
以下代码在MSVC和GCC中产生截然不同的结果:
#include <iostream>
template <typename T>
void foo(const T&) {
#ifdef _MSC_VER
std::cout << "foo(const T&): " << __FUNCDNAME__ << std::endl;
#else
std::cout << __PRETTY_FUNCTION__ << std::endl;
#endif
}
void foo(const char*) {
std::cout << "foo(const char*)" << std::endl;
}
int main() {
extern char s[];
foo(s);
}
char s[] = "abc";
Run Code Online (Sandbox Code Playgroud)
MSVC 2013 Update 5,MSVC 2015 Update 1(也在http://webcompiler.cloudapp.net上尝试了更新2,结果相同):
foo(const char*)
Run Code Online (Sandbox Code Playgroud)
GCC 5.3.0,Clang 3.7.0(DEMO):
void foo(const T&) [with T = char []]
Run Code Online (Sandbox Code Playgroud)
案例2
现在让我们删除模板:
#include …Run Code Online (Sandbox Code Playgroud) 我有一个简单的"Hello,world"风格程序,我在FreeBSD上用clang ++编译:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "Oh, hello" << endl;
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我用clang ++和它的libc ++编译的:
$ clang++ -stdlib=libc++ -v ohhello.cpp
FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0)
Target: x86_64-unknown-freebsd11.0
Thread model: posix
InstalledDir: /usr/bin
"/usr/bin/clang++" -cc1 -triple x86_64-unknown-freebsd11.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name ohhello.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/bin/../lib/clang/3.8.0 -internal-isystem /usr/include/c++/v1 -fdeprecated-macro -fdebug-compilation-dir /usr/home/mike/projects/ohhello -ferror-limit …Run Code Online (Sandbox Code Playgroud) 我有两个应用程序,一个用 gcc(c++) 编译,另一个用 clang++ 编译。我要为这两个应用程序使用通用共享增强库。我的问题是是否使用 clang 编译器或 gcc 编译器编译 boost 共享库。我可以在使用 clang 编译的应用程序中使用使用 gcc 编译的 boost 库吗?
-Wlifetime编译标志的目的是什么clang?
我在Internet上找到的有关信息非常模糊。这是任何明显的功能吗?
我正在尝试在我的 mac 上安装 allennlp。我尝试安装 macOS 头文件,它解决了缺少头文件的问题,但现在我遇到了新问题。
我运行时的错误pip install allennlp:
Running setup.py bdist_wheel for jsonnet ... error
Complete output from command /anaconda3/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/qf/jkn4v43j08xgst0r9yxyl0dc0000gn/T/pip-install-i4nyb384/jsonnet/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /private/var/folders/qf/jkn4v43j08xgst0r9yxyl0dc0000gn/T/pip-wheel-eof7cc6k --python-tag cp37:
running bdist_wheel
running build
running build_ext
x86_64-apple-darwin13.4.0-clang++ -c -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 core/desugarer.cpp -o core/desugarer.o
In file included from core/desugarer.cpp:17:
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/cassert:21:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/assert.h:44:
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdlib.h:111:82: error: use …Run Code Online (Sandbox Code Playgroud) TL; 博士;
如何从 callExpr -> arg_0 -> DeclRefExpr 获取用于常量大小数组声明大小的宏名称。
详细问题说明:
最近我开始着手一项挑战,它需要源到源转换工具来修改带有附加参数的特定函数调用。Reasearching about how I can acheive 向我介绍了这个惊人的工具集 Clang。我一直在学习如何使用 libtooling 中提供的不同工具来实现我的目标。但现在我遇到了一个问题,在这里寻求你的帮助。
考虑下面的程序(我的源代码),我的目标是使用安全版本的 strcpy_s 重写对 strcpy 函数的所有调用,并在新函数调用中添加一个附加参数,即目标指针最大大小。所以,对于下面的程序,我重构的调用就像 strcpy_s(inStr, STR_MAX, argv[1]);
我编写了一个 RecursiveVisitor 类并检查 VisitCallExpr 方法中的所有函数调用,以获取 dest arg 的最大大小,我正在获取第一个 agrument 的 VarDecl 并尝试获取大小(ConstArrayType)。由于源文件已经过预处理,我将 2049 视为大小,但在这种情况下我需要的是宏 STR_MAX。我怎么能得到呢?(使用此信息创建替换,然后使用 RefactoringTool 替换它们)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define STR_MAX 2049
int main(int argc, char **argv){
char inStr[STR_MAX];
if(argc>1){
//Clang tool required to transaform the below call into strncpy_s(inStr, STR_MAX, argv[1], strlen(argv[1]));
strcpy(inStr, argv[1]);
} else { …Run Code Online (Sandbox Code Playgroud) clang++ ×10
c++ ×6
clang ×3
g++ ×2
gcc ×2
allennlp ×1
c++11 ×1
c++17 ×1
c++20 ×1
freebsd ×1
googletest ×1
libc++ ×1
libstdc++ ×1
libtooling ×1
llvm-clang ×1
macos ×1
python-3.x ×1
stl ×1
templates ×1
visual-c++ ×1