-
Notifications
You must be signed in to change notification settings - Fork 18
compiling 01_hellow.cpp #2
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
Comments
Just to clarify, the C++ code in the "modules" directory (which uses All of the code in the "headers" directory (which uses |
clang++ -std=c++23 01-hellow.cpp -o 01-hellow clang++ --version How to solve the compile error? |
Please try adding: -stdlib=libc++
|
Hi @cpp-tutor,
Additional info:
|
Hi Thomas,
I notice that you are using quite an old version of clang, as I recall "import std;" didn't work for me until clang 14.Also please be aware that "println()" is C++23 (look for header "print" in /usr/include/c++[...]), and I'm not even sure if is available with "import" yet.
I can't recommend Compiler Explorer because "-stdlib=libc++" is not available, so my best suggestion is to download and compile the latest clang/LLVM, and enable sub-project "libcxx". (This does take a long time.)
Alternatively, you can clone the "1.0 C++20" branch of the repo, and use the source code from the "headers" directory.
Kind regards,
Richard
|
Still likely an issue for many in 2025, but with a solution for clang-18 and newer, see this page (summary below): Firstly, compile the clang++ -std=c++23 -stdlib=libc++ \
-Wno-reserved-identifier -Wno-reserved-module-identifier \
--precompile -o std.pcm /usr/share/libc++/v1/std.cppm Then, reference this new file and libc++ to compile a single source file to an executable (in this case clang++ -std=c++23 -stdlib=libc++ \
-fmodule-file=std=std.pcm -o test std.pcm test.cpp Paths shown above should be correct as for most Linuxes (in particular, Debian Bookworm, with |
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,
The text was updated successfully, but these errors were encountered: