learnmoderncpp-tutorial icon indicating copy to clipboard operation
learnmoderncpp-tutorial copied to clipboard

compiling 01_hellow.cpp

Open rinka-meltiron opened this issue 1 year ago • 5 comments

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,

rinka-meltiron avatar Dec 28 '23 10:12 rinka-meltiron

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.

cpp-tutor avatar Dec 30 '23 11:12 cpp-tutor

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?

allen-c avatar Mar 21 '24 11:03 allen-c

Please try adding: -stdlib=libc++

cpp-tutor avatar Mar 22 '24 08:03 cpp-tutor

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

Krannich479 avatar May 20 '24 20:05 Krannich479

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

cpp-tutor avatar May 21 '24 07:05 cpp-tutor