C++ setup on Ubuntu

·

2 min read

C++ setup on Ubuntu

hello_friend,

You might already know this, but coding on Linux sometimes can be a little .. demanding. I'm using VSCode since I'm not part of the JetBrains hype train, especially not since the Solarwinds hack. So what we need to do in this case is find out our include path and tell them to VSCode. This really isn't that big of an issue, just fire this console command:

echo | gcc -xc++ -E -v -

You will get an output similar to this:

Now just copy the lines in the lower half of the output, beginning with /usr/include/c++/11. Create a folder .vscode in your project, then just copy this file into it:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/c++/11",
                "/usr/include/x86_64-linux-gnu/c++/11",
                "/usr/include/c++/11/backward",
                "/usr/lib/gcc/x86_64-linux-gnu/11/include",
                "/usr/local/include",
                "/usr/include/x86_64-linux-gnu",
                "/usr/include"
            ],
            "defines": [],
            "cStandard": "c17",
            "cppStandard": "c++14",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

This should normally suffice. Now you can start coding in VSCode without it always crying for include paths and unknown types. Your project folder should now look something like this:

Even better is creating a makefile for your program. Many new programmers intuitively shy away from makefiles, but for somebody using your program, it's often a true blessing. If you are interested in this topic, here's a simple makefile I'm using for the Multithread drill in one of my articles:

CXX = g++
CXXFLAGS = -std=c++11 -Wall

# Source files
SRC_FILES = src/Parrot.cpp src/Foodbowl.cpp src/Toybox.cpp src/BirdBath.cpp main.cpp

# Object files
OBJ_FILES = obj/Parrot.o obj/Foodbowl.o obj/Toybox.o obj/BirdBath.o obj/main.o

# Target executable
TARGET = bin/parrotparty

all: $(TARGET)

$(TARGET): $(OBJ_FILES)
    $(CXX) $(CXXFLAGS) -o $@ $^

obj/Parrot.o: src/Parrot.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

obj/Toybox.o: src/Toybox.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

obj/BirdBath.o: src/BirdBath.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

obj/Foodbowl.o: src/Foodbowl.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

obj/main.o: main.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

clean:
    rm -f $(OBJ_FILES) $(TARGET)

.PHONY: all clean

Whenever you add a new class to your program, just place it next to the others in the list at the beginning, and also add it to the obj files below. If you like, you can further slim down the file by using some black bash magix, but I like to have it in this form to make sure I did not forget a file.