Error trying to use modules, import function unknown?
-
wrote on 30 Apr 2025, 04:13 last edited by
Hi,
I am doing a course on 'modern' C++ and using the following in CMakeLists.txt:
#Require C++23
set(CXX_STANDARD_REQUIRED ON) #Make C++23 a hard requirement
set(CMAKE_CXX_STANDARD 23) # Default C++ standard for targets
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
The course instructor is using VS Code to write the code examples. I also have VS Code installed and these little code snippets work fine when compiled from the Code IDE but will not work using QtCreator as the IDE.
I am using the same compilers, either Clang 19.1 or GCC 13.3.0 as this is the latest version in the Mint repos.
The code is very simple and just puts 'import utilities' (the file utilities.ixx) after the #includes and before main().
The error I get from QtCreator is that the compiler doesn't understand import and yet the same compiler and cmake understands them perfectly when compiling from the Code IDE?
I really prefer using the QtCreator IDE to VS Code but I can't get past this problem and I can't even say for sure where the problem lies.
Any ideas / suggestions welcome
Colin -
wrote on 30 Apr 2025, 04:42 last edited by
Hi Colin,
The issue might be with how QtCreator handles C++23 modules compared to Visual Studio Code. Here are some quick steps to troubleshoot:
Check CMake Settings: Ensure CMAKE_CXX_STANDARD and CMAKE_CXX_SCAN_FOR_MODULES are applied in QtCreator.
Enable Module Flags: Add -fmodules-ts for GCC or -std=c++23 explicitly in QtCreator's build settings.
Compare Build Logs: Look for differences between the build logs of QtCreator and VS Code to find missing flags or settings.
Update QtCreator: Ensure you're using the latest version for proper C++23 module support.
If all else fails, stick with Visual Studio Code for this particular setup or report the issue to QtCreator's team for clarification.
Hope this helps!
-
Hi,
I am doing a course on 'modern' C++ and using the following in CMakeLists.txt:
#Require C++23
set(CXX_STANDARD_REQUIRED ON) #Make C++23 a hard requirement
set(CMAKE_CXX_STANDARD 23) # Default C++ standard for targets
set(CMAKE_CXX_SCAN_FOR_MODULES ON)
The course instructor is using VS Code to write the code examples. I also have VS Code installed and these little code snippets work fine when compiled from the Code IDE but will not work using QtCreator as the IDE.
I am using the same compilers, either Clang 19.1 or GCC 13.3.0 as this is the latest version in the Mint repos.
The code is very simple and just puts 'import utilities' (the file utilities.ixx) after the #includes and before main().
The error I get from QtCreator is that the compiler doesn't understand import and yet the same compiler and cmake understands them perfectly when compiling from the Code IDE?
I really prefer using the QtCreator IDE to VS Code but I can't get past this problem and I can't even say for sure where the problem lies.
Any ideas / suggestions welcome
Colin@Colins2 Do you get this error when you compile the project or just in the QtCreator editor?
Did you make sure that the C++ standard flag was passed to the compiler? -
@Colins2 Do you get this error when you compile the project or just in the QtCreator editor?
Did you make sure that the C++ standard flag was passed to the compiler?wrote on 30 Apr 2025, 12:36 last edited by Colins2@jsulm I get the error compiling the project.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.28)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")project(lambdas LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES ON)add_executable(lambdas
main.cpp
vcpkg.json
utilities.ixx
)find_package(fmt CONFIG REQUIRED)
include(GNUInstallDirs)
install(TARGETS lambdas
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)All the required flags should be passed, as far as I can see.
-
Hi Colin,
The issue might be with how QtCreator handles C++23 modules compared to Visual Studio Code. Here are some quick steps to troubleshoot:
Check CMake Settings: Ensure CMAKE_CXX_STANDARD and CMAKE_CXX_SCAN_FOR_MODULES are applied in QtCreator.
Enable Module Flags: Add -fmodules-ts for GCC or -std=c++23 explicitly in QtCreator's build settings.
Compare Build Logs: Look for differences between the build logs of QtCreator and VS Code to find missing flags or settings.
Update QtCreator: Ensure you're using the latest version for proper C++23 module support.
If all else fails, stick with Visual Studio Code for this particular setup or report the issue to QtCreator's team for clarification.
Hope this helps!
wrote on 30 Apr 2025, 12:50 last edited by@Shrishashank said in Error trying to use modules, import function unknown?:
Hi, thanks for the response.
The settings in CMakeLists.txt should be correct. See the answer to jsulm where I posted it.
I am using the latest version of QtCreator but I don't see why that should make a difference as the error seems to be a CMake one.
What is confusing me is that the code compiles and runs from VS Code using the same compiler(s).
Colin -
wrote on 1 May 2025, 14:20 last edited by
I've found the problem but not the solution!
Whatever I set in kits or project Qt always defaults back to /usr/bin/gcc, which is version 13......
I have installed gcc 14.2 through Homebrew but I can't get Qt to recognize it.
I also have Clang 19.1 with the same problem.
This is why I am unable to use the modules as it only works with gcc14.
Short of actually uninstalling gcc13, I can't think of what else to do.
As mentioned above, Code, though I hate to admit it, works OK. I can select any installed compiler to use.
Colin -
I've found the problem but not the solution!
Whatever I set in kits or project Qt always defaults back to /usr/bin/gcc, which is version 13......
I have installed gcc 14.2 through Homebrew but I can't get Qt to recognize it.
I also have Clang 19.1 with the same problem.
This is why I am unable to use the modules as it only works with gcc14.
Short of actually uninstalling gcc13, I can't think of what else to do.
As mentioned above, Code, though I hate to admit it, works OK. I can select any installed compiler to use.
Colinwrote on 2 May 2025, 06:49 last edited by@Colins2 said in Error trying to use modules, import function unknown?:
I have installed gcc 14.2 through Homebrew but I can't get Qt to recognize it.
I also have Clang 19.1 with the same problem.QtCreator does not always find all installations of compilers (only those in standard paths). However, you can manually add compilers. Then, you copy an existing kit (a kit is a combination of Qt version and compiler) and change its compiler to the one you added manually.
Once you have the proper kit you need to add a new build configuration under "Projects". Make sure to use a totally different folder as before (or delete the old one). CMake will remember the previous configuration and will not change the compiler if you are reusing the same folder.
-
@Colins2 said in Error trying to use modules, import function unknown?:
I have installed gcc 14.2 through Homebrew but I can't get Qt to recognize it.
I also have Clang 19.1 with the same problem.QtCreator does not always find all installations of compilers (only those in standard paths). However, you can manually add compilers. Then, you copy an existing kit (a kit is a combination of Qt version and compiler) and change its compiler to the one you added manually.
Once you have the proper kit you need to add a new build configuration under "Projects". Make sure to use a totally different folder as before (or delete the old one). CMake will remember the previous configuration and will not change the compiler if you are reusing the same folder.
wrote 27 days ago last edited by@SimonSchroeder That's a very important point!
I have solved my immediate problems like this:
I deleted GCC-13 from the kits/compilers.
I used sudo update-alternatives to make /usr/bin/gcc point to my GCC14.2 homebrew installation.
That worked partially but GCC-14.2 doesn't handle modules properly and nor does it reliably handle vcpkg packages!
(I have been using clang-19.1 with VS Code for the project/tutorials mentioned above and that does work)
I struggled to get QtCreator to use the clang compiler but at last found the solution, albeit using a sledgehammer to crack a nut!- Delete GCC entries from the Kits/compilers section. My Qt installation already found clang as it was in my path.
- Added 'export $CXX=clang to my .bashrc. Not sure how necessary this is but it was mentioned in an error message.
3)Added the line "set(CMAKE_CXX_COMPILER clang++)" to CMakeLists before "project(helloClang LANGUAGES CXX)"
Now my little helloClang compiles with the clang compiler.
I added a vcpkg statement and the fmt library with the appropriate lines in CMakeLists - and it works!
I haven't had time to try out modules yet but I'm sure they will work as well.
I don't think it was really necessary to delete the GCC kits, probably setting the compiler in CMakeLists may be enough.
I will continue experimenting to see what works.
None of this is necessary in VS Code, it's a simple drop-down menu to select the compiler when configuring initially. Also, there are no entries in the CMakeLists file nor environmental variables set.
An equivalent in QtCreator would be to just go to the kits page and select the wanted compiler and click Apply/OK but this does not work.
I'm just pleased that I will be able to use modules and vcpkg packages from QtCreator now. -