AlderAutomation

Cmake and Neovim Intro

Last updated: 4 days, 10 hours ago

Intro

I haven't been programming for almost a year (shame!) because I have been stuck playing WoW Classic. And now that I am back to programming, I have noticed that I can't recall certain things, like how to use cmake. I am going to re-learn it ... again. Hopefully this time it sticks. I really have a hard time retaining information on cmake.

According to research, this guide by Henry Schreiner is the best one to work from.

Notes

I am going to be performing all these steps on a fresh Fedora installation so that I can document all the steps properly.

I am going to be using neovim as my editor and I am going to try to stay terminal only for this.... just because I can :)

Installation

Obviously, the first thing we need to do is install c++, cmake, and other tooling.

sudo dnf install gcc-c++ cmake neovim git

And then check to make sure installed:

gcc --version   
g++ --version   
make --version   

Start a Project

First thing I am going to is create a project directory, this can be whatever you want:
mkdir -p ~/CPP_Code/RelearnCMake
the -p will create all child directories that you specify, otherwise you will have to create each one separately.

Next, I am going to create a very basic main file, just to get running and make sure everything is working correctly:
touch ./main.cpp
nvim ./main.cpp
Inside the main file:

#include <iostream>

int main(int argc, char *argv[]) {
    std::cout << "Hello, world" << std::endl; 

    return 0; 
}

Now I want to commit this to git as an init commit. But git needs to be configured first:
git config --global user.email "your@email.com" git config --global user.name "yourName"

Now we can do the commit:
git add . - adds all files in directory to commit
git commit -m "init commit" - commits with the message "init commit"

And then to test that cpp file works:
g++ ./main.cpp -o ./main.o - builds main cpp to o. -o specifies build target and can be pointed at other directories, which we are going to do.
./main.o - This should output to terminal on next line "Hello, World"

CMake Intro

Now that is all working, let's start working on CMake. We want cmake to do all the building for us, because as the project gets bigger the build commands will get bigger and more confusing.

First thing I'm going to do is make a ./src and move main into it:
mkdir ./src && mv ./main.cpp ./src - the "&&" allows stringing commands into one. This is more important in scripting than here in file management but gives me something else to show you :)

Also, go ahead and remove the existing main.o, Cmake will make a new one:
rm ./main.o

Now let's make a CMakeLists.txt:
touch ./CMakeLists.txt - going to do this one in main directory
nvim ./CMakeLists.txt

inside the CMakeLists.txt, we want to add the following lines (you can leave out the comments if you want):

# Min Version. Every CMakeList should have this. can be a range to allow better compatibility  
cmake_minimum_required(VERSION 3.15...4.4)  

# Setting the project. Every top level CMake will have this. 
project(MyProject VERSION 1.0 DESCRIPTION "ReLearning CMake" LANGUAGES CXX)

# tell cmake, when compiling main, use C++23
target_compile_features(main PRIVATE cxx_std_23)

That's a basic cmake, now we can try building and hoping:
cmake -S . -B build - -S is source directory. -B is the build directory
cmake --build build - this builds the actuall c++ app
./build/main - runs app and you should get "Hello, World" again

Backing up a step to the &&, we can string this entire command together and I would put it into a project notes.txt for future referance and copy/pasting. Could also put it into a start up script:
nvim ./startup.sh - create the script file
Add in:

#!/bin/bash

cmake -S . -B build && cmake --build build && ./build/main

chmod +x ./startup.sh - to make it executable.
./startup.sh - to run it. it should do all the things and still print "Hello, World"

Could also do something like: echo "cmake -S . -B build && cmake --build build && ./build/main" >> notes.md that will put the entire command into note.md, if it doesnt exist it will create it. I like this as well so that if I need to run the file I remember how.

NeoVim

Being terminal only and no GUI. I am fighting with flipping in and out of text files as well as running terminal commands. Now I need to look at NeoVim capabilities before I get frustrated and scrap this entire project which I feel coming.

The first thing I want is line numbers. I know I will need them for debugging. We need to edit:
~/.config/nvim/init.lua
If not there, create it.
Then add:
vim.opt.nu = true
While we are in that file lets add some other options:
vim.opt.tabstop = 4 - the number of spaces of a tab
vim.opt.shiftwidth = 4 - also needed for tabbing size

vim.o.cursorline = true - Highlight the current line
vim.o.termguicolors = true - Enable 24-bit RGB colors

Ok, but none of this helps file navigation.

:Explore will allow you to navigate the file system and open other files
While in explore mode:
% creates a new file
d creates a new directory
D deletes a file or directory
R renames
t opens in new tab
v open in vertical split
s open in horizontal split

I didn't like split screening. But tabs seem like a good way to have multiple editors open at once. Use gt to no next tab and gT to go previous tab. I might look into some way to have a visual representation of which tabs are open.

To use the terminal:
:terminal - but be sure to press i to enter insert mode once in terminal, weird but oh well.
To Leave terminal:
ctrl + \ ctrl+n and then ctrl o - why? I don't know. It seems way too hard, but it works. I noticed that can also type exit in that terminal, but it also closes the file you were in when you opened the terminal.

Conclusion

This has taken so long to get this far; it was a lot of fun though, and I hope you enjoyed it as well. I am now going to go mess around with this setup to see if I can start building a tool for an app called NovelWriter. I want to be able to do some more in-depth analysis of the story I am writing. I'll do more updates soon, but I really need to get back to Pillage and Plunder. Today I was supposed to be working on that, but I couldn't get this out of my head.

If you want me to elaborate on anything, please message me on Discord, Mastodon, X, or email me. I’ll be happy to go into more detail one-on-one or to create more posts.

noaibadge

#c++ #cmake #neovim #programming #tutorial