How to use "import std" in C++
-
I am following the book Programming: Principles and Practice Using C++ (C++ In-depth) 3rd Edition by Stroustrup. This book is using C++20 and C++23.
For this purposes I setup my environment as Qt Creator and Microsoft Build Tools 2022 C++ compiler on my Windows machine.
The hello world example is like this// This program outputs the message "Hello, World!" to the monitor import std; // gain access to the C + + standard library int main() // C + + programs start by executing the function main { std:: cout < < "Hello, World!\ n"; // output "Hello, World!" return 0; }
If I change
import std;
with
#include <iostream>
compiling works.
In my understanding in order to use C++ 23 I need to manually build the C++23 modulesI am able to compile these libraries using on the command prompt:
cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx"
and these will produce two files:
std.ifc
std.objBut how do I use these in Qt Creator by default in a Qt Creator project?
-
Have a look at import CMake; the Experiment is Over!.
You then need to:
- Make sure you have at least CMake 3.28 installed
- Change the
CMakeLists.txt
from a Non Qt-Project > Plain C++ Application wizard as done in the blog post above
But, the C++ code model used by Qt Creator can't handle modules at this time. See https://github.com/clangd/clangd/issues/1850 for tracking.
-
So, no workaround? Can I not follow the book properly? I'm just learning C++
-
Just to clarify, from the command line if I open "x64 Native Tools Command Prompt for VS 2022" - that's because I want 64-bit and I create a file inside a random directory named hello_world.cpp containing
// This program outputs the message "Hello, World!" to the monitor import std; // gain access to the C + + standard library int main() // C++ programs start by executing the function main { std:: cout << "Hello, World!\n"; // output "Hello, World!" return 0; }
and I first create the standard library like this as mentioned in the Microsoft tutorial
cl /std:c++latest /EHsc /nologo /W4 /c "%VCToolsInstallDir%\modules\std.ixx"
then compiling works
cl /std:c++latest /EHsc /nologo /W4 hello_world.cpp std.obj
and creates the executable.
The question is how to achieve the same using Qt Creator.
-
Did you follow the recommendation from @cristian-adam ?
The article linked also provided a complete example with a
CMakeFiles.txt
so you could take it and open it with Qt Creator. Please note the minimal CMake version required. -
I tried a few things, nothing works for me, but I am a beginner and this is new for me, I don't understand how all of this works yet. I will move on with my study of C++ without "import std;" for the time being. Anyway, it seems that import std was introduced in CMake 3.30 as detailed here
So, I guess I just need to wait a bit.
Thanks everyone for support! -
In the post linked by @cristian-adam there is the following comment:
No, import std; has not yet been implemented. The plan is for the compiler to populate a CMake::CXX23 target with the requisite information and have that target provide import std; modules. There are discussions about how compilers/standard libraries should provide information for CMake to use and make such targets.
I assume that the problem is that CMake does not know how to look up modules that are not provided by your own project. Because it cannot find the proper dependencies it will not build.
I'm eagerly waiting for modules to work properly everywhere. This will be a huge boost for C++.