Search code examples
c++gccsublimetext3sublimetextc++20

How to build a system for C++20 on Sublime Text?


I'm trying to run c++20 using the command + B key combination, not yet succeeded. Also tried to add -std=gnu++20, it did not work. How do I do that?


Currently, my system works with c++17 and here is the config build-system:

{
    "cmd": ["g++", "-std=c++17", "-o", "${file_path}/${file_base_name}", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "quiet": true,

    "variants": [
        {
            "name": "Run with C++20",
            "cmd": ["bash", "-c", "g++20 '${file}' -std=c++20 -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        },
        {
            "name": "Run with C++17",
            "cmd": ["bash", "-c", "g++ '${file}' -std=c++17 -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]

        },
        {
            "name": "Build without C++11",
            "cmd": ["g++ '${file}' -o '${file_path}/${file_base_name}'"]
        },

        {
            "name": "Run",
            "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }


    ]
}

Basic Test:

#include <iostream>
#include <string_view>
#include <string>

template <typename PrefixType>
void test_prefix_print(const std::string& str, PrefixType prefix) {
    std::cout << '\'' << str << "' starts with '" << prefix << "': " <<
              str.starts_with(prefix) << '\n';
}

int main() {
    std::boolalpha(std::cout);
    auto helloWorld = std::string("hello world");

    test_prefix_print(helloWorld, std::string_view("hello"));

    test_prefix_print(helloWorld, std::string_view("goodbye"));

    test_prefix_print(helloWorld, 'h');

    test_prefix_print(helloWorld, 'x');
}

Important:

I have the gcc 10.2.0 is already installed and up-to-date using brew. I think I have to point that in the config file. How do I do that?

Reference:

C++20 Support in GCC


Solution

  • You made a small typo. The executable is still g++, you just need to add either -std=c++20, -std=c++2a, or -std=gnu++20, depending on which features/extensions you want to take advantage of. So, change the relevant lines in your build system to this:

    "name": "Run with C++20",
    "cmd": ["bash", "-c", "g++ '${file}' -std=c++2a -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]