标签: clang++

铿锵"你好,世界!" Windows中的链接错误

我刚下载了CLang源代码,使用CMake创建了一个Visual C++ 10 IDE工作区,并构建了Visual C++ 10.0(express)中的所有内容.

现在我在hello world上遇到了一堆链接器错误:

d:\dev\test> type con >foo.cpp
#include <iostream>
using namespace std;
int main() { cout << "Hello, cling-clong world!" << endl; }
^Z

d:\dev\test> clang++ foo.cpp
foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main
foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev
foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry
D2Ev
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function …

c++ std clang linker-errors clang++

11
推荐指数
1
解决办法
7130
查看次数

模板类的模板化构造函数的显式实例化

我不确定它是否是Clang 3.2中的错误或违反C++ 03,但似乎模板类的模板化构造函数的显式实例化失败,但模板类的模板化成员函数的显式实例化成功.

例如,以下编译没有clang ++和g ++的问题:

template<typename T>
class Foo
{
public:
    template<typename S>
    void Bar( const Foo<S>& foo )
    { }
};
template class Foo<int>;
template class Foo<float>;

template void Foo<int>::Bar( const Foo<int>& foo );
template void Foo<int>::Bar( const Foo<float>& foo );
template void Foo<float>::Bar( const Foo<int>& foo );
template void Foo<float>::Bar( const Foo<float>& foo );
Run Code Online (Sandbox Code Playgroud)

而以下编译没有使用g ++警告但使用clang ++失败:

template<typename T>
class Foo
{
public:
    template<typename S>
    Foo( const Foo<S>& foo )
    { }
};
template class Foo<int>;
template …
Run Code Online (Sandbox Code Playgroud)

c++ clang explicit-instantiation clang++

11
推荐指数
1
解决办法
2711
查看次数

clang/g ++与朋友功能的区别

为什么下面的代码用g ++编译好但是在clang上得到错误?

#include <iostream>

class Object {};

class Print
{
public:
    template <typename CharT>
    inline friend std::basic_ostream<CharT> & operator<<(std::basic_ostream<CharT> & out, const Object&)
    {
        return (out << "object");
    }
    static void f( const Object& str )
    {
        std::cout << str;
    }
};

