Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiling 01_hellow.cpp #2

Open
rinka-meltiron opened this issue Dec 28, 2023 · 5 comments
Open

compiling 01_hellow.cpp #2

rinka-meltiron opened this issue Dec 28, 2023 · 5 comments

Comments

@rinka-meltiron
Copy link

rinka-meltiron commented Dec 28, 2023

g++ -o 01-hellow -std=c++20 01-hellow.cpp on my Linux machine did not work.
The issue with gcc is:
$ g++ -o 01-hellow -std=c++20 01-hellow.cpp
01-hellow.cpp:3:1: error: ‘import’ does not name a type
3 | import std;
| ^~~~~~
01-hellow.cpp:3:1: note: C++20 ‘import’ only available with ‘-fmodules-ts’, which is not yet enabled with ‘-std=c++20’
01-hellow.cpp: In function ‘int main():
01-hellow.cpp:7:5: error: ‘cout’ was not declared in this scope
7 | cout << "Hello, World!" << '\n';
| ^~~~
01-hellow.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | // 01-hellow.cpp : prints a line of text to the console

and with clang is:
$ clang++ -o 01-hellow -std=c++20 -stdlib=libc++ 01-hellow.cpp
01-hellow.cpp:3:8: fatal error: module 'std' not found
import std;
~~~~~~~^~~
1 error generated

I suggest updating the code for people who use linux, vim (or similar editors). A lot of us use the command line.

A quick answer of course is:
// 01-hellow.cpp : prints a line of text to the console

#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!" << '\n';
}

Thanks,

@cpp-tutor
Copy link
Owner

Just to clarify, the C++ code in the "modules" directory (which uses import) is not (yet) compatible with g++. Please use Clang under Linux with the -fmodules flag.

All of the code in the "headers" directory (which uses #include) should compile with either g++ or Clang under Linux with the flags you described above. Should you discover any that do not, please add to this issue.

@allen-c
Copy link

allen-c commented Mar 21, 2024

clang++ -std=c++23 01-hellow.cpp -o 01-hellow
01-hellow.cpp:3:8: fatal error: module 'std' not found
3 | import std;
| ~~~~~~~^~~
1 error generated.

clang++ --version
clang version 19.0.0git ([email protected]:llvm/llvm-project.git cbcdf126ccc774c063b5d5140c1393ff5305dded)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin

How to solve the compile error?

@cpp-tutor
Copy link
Owner

cpp-tutor commented Mar 22, 2024 via email

@Krannich479
Copy link

Krannich479 commented May 20, 2024

Hi @cpp-tutor,
thank you for your assistance! Unfortunately I face the same issue even though I use clang. Running
clang++ -fmodules -std=c++2b -stdlib=libc++ -o 01-hellow 01-hellow.cpp
(for 'Working draft for ISO C++ 2023 DIS' standard) or
clang++ -fmodules -std=c++20 -stdlib=libc++ -o 01-hellow 01-hellow.cpp
both result in

01-hellow.cpp:3:1: error: unknown type name 'import'
import std;
^
01-hellow.cpp:4:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
                ^
01-hellow.cpp:7:5: error: use of undeclared identifier 'println'
    println("Hello, World!");
    ^
1 warning and 2 errors generated.

Additional info:

% clang -v
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
% sw_vers
ProductName:	macOS Monterey
ProductVersion:	12.5.1
BuildVersion:	21G83

@cpp-tutor
Copy link
Owner

cpp-tutor commented May 21, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants