admin管理员组

文章数量:1122832

I am using the latest version of g++, i.e. version 14.2 on Ubuntu 24.04. When I compile, the "Hello World" code, I get some errors. I am new to c++ and I will appreciate a guiding hand. I can rewrite the code to be conform with c++20 and compile it without problem, but I want to try c++23.

Here is the code:

import std;
using namespace std;

int main(){
    println("Hello World");

}

Here is how I compile and the errors:

hakiza@VBox:~/cpp/ppp/ch00$ g++ -std=c++2b helloWorld.cpp -o hello
helloWorld.cpp:1:1: error: ‘import’ does not name a type
    1 | import std;
      | ^~~~~~
helloWorld.cpp:1:1: note: C++20 ‘import’ only available with ‘-fmodules-ts’, which is not yet enabled with ‘-std=c++20’
helloWorld.cpp: In function ‘int main()’:
helloWorld.cpp:5:5: error: ‘println’ was not declared in this scope
    5 |     println("Hello World");
      |     ^~~~~~~

Here is the g++ version:

hakiza@VBox:~/cpp/ppp/ch00$ g++ --version
g++ (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I am using the latest version of g++, i.e. version 14.2 on Ubuntu 24.04. When I compile, the "Hello World" code, I get some errors. I am new to c++ and I will appreciate a guiding hand. I can rewrite the code to be conform with c++20 and compile it without problem, but I want to try c++23.

Here is the code:

import std;
using namespace std;

int main(){
    println("Hello World");

}

Here is how I compile and the errors:

hakiza@VBox:~/cpp/ppp/ch00$ g++ -std=c++2b helloWorld.cpp -o hello
helloWorld.cpp:1:1: error: ‘import’ does not name a type
    1 | import std;
      | ^~~~~~
helloWorld.cpp:1:1: note: C++20 ‘import’ only available with ‘-fmodules-ts’, which is not yet enabled with ‘-std=c++20’
helloWorld.cpp: In function ‘int main()’:
helloWorld.cpp:5:5: error: ‘println’ was not declared in this scope
    5 |     println("Hello World");
      |     ^~~~~~~

Here is the g++ version:

hakiza@VBox:~/cpp/ppp/ch00$ g++ --version
g++ (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Share Improve this question asked Nov 22, 2024 at 10:35 ezymanezyman 3271 silver badge12 bronze badges 2
  • Did you pass -fmodules-ts as the error message suggests? Aside, modules is a C++20 feature, but the support for std module is still incomplete last time I checked. If you just want to try out C++23 feature, just #include<print> and do std::println(...). – Weijun Zhou Commented Nov 22, 2024 at 10:41
  • @Weijun Zhou: I was expecting that g++-14 could support import std;, but it seems that is not the case. – ezyman Commented Nov 22, 2024 at 16:49
Add a comment  | 

1 Answer 1

Reset to default 0

After some googling, I found the answer by @ChrisMM in this post: "GCC's libstdc++ does not support P2465R3 yet, which allows for import std;". One can get the same answer from compiler_support for c++23.

本文标签: gc23 quotHello Worldquot not compilingStack Overflow