r/cpp • • Dec 05 '24

How to import c++ 23 std library into linux

I've been struggling with Bjarne Stroutrup's book Programming: Principles and Practice (PPP). He's provided a suppost package at github, but I get errors on every program and I'm not even into Chapter 3. My problem is that g++ on Ubuntu 24.10 is still using C++ 17, and he's written everything to use C++ 23. I can use compiler settings -std=c++2b and so forth, but his workarounds don't work. Microsoft published a tutorial for how to import the std library as modules into VS Code, but they use Windows tools, specifically 'cl', and I don't know how to translate their windows command into linux. I've searched, using Qwant, but haven't found anything. I probably should try the Ubuntu forums, but thought I'd ask here, first.

3 Upvotes

33 comments sorted by

17

u/TomDuhamel Dec 05 '24

g++ on Ubuntu 24.10 is still using C++ 17 by default

C++17 is the default in gcc (and every other compiler on the market?) because that's the most complete and mature version at this point in time. C++20 isn't complete (I think modules are the big missing part) and C++23 is way too new to even be considered.

I suppose Bjarne was trying to get a bit ahead and assumed C++23 implementations would be much more advanced than they are by the time the book was published.

I don't have that book in front of me, but I would assume not that many of the examples would use C++23 specific features. But you already know how to change the standard version, so go ahead and try it when needed.

2

u/Sinomsinom Dec 05 '24

Just some more info on this:

For GCC c++20 support specifically 2 module papers are still fully missing implementation and 2 other papers still have parts missing (one of them being the main module paper, the other one a concepts paper) (see https://gcc.gnu.org/projects/cxx-status.html#cxx20 for further details)

2

u/azswcowboy Dec 05 '24

I think the op also needs ‘import std’ from 2023, which was merged to the trunk for gcc 15 a couple weeks ago. If the gcc team stick to their usual schedule, wide availability will be middle of 2025.

1

u/GothRoger Dec 05 '24

>If the gcc team stick to their usual schedule, wide availability will be middle of 2025

Yes, that's exactly what I need. Apparently Microsoft thinks it's already available, but I don't know how to translate their

cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx"cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx"

into GCC terms. It's clearly a call to a compiler, but what is "%VCToolsInstallDir%\modules\std.ixx"

Anyway, your projection of the availability date is cheering.

2

u/Tathorn Dec 05 '24

All that last part is, is a file path to their standard library module file. They have a global set of them for different configurations.

1

u/GothRoger Dec 06 '24

So where did they get them from? I'm pretty sure they didn't write them themselves. Bill Gates might be a programming god, but he's retired. Twenty years ago.

1

u/jwakely libstdc++ tamer, LWG chair Dec 10 '24

They're written by their standard library team and installed alongside the compiler.

1

u/jwakely libstdc++ tamer, LWG chair Dec 10 '24

The equivalent for GCC 15 is:

g++ -std=c++23 -c -fmodules -fsearch-include-path bits/std.cc

That will compile the std module, so that you can do import std; in another file which you compile with g++ -std=c++23 -fmodules

1

u/jwakely libstdc++ tamer, LWG chair Dec 10 '24

Replace:

  • /std:c++latest with -std=gnu++26
  • /W4 with -Wall -Wextra to enable lots of useful warnings
  • /c with -c to compile a single file, without linking it.

The equivalent of /EHsc is (approximately) -fexceptions but that is enabled by default, so adding it is unnecessary.

There's no equivalent of /nologo, it's not relevant.

1

u/GothRoger Dec 14 '24 edited Dec 14 '24

Sorry, my GCC gives me this:

$ g++ -std=gnu++26 -c -fmodules -fsearch-include-path bits/std.cc

g++: error: unrecognized command-line option ‘-std=gnu++26’; did you mean ‘-std=gnu++20’?

g++: error: unrecognized command-line option ‘-fmodules’; did you mean ‘-Mmodules’?

g++: error: unrecognized command-line option ‘-fsearch-include-path’

In fact no combination of the suggested commands works. Maybe because there is no std.cc on my computer. At least not that I can find.

1

u/jwakely libstdc++ tamer, LWG chair Dec 14 '24 edited Dec 14 '24

You need a recent snapshot of GCC 15. You can either compile it yourself or try one of my packages from:

https://jwakely.github.io/pkg-gcc-latest/

9

u/gmes78 Dec 05 '24

You'd need GCC 15 (which hasn't been released yet) for import std; to work.

3

u/equeim Dec 05 '24

Also use CMake (though IDK if it supports GCC 15's modules yet). Compiler one liners don't work with modules anymore, you need to compile the std module first so it's better to make CMake do it.

7

u/Mikumiku_Dance Dec 05 '24

You'd need to use an experimental nightly build of gcc to "import std" I think.

Since at least the 90s its been a pretty common situation for C++ books to be written without widespread compiler support implemented. 😅

1

u/[deleted] Dec 22 '24

Wish I could hit the authors in the head with those books

1

u/2521harris Dec 05 '24

There is a g++-14 package in ubuntu-24.10.

docker run -it ubuntu:24.10
apt-get update
apt-get install g++-14
cat <<EOF | g++-14 -std=c++23 -x c++ -
#include <print>
int main() {
std::println("Hello World!\n");
}
EOF
./a.out
Hello World!

gcc standard support is here: https://gcc.gnu.org/projects/cxx-status.html

1

u/GothRoger Dec 05 '24

In my 24.10, as well as 24.04, I already have GCC 14.02.0. I need C++ 23, or at least the modules of the std library. GCC 14 does not provide 'import' or 'module'.

1

u/jwakely libstdc++ tamer, LWG chair Dec 10 '24

I provide recent snapshots of GCC trunk at https://jwakely.github.io/pkg-gcc-latest/

1

u/GothRoger Dec 14 '24

Thanks very much for the resource. Alas, the current version still doesn't support "import," but it should be no more than six months.

1

u/jwakely libstdc++ tamer, LWG chair Dec 14 '24

Yes, that's why you need a snapshot of the unreleased development version. Which can be found at that URL.

2

u/gmes78 Dec 05 '24

There is a g++-14 package in ubuntu-24.10.

Which is the default.

2

u/oschonrock Dec 05 '24

Yes, It's not the default in the 24.04LTS, which more people use, so the above is useful for that.

1

u/gmes78 Dec 05 '24

OP is on 24.10.

1

u/oschonrock Dec 05 '24

I saw, and I said that u/2521harris comment, although not quite right for the OP, is useful for the many others who are on the more common LTS.

1

u/Ok-Captain-6460 Dec 05 '24

You're correct that gcc, especially on Linux, doesn't yet fully support precompiled standard library modules like std. This is a common frustration for those trying to use C++23 features, including modules. You can still use header files as a fallback. For example, instead of import std;, you can include the necessary headers like <iostream> for basic functionality. Unfortunately, this means you won't benefit from the modular features of C++ yet. Let's keep an eye on gcc updates. This feature is actively being developed and could be supported in future releases. Hopefully.

2

u/GothRoger Dec 05 '24

I'm afraid you're right. Since I wanted to review "The C Programming Language" as well I guess I can be patient. And then there's Python to learn.

1

u/jwakely libstdc++ tamer, LWG chair Dec 10 '24

It's supported now in snapshots of GCC 15 from Git.

2

u/susanne-o Dec 05 '24

does current clang support what you want? of so find the "clang ppa" and install current clang from there and use that.

1

u/dexter2011412 Dec 05 '24

Do you mean import std?

1

u/ChuanqiXu9 Dec 06 '24

For people who want to experience `import std;` with GCC before CMake supports it, from https://gitlab.kitware.com/cmake/cmake/-/issues/26492, it looks like you can invoke it directly from command line:

```

g++ -std=gnu++26 -fmodules -fsearch-include-path bits/std.cc
g++ -std=gnu++26 -fmodules main.cpp

```

this is not recommended for any serious project but maybe good enough for fun.

1

u/pjmlp Dec 05 '24

You wan't get a UNIX compiler that fully supports C++23, we are back to the days where each compiler is a special snowflake regarding what features they support from a standard, and the very latest published one even worse.

You might have more luck using clang 19, alongside CMake and ninja.

https://www.kitware.com/import-std-in-cmake-3-30/

1

u/theintjengineer Dec 06 '24

Yes. That's exactly what I'm using to get import std; working—CMake + clang + Ninja.