Hi all.
I am trying to apply what the import std in CMake 3.30 post does to use in a project, but am facing errors.
My setup:
CMakeLists.txt
CMakePresets.json
src/CMakeLists.txt
src/kk-intro.cppm
src/kk-intro.cc
src/main.cc
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.30)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD
"0e5b6991-d74f-4b3d-a41c-cf096e0b2508")
project(kk LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(src)
CMakePresets.json
(simplified)
{
"version": 8,
"configurePresets": [
{
"name": "linux-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"installDir": "${sourceDir}/install/${presetName}",
"cacheVariables": {
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_CXX_FLAGS": "-stdlib=libc++",
"CMAKE_EXE_LINKER_FLAGS": "-fuse-ld=lld -stdlib=libc++",
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
},
{
"name": "lnx-debug-C",
"inherits": "linux-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "lnx-release-C",
"inherits": "linux-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "lnx-release-B",
"displayName": "LnxRelB",
"configurePreset": "lnx-release-C"
},
{
"name": "lnx-debug-B",
"displayName": "LnxDbgB",
"configurePreset": "lnx-debug-C"
}
],
}
src/CMakeLists.txt
set(CMAKE_CXX_MODULE_STD 1)
add_library(kk STATIC)
target_sources(kk
PRIVATE kk-intro.cc
PUBLIC
FILE_SET CXX_MODULES
FILES kk-intro.cppm)
target_compile_features(kk
PRIVATE cxx_std_23
INTERFACE cxx_std_23)
add_executable(main)
target_sources(main
PRIVATE main.cc)
target_link_libraries(main
PRIVATE kk)
The module and main.cc
files (just for demo purposes)
// kk-intro.cppm
export module kk;
import std;
export namespace kk {
constexpr std::string module_name{"kk"};
void print_hello_world();
}
// kk-intro.cc
module kk;
namespace kk {
void print_hello_world() { std::println("Hello, World!"); }
}
// src/main.cc
import kk;
import std;
int main() {
kk::print_hello_world();
std::println("Setting up {}!", kk::module_name);
}
Compiler- and lib-wise
- I have
clang++
installed on$HOME/tools/llvm19.1.3
- this directory in is my$PATH
. - I don’t know whether this is necessary, but I added
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/tools/llvm19.1.3/lib/x86_64-unknown-linux-gnu/
to my~/.bashrc
.
Extras
Now, some of my rationale behind some of the code:
- I thought that adding
std_cxx_23
to bothPRIVATE
andINTERFACE
intarget_compile_features(kk ...
would allow me to useimport std;
onmain.cc
. At least this was the idea. I’ve tried not doing that, but that didn’t solve the problem. - I have the feeling the problem has to do with compiler and link flags that I’m not properly using.
The error that seems to blow up everything is:
cmake --workflow lnx-config-build-dbg
Executing workflow step 1 of 2: configure preset "lnx-debug-C"
Preset CMake variables:
CMAKE_BUILD_TYPE="Debug"
CMAKE_CXX_COMPILER="clang++"
CMAKE_CXX_FLAGS="-stdlib=libc++"
CMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld -stdlib=libc++"
CMAKE_INSTALL_PREFIX:PATH="/home/elmago/cpp/install/lnx-debug-C"
-- The CXX compiler identification is Clang 19.1.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/elmago/tools/llvm19.1.3/bin/clang++ - skipped
-- Detecting CXX compile features
CMake Warning (dev) at /home/elmago/.local/share/cmake-3.31/Modules/Compiler/CMakeCommonCompilerMacros.cmake:248 (cmake_language):
CMakes support for `import std;` in C++23 and newer is experimental. It
is meant only for experimentation and feedback to CMake developers.
Call Stack (most recent call first):
/home/elmago/.local/share/cmake-3.31/Modules/CMakeDetermineCompilerSupport.cmake:113 (cmake_create_cxx_import_std)
/home/elmago/.local/share/cmake-3.31/Modules/CMakeTestCXXCompiler.cmake:83 (CMAKE_DETERMINE_COMPILER_SUPPORT)
CMakeLists.txt:9 (project)
This warning is for project developers. Use -Wno-dev to suppress it.
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/elmago/cpp/build/lnx-debug-C
Executing workflow step 2 of 2: build preset "lnx-debug-B"
[10/16] Building CXX object src/CMakeFiles/kk.dir/kk-intro.cppm.o
FAILED: src/CMakeFiles/kk.dir/kk-intro.cppm.o src/CMakeFiles/kk.dir/kk.pcm
/home/elmago/tools/llvm19.1.3/bin/clang++ -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES -stdlib=libc++ -g -std=c++23 -MD -MT src/CMakeFiles/kk.dir/kk-intro.cppm.o -MF src/CMakeFiles/kk.dir/kk-intro.cppm.o.d @src/CMakeFiles/kk.dir/kk-intro.cppm.o.modmap -o src/CMakeFiles/kk.dir/kk-intro.cppm.o -c /home/elmago/cpp/src/kk-intro.cppm
error: GNU extensions was enabled in PCH file but is currently disabled
error: module file CMakeFiles/__cmake_cxx23.dir/std.pcm cannot be loaded due to a configuration mismatch with the current compilation [-Wmodule-file-config-mismatch]
/home/elmago/cpp/src/kk-intro.cppm:5:11: error: use of undeclared identifier 'std'
5 | constexpr std::string module_name{"kk"};
| ^
3 errors generated.
[11/16] Building CXX object CMakeFiles/__cmake_cxx23.di...mago/tools/llvm19.1.3/share/libc++/v1/std.compat.cppm.
ninja: build stopped: subcommand failed.
- That mention of
GNU extensions
, PCH, etc. Why? Where? - Now,
std.pcm
relates toCMakeFiles/__cmake_cxx23.dir/std.pcm
butstd.compat.pcm
relates to theLD_LIBRARY_PATH
that I set. Weird, and I don’t know how to actually interpret that.
I appreciate any help in advance.