This repository contains reusable snippets of C++ code to implement various tasks such as generating random numbers or working with dates.
The repository also serves as a quick reminder for one who has not been working with C++ for a while.
The most important directory is snippets
.
This is a CMake project and can be opened as such in Microsoft Visual Studio or JetBrains CLion.
./configure --prefix=/usr/local/libname-version
make
make install
To run an executable built against a third-party library:
LD_LIBRARY_PATH=/usr/local/name-version/lib/ exe-name
Linking against a static .a
library:
g++ source.cpp /path/to/libmy.a
Linking against a shared library in a standard location:
# against libm.so
g++ source.cpp -lm
Linking against a custom shared library:
# against libX11.so
g++ source.cpp -L/path/to/libs/ -lX11
The list of standard locations to search for libraries is
in /etc/ld.so.conf
. If this file is modified, ldconfig
needs to be run to process the changes.
To list all functions in a library or compiled executable, run
nm library.a
To see which shared libraries are required by an executable, run
ldd my_app
git submodule add https://github.com/google/googletest.git
git submodule init
git submodule update
A single-file app or a test case can easily be run in VS Code with the "Code Runner" plugin. The plugin needs the executor map to be configured for C++, as follows.
On Windows, using VC++ compiler:
"code-runner.executorMap": {
"cpp": "\"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\" && cd $dir && cl $fileName /std:c++20 /nologo /EHsc /W4 /Fe:$fileNameWithoutExt.exe && $dir$fileNameWithoutExt.exe && DEL $dir$fileNameWithoutExt.exe && DEL $dir$fileNameWithoutExt.obj"
}
On Windows, using mingw:
"code-runner.executorMap": {
"cpp": "g++ -std=c++20 $fullFileName -o $fileNameWithoutExt && $fileNameWithoutExt.exe && DEL $fileNameWithoutExt.exe"
}
On Linux, using GCC:
"code-runner.executorMap": {
"cpp": "g++ -lstdc++ --std=c++17 $fullFileName && ./a.out && rm ./a.out"
}