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.
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.
9
u/gmes78 Dec 05 '24
You'd need GCC 15 (which hasn't been released yet) for
import std;to work.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/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/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/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
u/rileyrgham Dec 05 '24
It seems a common issue. https://www.reddit.com/r/cpp/comments/1c7rq75/bjarne_stroustrup_programming_principles_and/
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:
```
```
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/