我正在尝试让控制流完整性与 clang++ 一起使用,但我无法运行以下代码:
\n#include <memory>\n\nstruct X {\n X() {}\n virtual ~X() {};\n};\n\nint main() {\n std::shared_ptr<X> _asd = std::make_shared<X>();\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n如果我编译并运行它
\nclang++ -fsanitize=cfi -fvisibility=hidden -flto -O2 -std=c++14 test.cpp\n./a.out\nRun Code Online (Sandbox Code Playgroud)\n编译后的程序输出:
\n[1] 45850 illegal hardware instruction (core dumped) ./a.out\nRun Code Online (Sandbox Code Playgroud)\n生成的程序在make_shared标准库内的函数调用内崩溃。
当我将析构函数设置为非虚拟时,它会成功终止,因此我最初认为它与此有关,cfi-vcall但它与该选项完美配合-fsanitize=cfi-vcall。
负责任的编译标志似乎是-fsanitize=cfi-unrelated-cast- 通过所有其他可用的检查,应用程序可以正常运行。
从反汇编来看,应用程序似乎是~X()在 make_shared 中调用(另请参阅下面的附加信息)?
这是一个编程错误吗?是在编译器、标准库还是我的代码中?
\n是否有解决方法可以使其在启用 CFI 的情况下编译和运行?
\n预先非常感谢!
\n例行升级 Ubuntu 后,我的系统构建失败。\n现在使用的是 gcc 14,我猜它需要 13?
\n我尝试按照此处的说明进行操作,但我不知道如何进行系统链接
\n请逐步回答我要输入的内容。
\n错误构建输出如下:
\nCMake Error at /usr/share/cmake-3.22/Modules/CMakeTestCXXCompiler.cmake:62 (message):\n The C++ compiler\n\n "/usr/bin/clang++"\n\n is not able to compile a simple test program.\n\n It fails with the following output:\n\n Change Dir: /mnt/Data/git/tipitaka-pali-reader/build/linux/x64/release/CMakeFiles/CMakeTmp\n\n Run Build Command(s):/usr/bin/ninja cmTC_4da98 && [1/2] Building CXX object CMakeFiles/cmTC_4da98.dir/testCXXCompiler.cxx.o\n [2/2] Linking CXX executable cmTC_4da98\n FAILED: cmTC_4da98 \n : && /usr/bin/clang++ CMakeFiles/cmTC_4da98.dir/testCXXCompiler.cxx.o -o cmTC_4da98 && :\n /usr/bin/ld: cannot find -lstdc++: No such file or directory\n clang: error: linker command …Run Code Online (Sandbox Code Playgroud) 我已将 clang 从版本 14 升级到最新版本(主干上为 18),看来我的代码中唯一的问题是来自多个基类的运算符继承(下面是对 godbolt 的示例和引用)。这是一个简化的示例,其中我有一个最终的向量类,它派生了一些添加功能的 mixin。在这种情况下,这些混合是向量-标量乘法和向量-向量乘法,以向量点积的形式实现。两种混合都会添加,operator*但是这两个运算符都专门用于处理特定类型的类型,因此不应该有任何歧义。到目前为止,这种方法运作良好。所以我的问题是 - 这个新错误是某些标准要求的实现还是这只是编译器测试版中的一个错误而我的代码没问题?
https://godbolt.org/z/ca8Yefjdc
#include <cstddef>
#include <type_traits>
#include <array>
template<
typename T, size_t N,
template <typename, size_t> typename FinalTemplate
>
struct VectorData
{
std::array<T, N> data;
};
template<
typename T, size_t N,
template <typename, size_t> typename FinalTemplate
>
struct VectorScalarMultiplication
{
using Final = FinalTemplate<T, N>;
template<typename U, typename Enable = std::enable_if_t<(
std::is_integral_v<U> || std::is_floating_point_v<U>
)>>
Final operator*(const U value) const
{
auto copy = static_cast<const Final&>(*this);
for (size_t …Run Code Online (Sandbox Code Playgroud) 这是一个例子:
struct TestClass {
void testFunction() const {}
static TestClass* ptr_;
constexpr static auto funcPtr = +[](){ ptr_->testFunction(); };
};
TestClass* TestClass::ptr_ = new TestClass();
int main() {
TestClass::funcPtr();
delete TestClass::ptr_;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会在gccand 中编译,不会在clangand中编译msvc。哪个编译器是正确的?这是一个错误吗gcc?
这是clang错误消息:
<source>:4:46: error: member access into incomplete type 'TestClass'
4 | constexpr static auto funcPtr = +[](){ ptr_->testFunction(); };
| ^
<source>:1:8: note: definition of 'TestClass' is not complete until the closing '}'
1 …Run Code Online (Sandbox Code Playgroud) 在过去的三个小时里,我对以下编译错误感到非常困惑.谁能告诉我这里发生了什么?
我试图将log :: formatter(下面标记)定义为它自己的变量,因此它可以在几个地方重复使用.但是,尝试重新使用它时出现编译错误.
但是,如果我完全摆脱该变量,而是复制并粘贴代码,它就会编译.有没有搞错?有什么办法可以做我想要的吗?
boost::shared_ptr<log::core> logger = log::core::get();
logger->set_logging_enabled( enabled );
logger->set_filter(trivial::severity >= level);
logger->add_global_attribute(
"TimeStamp", attr::local_clock());
logger->add_global_attribute(
"ProcessID", attr::current_process_id());
logger->add_global_attribute(
"ThreadID", attr::current_thread_id());
// want this to be it's own variable
log::formatter fmt = expr::stream
<< "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] "
<< "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] "
<< "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] "
<< "[" << expr::attr< std::string >("Channel") << "] "
<< "[" << expr::attr< severity_level >("Severity") …Run Code Online (Sandbox Code Playgroud) 我正在使用coliru.
命令行是:
g++ -std=c++11 -O2 main.cpp && ./a.out
clang++ -std=c++11 -O2 main.cpp && ./a.out
Run Code Online (Sandbox Code Playgroud)
以下代码在g ++中编译良好,但在clang ++中编译不正确.
template <typename T, typename... U>
A& operator ()(T a, U... b)
{
struct Inner {
operator decltype(T(U...)) const {
return a(b...);
}
} inner;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp:17:37:错误:'U'没有引用值
Run Code Online (Sandbox Code Playgroud)operator decltype(T(U...)) const { ^main.cpp:13:43:注意:在这里宣布
Run Code Online (Sandbox Code Playgroud)template <typename T, typename... U> ^生成1个错误.
我现在得到的错误是:
main.cpp:18:41: error: reference to local variable 'a' declared in enclosing function 'operator()'
Run Code Online (Sandbox Code Playgroud)
我的班级看起来像这样:
template <typename R>
class A { …Run Code Online (Sandbox Code Playgroud) 如何让scons使用Clang和libc ++?在我传递给Environment的任何标志中放入"-stdlib = libc ++"会导致未定义的引用错误,如下所示:
hello.o: In function `main':
hello.cpp:(.text+0xc): undefined reference to `std::__1::cout'
hello.o: In function `std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)':
Run Code Online (Sandbox Code Playgroud) I'm having an interesting problem. I'm compiling a toy compiler with
clang++ -g -x c++ y.tab.c lex.yy.c semantic_actions.cpp -o parser -lfl
Run Code Online (Sandbox Code Playgroud)
In y.tab.c I included semantic_actios.hpp and the contents in semantic_actions.hpp are some method declarations that are used in y.tab.c. That compiles fine.
However if I change it to
clang++ -c -g -x c++ semantic_actions.cpp -o semantic_actions.o
clang++ -g -x c++ y.tab.c lex.yy.c -o parser semantic_actions.o -lfl
Run Code Online (Sandbox Code Playgroud)
I see
semantic_actions.o:1:1: error: source file is not valid UTF-8
<CF><FA><ED><FE><U+0007>
^ …Run Code Online (Sandbox Code Playgroud) 以下代码无法在我的Mac上编译
#include <iostream>
#include <array>
template <typename T, unsigned int N>
using Vector = std::array<T, N>;
template <typename T, unsigned int N>
T dot(const Vector<T, N> &l, const Vector<T, N> &r) {
T result{0};
for (auto i = 0; i < N; ++i) {
result += l[i] * r[i];
}
return result;
}
using Vector3f = Vector<float, 3>;
int main(int argc, const char * argv[]) {
Vector3f u{1.0f, 2.0f, 3.0f};
Vector3f v{6.0f, 5.0f, 4.0f};
std::cout << dot(u, v) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我想使用operator <<来编写具有指定基类型的枚举.令我惊讶的是,似乎我必须自己写出操作员.例如,我想写的代码是
#include <iostream>
enum myenum : uint16_t
{
X = 0,
};
int main ()
{
std::cout << "Value is" << X << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc 4.8和visual studio 2015对此没有任何问题.clang ++ - 3.6错误
# clang++-3.6 -std=c++11 -O0 ostream.cpp -o test.exe
ostream.cpp:18:29: error: use of overloaded operator '<<' is ambiguous (with operand types
'basic_ostream<char, std::char_traits<char> >' and 'myenum')
std::cout << "Value is" << X << std::endl;
~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:181:7: note:
candidate function
operator<<(unsigned short __n)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:189:7: note: …Run Code Online (Sandbox Code Playgroud)