Reputation: 41
I’m new to Linux and nvim
and have heard a lot of great things about the Kickstart.nvim
configuration by tj. I’m learning C++ and trying to configure the compiler to use --std=c++20
by default, but I’m having trouble getting it to work.
I don’t want to manually add flags every time I compile, and I would prefer a setup where I don't need to specify the flag each time. I’ve tried searching and also used ChatGPT to help me understand how to set it up, but I’m still confused. I also tried setting the --std=c++20
flag globally, but that didn’t seem to work either.
Could anyone guide me on how to set up my nvim
and Kickstart.nvim configuration to automatically use --std=c++20
when compiling C++ code?
Thanks in advance for your help!
Upvotes: 4
Views: 72
Reputation: 11
I'm not entirely sure what you mean by "set up nvim to automatically use --std=c++20," but here are a few common approaches for compiling C++ code with that flag by default:
Using a Build Script: Create a build script (e.g., build.sh) that contains your compile command. For example:
#!/usr/bin/env zsh
set -xe
clang++ --std=c++20 -Wall -Wextra -g main.cpp -o main
Make sure the script is executable (chmod +x build.sh). Then, run it with:
./build.sh
Using a Shell Function: Alternatively, you can define a shell function in your configuration (e.g., in your .zshrc file):
runcpp() {
if [ "$#" -ne 2 ]; then
echo "Usage: runcpp <source_file> <output_file>"
return 1
fi
clang++ -Wall -Wextra -Wpedantic -Werror -std=c++20 -g "$1" -o "$2"
}
After adding this function and reloading your shell, compile your code by running:
runcpp filename.cpp compiledFileName
Then, execute your compiled program with:
./compiledFileName
Integrating with Neovim: You can also add a custom command in your keymaps.lua file to build the current C++ file. For example:
vim.api.nvim_create_user_command("BuildCpp", function()
vim.cmd("!clang++ -Wall -Wextra -Wpedantic -Werror -std=c++20 -g % -o %:r")
end, { desc = "Compile current C++ file with C++20 standard" })
This creates a new Neovim command :BuildCpp that runs the compile command in the terminal. Here, % refers to the current file name, and %:r is the current file name without its extension (e.g., "myfile.cpp" becomes "myfile").
For larger projects, you might consider using a build system like Make or CMake, but these simpler methods should work well as you're starting out.
Upvotes: 1