Why does this C++ program does not compile using modules? - c++

Sorry if i ask again about "moudules" in C++.
Im using g++ gcc-c++-12.2.1-4.fc37.x86_64 in fedora 37 linux and vscode.
So i said: i will upgrade my knowladge with the book "A tour of C++ third edition" which its updated to c++20 standard.
The thing that the first program:
import std;
int main()
{
std::cout << "Hello, World!\n";
}
Doesn't compile.
I have enabled "std=c++20" and "-fmodules-ts" and also tried "-std=gnu++20".
The output is this, its like the compiled modules are missing:
std: error: failed to read compiled module: No existe el fichero o el directorio
std: nota: compiled module file is ‘gcm.cache/std.gcm’
std: nota: imports must be built before being imported
std: error fatal: returning to the gate for a mechanical issue
Ok it's in spanish becouse is my native tongue.
Any help?

import std;
This line of code requires not only module support, but also Standard Library Modules (P2465R3 Standard Library Modules std and std.compat) that's part of C++23.
From the Compiler support for C++23 page on cppreference, we can see that it's still not supported by GCC libstdc++.
As OP mentioned that the code snippet is taken from Bjarne's book, I double-checked section 1.2 of it. In the paragraph after the next from the code snippet containing import std;, it's stated that this is not yet standard:
The import directive is new in C++20 and presenting all of the standard library as a module std is not yet standard. [...]

Related

Learning C++20: import <iostream>; error?

I'm starting to learn C++20, my first compilable language...
import <iostream>;
int main()
{
int answer {42};
std::cout << "The answer is "
<< answer
<< std::endl;
return 0;
}
When I try to compile the file above, I get an error message due to the compiler not recognizing the statement import <iostream>;, even though I have the newest version of GCC compiler for Ubuntu 20.04.4 LTS.
C++ 20 is considered a new standard (or maybe the latest standard). So most of the compilers do not have full coverage of everything in C++ 20 and the GCC compiler that you are using does not know what is import <iostream> because it does not have full coverage. What you could do, is to check out this list of compilers supporting C++ 20.

How do I import C++ 20 <format> module?

I am using Visual Studio 2019 (Community edition) running on a Windows 10 machine. I have created a simple console application and I want to import the format module so that I can use something like std::format(). I get an error that 'cannot find header 'format' to import. My code is based on a book by Horton and van Weet titled 'Beginning C++ 20' which starts with two lines namely: import iostream (this is contained in angled brackets) followed by a line which says import format also contained within angled brackets. Maybe let me rephrase my question. The code generated by Visual Studio says #include iostream but does not allow me to #include format. So, when do I use #include and when do I use import?
While waiting for MSVS support of std::format, you can use the fmt library that is the basis for the std::format. This can be found at https://github.com/fmtlib/fmt. It is compatible with the C++20 standard but does include additional features. Add the library to your source and use fmt::format instead of std::format. It'll be easy to convert this to std::format once fully supported.
When using Visual Studio 2019/2022 to run C++20 code, you need go to: Project->'source code name' Properties and:
Configuration Properities->General->C++ Lanaguage Standard: ISO C++20 Standard (/std:c++20).
Configuration Properities->C/C++->Language->C++ Lanaguage Standard: ISO C++20 Standard (/std:c++20).
Configuration Properities->C/C++ ->General -> Scan Sources for Module Dependencies: Yes.
These three options must be done.

Difference between `import` and `#include`? cpp20

