Tuesday, October 23, 2018

Compile SFML app with VS Code on Linux

As official tutorial says, there are 3 ways to install SFML on Linux:
  1. Install it directly from your distribution's package repository
  2. Get the source code, build it and install it
  3. Download the precompiled SDK and manually copy the files
On Ubuntu 18.04, $ apt search sfml reports that the latest version in repository is 2.4.2, while actually the latest version is 2.5.1. So the first option might not be the best. Third option is probably the worst, because precompiled SDK was most likely compiled with different version of GCC.
So that makes the second option the best way to go. After SFML is installed, it's time to compile SFML app. Here's an example of VS Code configuration files:

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/path/to/SFML/SFML-master/include/"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}


launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/sfml-app",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Skip raise.c files",
                    "text": "-interpreter-exec console \"skip -gfi /build/glibc-OTsEL5/glibc-2.27/**/*\""
                 }
            ],
            "preLaunchTask": "build"
        }
    ]
}


tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "compile",
            "type": "shell",
            "presentation": {
                "reveal": "never",
                "panel": "shared",
            },
            "command": "g++",
            "args": [
                "-g",
                "-c",
                "main.cpp"
            ]
        },
        {
            "label": "build",
            "type": "shell",
            "presentation": {
                "reveal": "never",
                "panel": "shared",
            },
            "command": "g++",
            "args": [
                "main.o",
                "-o",
                "sfml-app",
                "-lsfml-graphics",
                "-lsfml-window",
                "-lsfml-system"
            ],
            "dependsOn": ["compile"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}