vscode 使用 clang 格式化 c++代码

♀尐吖头ヾ 提交于 2020-10-07 00:51:12

需要ubuntu  安装 clang

vscode 安装 clang-format 插件和 clangd插件

sudo apt-get update

sudo apt-get upgrade -y



curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz | tar -xJC .

# For Ubuntu 18.04: 
# curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz | tar -xJC . 
# For Ubuntu 14.04: 
# curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | tar -xJC .




mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local

# For Ubuntu 18.04: 
mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local

# For Ubuntu 14.04: 
mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-14.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local


add  .bashrc or .zshrc 


export PATH=/usr/local/clang_9.0.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/clang_9.0.0/lib:$LD_LIBRARY_PATH

 

添加路径, 这样就能使用格式化了

 

t.cpp

#include <utility>
#include <string>
#include <iostream>

template<class... T>
auto pack(T... t)
{
    // Now legal in  C++20
    return [...t=std::move(t)](auto&& f){f(t...);};
};

// Forgive me Kate Gregory
void f(int age, const std::string& name )
{
    std::cout <<"I am " << name <<" and am " <<age <<" years old\n";
}
int main(int argc,char** argv )
{
    if ( argc != 3)
    {
        std::cerr << " Provide my age then my name\n";
        return -1;
    }

    int age = std::stoi(argv[1]);
    std::string name =  argv[2];
    auto func = pack(age,name);
    func(f);
    return 0;
}

clang++ -stdlib=libc++ -std=c++2a -Wall trial.cpp -o trial

./trial 25 oguz

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!