int main()
{
    std::cout << Object() << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

证明链接:g ++/clang ++

当我将友元函数移动到全局命名空间时,为两个编译器编译好的代码(clang ++/g ++).

在这种情况下,哪种实现更兼容C++ Standart?

c++ g++ friend-function name-lookup clang++

11
推荐指数
1
解决办法
795
查看次数

如何让clang在没有颜色的情况下转储AST?

使用clang-check转储源代码的AST,可以用下面的命令来完成:

$ clang-check -ast-dump file.c --
Run Code Online (Sandbox Code Playgroud)

但是,此命令的输出将在终端内显示为彩色.
当我将输出定向到文件时,我会遇到所有颜色转义码:

$ clang-check -ast-dump file.c -- > out.txt  
Run Code Online (Sandbox Code Playgroud)

例:

[0;1;32mTranslationUnitDecl[0m[0;33m 0x227c5c0[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x227cac0[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __int128_t[0m [0;32m'__int128'[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x227cb20[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __uint128_t[0m [0;32m'unsigned __int128'[0m
[0;34m|-[0m[0;1;32mTypedefDecl[0m[0;33m 0x227ce70[0m <[0;33m<invalid sloc>[0m> [0;33m<invalid sloc>[0m implicit[0;1;36m __builtin_va_list[0m [0;32m'__va_list_tag [1]'[0m
...
Run Code Online (Sandbox Code Playgroud)

在clang-check中是否有禁用颜色的标志?
我尝试添加以下标志,但它不起作用:

--extra-arg="--no-color-diagnostics"
Run Code Online (Sandbox Code Playgroud)

clang abstract-syntax-tree ansi-colors clang++

11
推荐指数
1
解决办法
5564
查看次数

constexpr上下文中std :: array指针的size()

假设我的功能如下:

int test(std::array<char, 8>* data) {
  char buffer[data->size() * 2];

  [... some code ...]
}
Run Code Online (Sandbox Code Playgroud)

显然,可以在编译时评估缓冲区的大小:数据有一个 constexpr大小为8个元素,8*2 = 16个字节.

然而,编译时-Wall,-pedantic-std=c++11我得到的臭名昭著的错误:

警告:可变长度数组是C99功能[-Wvla-extension]

我相信它是有道理的:array::size()constexpr,但它仍然是一种方法,在上面的函数中我们仍然必须取消引用指针,而不是constexpr.

如果我尝试类似的东西:

int test(std::array<char, 8>& data) {
  char buffer[data.size() * 2];
  [...]
}
Run Code Online (Sandbox Code Playgroud)

gcc (尝试过5.2.0版)似乎很高兴:没有警告.

但是对于clang++(3.5.1),我仍然会收到一个抱怨可变长度数组的警告.

在我的情况下,我不能轻易改变签名test,它必须采取指针.那么......几个问题:

  1. 在constexpr上下文中获取std::array 指针大小的最佳/最标准方法是什么?

  2. 预期指针与引用的行为有何不同?哪个编译器对警告是正确的,gcc或者clang

c++ gcc constant-expression c++11 clang++

11
推荐指数
1
解决办法
647
查看次数

Rcpp和默认的C++编译器

我对Rcpp有一些奇怪的麻烦 - 它使用了不可预测的C++编译器.这个问题有点类似于这个问题.
我在OSX上,我有2个编译器 - 默认clang和openmp clang-omp支持.我也有以下~/.R/Makevars文件(我设置clang-omp为默认编译器):

CC = clang-omp
CXX = clang-omp ++
CFLAGS + = -O3 -Wall -pipe -pedantic -std = gnu99
CXXFLAGS + = -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp

问题是,我正在开发的软件包编译而clang++不是clang-omp++.我也尝试过(作为实验来解决问题)更改包src/Makevars和设置CXX=clang-omp++以及修改后的$R_HOME/etc/Makeconf CXX条目CXX = clang-omp++.没有运气 - 它仍然可以编译clang++.不知道为什么会这样.

这里也是小的可重现的(来自控制台R和来自Rstudio)的例子(不知道它是否与上面的问题有关).假设2个非常相似的cpp函数:
1.

#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
  return x * 2;
} …
Run Code Online (Sandbox Code Playgroud)

macos r rcpp clang++

11
推荐指数
1
解决办法
4349
查看次数

变得铿锵有力地修复头文件

我正在将当前使用gcc编译的项目移动到clang,并且有一堆gcc没有生成的警告(-Winconsistent-missing-override).clang-tidy用于修复*.cpp文件中的这些错误,但它不会触及hpp文件,因为在数据库中找不到编译命令(正如我所料).

我正在使用ninja构建项目并ninja -t compdb cc cxx > .build/compile_commands.json生成编译数据库.我试过跑:

clang-tidy-3.6 -p .build/      \
      $(find src/ -name *.cpp) \
      $(find src/ -name *.hpp) \
      --checks=misc-use-override --fix
Run Code Online (Sandbox Code Playgroud)

修复错误.它拒绝触摸头文件抱怨:

Skipping .../src/header/file.hpp. Compile command not found.
Run Code Online (Sandbox Code Playgroud)

c++ automated-refactoring llvm-clang clang++ clang-tidy

11
推荐指数
1
解决办法
1万
查看次数

在clang中矢量化一个函数

我正在尝试使用clang根据此clang参考向量化以下函数.它需要一个字节数组的向量,并根据此RFC应用掩码.

static void apply_mask(vector<uint8_t> &payload, uint8_t (&masking_key)[4]) {
  #pragma clang loop vectorize(enable) interleave(enable)
  for (size_t i = 0; i < payload.size(); i++) {
    payload[i] = payload[i] ^ masking_key[i % 4];
  }
}
Run Code Online (Sandbox Code Playgroud)

以下标志传递给clang:

-O3
-Rpass=loop-vectorize
-Rpass-analysis=loop-vectorize
Run Code Online (Sandbox Code Playgroud)

但是,矢量化失败并出现以下错误:

WebSocket.cpp:5:
WebSocket.h:14:
In file included from boost/asio/io_service.hpp:767:
In file included from boost/asio/impl/io_service.hpp:19:
In file included from boost/asio/detail/service_registry.hpp:143:
In file included from boost/asio/detail/impl/service_registry.ipp:19:
c++/v1/vector:1498:18: remark: loop not vectorized: could not determine number
      of loop iterations [-Rpass-analysis]
    return this->__begin_[__n];
                 ^
c++/v1/vector:1498:18: …
Run Code Online (Sandbox Code Playgroud)

c++ vector simd clang++

11
推荐指数
1
解决办法
2948
查看次数

警告:不建议使用隐式复制构造函数的定义

我在C++ 11代码中有一个警告,我想正确修复,但我真的不知道如何.我创建了自己的异常类,派生自std::runtime_error:

class MyError : public std::runtime_error
{
public:
    MyError(const std::string& str, const std::string& message)
      : std::runtime_error(message),
        str_(str)
    { }

    virtual ~MyError()
    { }

    std::string getStr() const
    {
        return str_;
    }

  private:
      std::string str_;
};
Run Code Online (Sandbox Code Playgroud)

当我使用clang-cl编译该代码时,/Wall我得到以下警告:

warning: definition of implicit copy constructor for 'MyError' is deprecated 
         because it has a user-declared destructor [-Wdeprecated]
Run Code Online (Sandbox Code Playgroud)

因为我已经定义了一个析构函数,MyError所以不会生成复制构造函数MyError.我不完全明白这是否会引起任何问题......

现在我可以通过简单地删除虚拟析构函数来消除该警告,但我总是认为如果基类(在本例中std::runtime_error)具有虚拟析构函数,派生类应该具有虚拟析构函数.

因此,我想最好不要删除虚拟析构函数,而是定义复制构造函数.但是如果我需要定义复制构造函数,也许我还应该定义复制赋值运算符和移动构造函数以及移动赋值运算符.但这对我的简单异常类似乎有点过分了!?

任何想法如何最好地解决这个问题?

c++ visual-c++ clang++

11
推荐指数
3
解决办法
5542
查看次数

GCC 编译器产生错误:成员函数的无效使用,而 CLang 编译器则不会

我正在使用以下程序:

\n

在main函数中,我想打印poll_timer函数的地址。

\n

该程序使用 clang 可以成功编译并运行,但使用 GCC 则不能。

\n

我在使用 GCC 时收到以下错误

\n
"709568706/source.cpp: In function \xe2\x80\x98int main()\xe2\x80\x99:\n709568706/source.cpp:28:32: error: invalid use of member function \xe2\x80\x98static void MessagePoller::poll_timer()\xe2\x80\x99 (did you forget the \xe2\x80\x98()\xe2\x80\x99 ?)\n     std::cout << (void*)m_sut->poll_timer << std::endl;\n                         ~~~~~~~^~~~~~~~~~"\n
Run Code Online (Sandbox Code Playgroud)\n
#include <iostream>\n#include <memory>\n\nclass MessagePoller\n{\n  protected:\n    static void poll_timer()\n    {\n        std::cout << "Poll timer Base called\\n";\n    }\n};\n\nclass TestMessagePoller : public MessagePoller\n{\npublic:\n    using MessagePoller::poll_timer;\n\n};\ntypedef std::shared_ptr<TestMessagePoller> TestMessagePollerPtr;\n\nint main()\n{   \n    TestMessagePollerPtr m_sut;\n    m_sut = TestMessagePollerPtr(new TestMessagePoller());\n\n    std::cout << "HERE1\\n";\n    m_sut->poll_timer();\n    std::cout << (void*)m_sut->poll_timer …
Run Code Online (Sandbox Code Playgroud)

c++ gcc compiler-specific clang++

11
推荐指数
1
解决办法
428
查看次数