Skip to content

Instantly share code, notes, and snippets.

@tapyu
Last active June 28, 2024 17:56
Show Gist options
  • Save tapyu/8bced1527e3fdc89e6ab7f4023643000 to your computer and use it in GitHub Desktop.
Save tapyu/8bced1527e3fdc89e6ab7f4023643000 to your computer and use it in GitHub Desktop.
C/C++-related files for development in Vscode

C/C++-related files for development in Vscode

  • VScode files (they go in .vscode/) [1] [2]:
    • lauch.json: debugging configurations.
    • taks.json: create task to build the C/C++ project.
    • c_cpp_properties.json: settings related to IntelliSense.
  • .clang-format: rules for how your code should be formatted, such as indentation, line length, spacing, and so on. It is based on a predefined style to base the formatting on (in my case, I use the Google style).
  • Simple Makefile for a basic build system template.
  • Instructions to analyze a C/C++ code performance.

TODO: To create comments for documentation, use Doxygen. It is very powerful and not so minimalist, so it might be tricky to get started. For documentation generated in the source code.

Language: Cpp
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: Empty
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlignTrailingComments: true
ColumnLimit: 80
DerivePointerAlignment: false
LambdaBodyIndentation: OuterScope
// file is specific to the C/C++ extension for VS Code. It is used
// to configure settings related to IntelliSense, providing
// - include paths and code navigation
// - error checking
// - and other language-specific settings.
// It is primarily focused on setting up the environment for editing
// and understanding the code, rather than building or debugging it.
// Note that some configs (e.g., `intelliSenseMode` or the C/C++
// standard) can be set in `settings.json`, whether in local (in
// `.vscode/`) or in user settings (in ~/.config/Code/).
// When there are conflicting settings between `settings.json` and
// `c_cpp_properties.json`, Visual Studio Code (VSCode) will prioritize
// the settings in `c_cpp_properties.json` over those in `settings.json`
// for IntelliSense-related configurations. Therefore, use this file
// for fine tweaking.
// NOTE: https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference
{
"configurations": [
{
"name": "My_C/C++_Linux_cfg", // used to differentiate between multiple configurations within a single project or workspace. If you have only one, this doens't make a relevant differece. You can see which config is select in the status bar
"includePath": [
"${workspaceFolder}/**", // An include path is a folder that contains header files
"/usr/include", // (such as `#include "myHeaderFile.h"`) that are included
"/usr/include/poppler" // in a source file. Add other paths here
],
"defines": [], // used to set preprocessor macros that will be applied during IntelliSense parsing. This helps the C/C++ extension understand your code better and provide accurate code completion, navigation, and error checking (?)
"compilerPath": "/usr/bin/g++", // (optional) The full path to the compiler you use to build your project to enable more accurate IntelliSense
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4 // don't edit this field. It tracks the current version. If it is wrongly changed or deleted, this file is overwritten with a minimal snippet configuration whenever it is saved
}

A profiler is a performance analysis tool used in software development. It measures the behavior of a program by collecting data on various aspects such as execution time, memory usage, and CPU usage. Profilers help developers identify performance bottlenecks and optimize their code. There are several types of profilers, each focusing on different aspects of performance:

  • CPU Profiler: Measures which parts of the code are consuming the most CPU time. This helps in identifying functions or methods that are slowing down the execution of the program.
  • Memory Profiler: Tracks memory usage of the application, identifying memory leaks and inefficient memory allocation that can lead to excessive memory consumption.
  • I/O Profiler: Monitors input/output operations, such as file reads and writes, and network activity, helping to identify slow I/O operations that may be impacting performance.
  • Concurrency Profiler: Analyzes multi-threaded or parallel applications, identifying issues such as thread contention, deadlocks, and suboptimal thread usage.

Profilers are essential tools for performance analysis and optimization in software development. They provide detailed insights into various performance aspects, helping developers improve the efficiency and effectiveness of their applications. Converting profiler output into dot graphs can aid in the visual analysis of performance data, making it easier to understand and address performance issues.

Example Profilers

  • gprof: A GNU profiler that produces a flat profile and a call graph, showing the time spent in each function and the call hierarchy.
  • Valgrind: A memory profiler that can detect memory leaks, memory corruption, and other memory-related issues.
  • perf: A performance analysis tool for Linux that can profile CPU, cache usage, and other hardware events.
  • VisualVM: A visual tool for profiling Java applications, providing insights into CPU and memory usage, thread activity, and more.
  • Instruments: A profiling tool for macOS and iOS applications, part of Xcode, that provides a variety of performance metrics.

Using gprof and gprof2dot to obtain plots

  1. Install gprof2dot
    pip install gprof2dot
    
  2. Make sure certain flags are disabled
    CXXFLAGS += -pg # Generate extra code to write profile information suitable for the analysis program prof (for -p) or gprof (for -pg)
    #CXXFLAGS += -O3 -fno-stack-protector # we don't want any optimization
  3. Compile the program with whatever build system tool you are using.
    make
  4. Execute the program as you normally would. This will generate a profiling data file (typically named gmon.out) in the current directory.
    /path/to/executable
  5. Use gprof profiler to analyze the gmon.out file generated in the previous step along with the executable.
    gprof /path/to/executable > flat_profile.txt
  6. Obtain the graph plot from the profile performance
    \cat flat_profile.txt | gprof2dot | dot -Tpng -o output.png 

The final result is something like

Utilize sanitize flags to detect issues (memory errors, undefined behavior, thread race conditions, etc.)

  1. Set/unset flags to Makefile
    #CXXFLAGS += -O3 -fno-stack-protector
    # CXXFLAGS += -pg
    CXXFLAGS += -ggdb3
    CXXFLAGS += -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=null -fno-sanitize=alignment -fsanitize-address-use-after-scope
  2. Run the executable with these flags.

Use Valgrind: A memory profiler that can detect memory leaks, memory corruption, and other memory-related issues.

  1. Download Valgrind
  2. Set/unset flags to Makefile
    #CXXFLAGS += -O3 -fno-stack-protector
    # CXXFLAGS += -pg
    CXXFLAGS += -ggdb3
  3. Run valgrind along with some imporant flags
    valgrind --leak-check=full --track-origins=yes /path/to/executable --flags --of --the --executable
    
// used to define debugging configurations. These configurations
// specify how the debugger should launch and interact with your
// application. Each configuration includes settings for the
// debugger type, the program to debug, and any arguments or
// environment variables needed.
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ debug in Vscode", // configures how VS Code will launch the debugger for your application
"type": "cppdbg",
"request": "launch", // Specifies the type of request. launch means to start a new debug session.
"program": "${workspaceFolder}/main", //path to your program
"args": [], // array of command-line arguments to pass to the program when it starts
"stopAtEntry": false,
"cwd": "${workspaceFolder}", // current directory where the program in launch
"environment": [], // array of environment variables to set for the debug session
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
# basic Makefile for building simple C or C++ projects.
# Phony targets
.PHONY: all clean
# Executable name
EXEC = main
# Compilers to use
CC = gcc
CXX = g++
# Compiler flags
CFLAGS = -std=c11
CFLAGS += -Wall -Wextra -Wcast-qual -Wmissing-prototypes -Wshadow
CFLAGS += -pedantic
# CFLAGS += -ggdb3 # only for debugging purpose
CXXFLAGS = -std=c++11
CXXFLAGS += -Wall -Wextra -Wcast-qual -Wmissing-prototypes -Wshadow
CXXFLAGS += -pedantic
# CXXFLAGS += -ggdb3 # only for debugging purpose
# Source files
CSRCS = $(wildcard *.c)
CXXSRCS = $(wildcard *.cpp)
# Object files
COBJS = $(CSRCS:.c=.o)
CXXOBJS = $(CXXSRCS:.cpp=.o)
# Default target
all: $(EXEC)
# Rule to link the object files to create the executable
$(EXEC): $(COBJS) $(CXXOBJS)
@if [ -n "$(CSRCS)" -a -z "$(CXXSRCS)" ]; then \
echo $(CC) $(COBJS) -o $(EXEC); \
$(CC) $(COBJS) -o $(EXEC); \
elif [ -z "$(CSRCS)" -a -n "$(CXXSRCS)" ]; then \
echo $(CXX) $(CXXOBJS) -o $(EXEC); \
$(CXX) $(CXXOBJS) -o $(EXEC); \
elif [ -n "$(CSRCS)" -a -n "$(CXXSRCS)" ]; then \
echo "Error: Mixing C and C++ source files is not supported."; \
exit 1; \
else \
echo "No source files found."; \
exit 1; \
fi
# Rule to compile C source files into object files
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
# Rule to compile C++ source files into object files
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
# Rule to clean up the build files
clean:
rm -f $(COBJS) $(CXXOBJS) $(EXEC)
// The tasks.json file is used to define tasks that can be executed in VS Code.
// These tasks can be anything from building your project to running tests or
// cleaning up build artifacts.
// You can execute tasks defined in tasks.json directly from the Command Palette
// (Ctrl+Shift+P) by typing "Run Task" and selecting the task you want to
// execute. For example, you can run the build task to compile your project.
// This file defines a build task to compile your C/C++. If this task is the
// default one, you can press `ctrl+shift+B` to run it. Concerning how to
// build your C/C++ project, it really depends on the developer tastes and
// the project complexity. Toy projects can build it by directly running
// the compiler (`gcc` or `g++`), but real projects usualy relies on build
// systems, such as `Makefile` and `CMake`.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build my C/C++ project", // when pressing Ctrl+Shift+P and selecting `Run Task`, this name will appear
"type": "shell",
"command": "make", // Change if want to use another approach
"group": {
"kind": "build", // Indicates that this task is associated with the build process (we can have other kind of tasks, such as test, cleaning, etc.)
"isDefault": true
},
"problemMatcher": { // VS Code can process the output from a task with a problem matcher. Problem matchers scan the task output text for known warning or error strings, and report these inline in the editor and in the Problems panel.
"owner": "cpp", // helps Visual Studio Code organize and present errors and warnings in a relevant context to the user. For example, if you're working on a C/C++ project and the problem matcher's owner is set to "cpp", VSCode will associate errors and warnings detected by this problem matcher with C/C++ files. "Associating" errors and warnings with a specific programming language or file type allows Visual Studio Code to provide targeted assistance and support tailored to the user's current development context
"fileLocation": ["relative", "${workspaceFolder}"], // specifies that file paths in error messages are relative to the workspace folder.
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
]
}
// NOTE: https://stackoverflow.com/questions/58581500/how-to-fix-debugger-in-vscode-if-you-have-makefile-project-on-c
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment