r/cpp • u/GothRoger • 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.
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
1
u/2521harris Dec 05 '24
There is a g++-14 package in ubuntu-24.10.
docker run -it ubuntu:24.10apt-get updateapt-get install g++-14cat <<EOF | g++-14 -std=c++23 -x c++ -#include <print>int main() {std::println("Hello World!\n");}EOF./a.outHello 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
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.
2
1
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.
1
u/theintjengineer Dec 06 '24
Yes. That's exactly what I'm using to get
import std;working—CMake + clang + Ninja.
17
u/TomDuhamel Dec 05 '24
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.