MushcatShiro's Blog

This is a page about »Cpp Setup«.

Windows C++ Development Setup with VS Code

Install c++ with MinGW-w64 tool chain via MSYS2. If SFML is used, it requires specific GCC provided in the download page. Take note of the PATH to g++.exe.

In the project folder, create three directories (or four if one wish to put mingw64 within the project), bin, include and lib. bin is for binaries, usually .dll files together with the build executable. include is for header files. Usually each library/project will have its own folder e.g. GL/*.h. lib is for static libraries that contain compiled object code and directly linked to executable during build process. .lib (windows) and .a (unix-like systems) will be part of the final application.

static library can be created by g++ -c source_file.cpp and librarian tool ar (unix) or lib.exe can be used to combine object files to single .a or .lib file.

Static library is usually for reusable function or classes bundled for other projects. It is often done early in the development of shared component. Static linking is straightforward and doesnt requries distributing a separate file with the main application.

dynamic files are created by g++ -fPIC source_file.cpp

Dynamic libraries is loaded during runtime. They are often code that used by multiple application simultaneously, reducing final application size and reducing the need of recompiling the entire application that uses it.

VS Config/Extensions

Extensions: C/C++ (Microsoft), C/C++ Extension Pack (Microsoft), C/C++ Themes (Microsoft), CMake Tools (Microsoft). Suspect the latter two comes with the prior two.

 1// c_cpp_pproperties.json
 2{
 3  "configurations": [
 4    {
 5      "name": "Win32",
 6      "includePath": [
 7        "${workspaceFolder}/**",
 8        "${workspaceFolder}/include"
 9      ],
10      "defines": [
11        "_DEBUG",
12        "UNICODE",
13        "_UNICODE"
14      ],
15      "compilerPath": "E:\\mingw64\\bin\\g++.exe",  // example windows path
16      "cStandard": "c17",
17      "cppStandard": "c++17",
18      "intelliSenseMode": "windows-gcc-x64"
19    }
20  ],
21  "version": 4
22}

does the job for now, c/cppStandard etc. doesn’t really matter

Build Without CMake

VS Code tasks allows user to define simple builds

 1// tasks.json
 2{
 3  "version": "2.0.0",
 4  "tasks": [
 5    {
 6      "label": "build with g++",
 7      "type": "shell",
 8      "command": "E:\\mingw64\\bin\\g++.exe",  // PATH to g++!
 9      "args": [
10        "${file}",
11        "-o",
12        "${workspaceFolder}\\bin\\main.exe",
13        "-I${workspaceFolder}\\include",
14        "-L${workspaceFolder}\\lib",
15        "-lsfml-graphics",  // link stuffs here
16      ],
17      "group": {
18        "kind": "build",
19        "isDefault": true
20      },
21      "problemMatcher": [
22        "$gcc"
23      ],
24      "detail": "Build the active file with g++."
25    }
26  ]
27}

Bring up VS Code command pallete - Tasks: Run Task - find task label.

MacOS

#C++