I don't understand why
I saw import std.core; here
I can't import std;
I can't import std.iostream;
I can #include <iostream>
Can you explain why above is happening? Maybe i guess std.iostream is not a module. Then why 1. works?
#Someprogrammerdue provided this reference, and its says
import <iostream>; // import declaration
When I run following in my compiler
import <iostream>;
int main()
{
return 0;
}
I get
main.cpp:1:8: error: 'iostream' was not declared in this scope
1 | import<iostream>;
Why is this happening?
I don't understand why
I saw import std.core; here
You saw what you did because you read the page and that's what was written there.
I can't import std;
This is because the C++20 standard library doesn't define modules. And because no other library can (or shouldn't) define module std because that module name is reserved for language implementation / future standardisation.
I can't import std.iostream;
See 2.
I can #include <iostream>
That header is part of the C++20 standard library.
Then why 1. works?
The MSVC documentation explains:
Although not specified by the C++20 standard, Microsoft enables its implementation of the C++ Standard Library to be imported as modules.
P.S. Support for modules is at the moment of writing only partially implemented by all major compilers with the exception of MSVC which appears to have full implementation since 19.28 (the modular standard libary is not a requirement for this).
P.P.S. Modular standard library is planned for C++23.
import std;
Improvement in compilation time.
According to ISO C++23, there is also import std.compat;.
Differences between the two are as follows (extracted from P2465R3):
import std; imports everything in namespace std from C++ headers (e.g. std::sort from <algorithm>) and C wrapper headers (e.g. std::fopen from <cstdio>). It also imports ::operator new etc. from <new>.
import std.compat; imports all of the above, plus the global namespace counterparts for the C wrapper headers (e.g. ::fopen).
import ... is said to be much faster to compile than #include ....
Interview with Bjarne Stroustrup (InfoWorld): C++ 23 to introduce module support.
P.S. Modules are not supported fully by most compilers yet. For MSVC users, please, refer to this link.
(https://learn.microsoft.com/en-us/cpp/cpp/tutorial-import-stl-named-module?view=msvc-170).

How to enable C++17 code generation in VS2019 CUDA project

I am moving some code from VS2017 on one pc to another pc with VS2019. Everything is fine excepted that I cannot use std::filesystem. In my former code, I was using C++14 and had std::experimental::filesystem. In the new code, I want to move to C++17 so I changed to std::filesystem (as shown in my code below). The weird thing is that intellisense (not sure it is the right name of the thing) shows no error. It even displays filesystem when I type std::f...
But the code won't build and give the error "namespace "std" has no member "filesystem"".
I changed C++ Language Standard to c++latest, VS2019 version is Community 16.6.5.
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::string path = "C:\\";
for (const auto& entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
EDIT: The last line of my initial question might not have been clear enough: I already changed "C++ Language Standard" to C++17 or C++latest.
EDIT: As requested, the output:
Thanks to #drescherjm, we found that it is a Cuda issue. Any idea from a Cuda specialist?
Using CUDA 11, the nvcc compiler-driver is capable of supporting usage of certain C++17 language features. Currently, in VS2019, this doesn't appear to be the default behavior.
The following method should work to enable C++17 support when compiling a cuda project in VS2019:
go to Project..Properties..Configuration Properties...CUDA C/C++...Command Line Then you will see a box in the right hand side bottom of the dialog labelled "Additional Options". In that box, type the following:
-std=c++17 -Xcompiler "/std:c++17"
then click "Apply" then rebuild.
(These instructions may change for future versions of CUDA or future versions of Visual Studio.)
Note that this method applies to CUDA projects only (i.e. when nvcc is invoked for compilation), and should be workable whether your code is in a file ending in .cpp or in a file ending in .cu. For non-CUDA projects, this may be helpful.
The std::filesystem features appear to require C++17. The CUDA nvcc compiler-driver is documented here.

Unable to Resolve Identifier constexpr

I'm teaching myself C++ with the textbook 'Programming -- Principles and Practice Using C++ (Second Edition)' and have come across a problem while trying to run an example problem. The line I'm supposed to input is
constexpr int max = 17;
but I am getting an error: "Unable to resolve identifier constexpr", but I have no idea why because I have the necessary header file (specific to the textbook, downloaded from www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h ) All the other programs I've tried have worked just fine...
Other useful info:
-Using Netbeans (C++ Version)
-Using Windows 8
-Using Cygwin
-Not an expert.
constexpr is a new feature of C++11. You need a C++11 capable compiler (like g++4.8) and have to enable the C++11 extensions when you compile a program:
g++ -std=c++11 main.cpp -o test