原文链接: vscode 使用 clang 格式化 c++代码
需要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