Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to use "import std" in C++
QtWS: Super Early Bird Tickets Available!

How to use "import std" in C++

Scheduled Pinned Locked Moved Unsolved Language Bindings
7 Posts 4 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AltitudeDashboard
    wrote on last edited by
    #1

    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 modules

    I 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.obj

    But how do I use these in Qt Creator by default in a Qt Creator project?

    1 Reply Last reply
    0
  • cristian-adamC Online
    cristian-adamC Online
    cristian-adam
    wrote on last edited by
    #2

    Have a look at import CMake; the Experiment is Over!.

    You then need to:

    1. Make sure you have at least CMake 3.28 installed
    2. 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.

    1 Reply Last reply
    3
  • A Offline
    A Offline
    AltitudeDashboard
    wrote on last edited by
    #3

    So, no workaround? Can I not follow the book properly? I'm just learning C++

    1 Reply Last reply
    0
  • A Offline
    A Offline
    AltitudeDashboard
    wrote on last edited by
    #4

    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.

    SGaistS 1 Reply Last reply
    0
  • SGaistS Offline
    SGaistS Offline
    SGaist Lifetime Qt Champion
    replied to AltitudeDashboard on last edited by
    #5

    @AltitudeDashboard hi,

    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.

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
  • A Offline
    A Offline
    AltitudeDashboard
    wrote on last edited by
    #6

    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!

    1 Reply Last reply
    0
  • S Offline
    S Offline
    SimonSchroeder
    wrote on last edited by
    #7

    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++.

    1 Reply Last reply
    1
  • Pl45m4P Pl45m4 referenced this topic

  • Login

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved