I followed a CMake official blog of import CMake; the Experiment is Over! and created the simple hello world program with modules in the blog. An error of /usr/include/c++/v1/stddef.h:17:15: fatal error: 'stddef.h' file not found
appeared when compiling. Thanks in advance.
CMakeLists.txt
file
cmake_minimum_required(VERSION 3.28)
project(std_module_example CXX)
# Turning off extensions avoids and issue with the clang 16 compiler
# clang 17 and greater can avoid this setting
set(CMAKE_CXX_EXTENSIONS OFF)
# Set the version of C++ for the project
set(CMAKE_CXX_STANDARD 20)
# Create a library
add_library(foo)
# Add the module file to the library
target_sources(foo
PUBLIC
FILE_SET CXX_MODULES FILES
foo.cxx
)
# Create an executable
add_executable(hello main.cxx)
# Link to the library foo
target_link_libraries(hello foo)
foo.cxx
file
// Global module fragment where #includes can happen
module;
#include <iostream>
// first thing after the Global module fragment must be a module command
export module foo;
export class foo {
public:
foo();
~foo();
void helloworld();
};
foo::foo() = default;
foo::~foo() = default;
void foo::helloworld() { std::cout << "hello world\n"; }
main.cxx
file
import foo;
int main() {
foo f;
f.helloworld();
return 0;
}
build:
❯ CXX=clang++ /opt/cmake/bin/cmake . -B build -GNinja -DCMAKE_CXX_FLAGS='-stdlib=libc++' -DCMAKE_EXE_LINKER_FLAGS='-stdlib=libc++' && /opt/cmake/bin/cmake --build build -v
-- The CXX compiler identification is Clang 17.0.3
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/lib64/ccache/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/ruby/fedora-src/modules-in-clang/t2/build
Change Dir: '/home/ruby/fedora-src/modules-in-clang/t2/build'
Run Build Command(s): /usr/bin/ninja-build -v
[1/8] "/usr/bin/clang-scan-deps" -format=p1689 -- /usr/lib64/ccache/clang++ -stdlib=libc++ -std=c++20 -x c++ /home/ruby/fedora-src/modules-in-clang/t2/main.cxx -c -o CMakeFiles/hello.dir/main.cxx.o -MT CMakeFiles/hello.dir/main.cxx.o.ddi -MD -MF CMakeFiles/hello.dir/main.cxx.o.ddi.d > CMakeFiles/hello.dir/main.cxx.o.ddi
[2/8] "/usr/bin/clang-scan-deps" -format=p1689 -- /usr/lib64/ccache/clang++ -stdlib=libc++ -std=c++20 -x c++ /home/ruby/fedora-src/modules-in-clang/t2/foo.cxx -c -o CMakeFiles/foo.dir/foo.cxx.o -MT CMakeFiles/foo.dir/foo.cxx.o.ddi -MD -MF CMakeFiles/foo.dir/foo.cxx.o.ddi.d > CMakeFiles/foo.dir/foo.cxx.o.ddi
FAILED: CMakeFiles/foo.dir/foo.cxx.o.ddi
"/usr/bin/clang-scan-deps" -format=p1689 -- /usr/lib64/ccache/clang++ -stdlib=libc++ -std=c++20 -x c++ /home/ruby/fedora-src/modules-in-clang/t2/foo.cxx -c -o CMakeFiles/foo.dir/foo.cxx.o -MT CMakeFiles/foo.dir/foo.cxx.o.ddi -MD -MF CMakeFiles/foo.dir/foo.cxx.o.ddi.d > CMakeFiles/foo.dir/foo.cxx.o.ddi
Error while scanning dependencies for /home/ruby/fedora-src/modules-in-clang/t2/foo.cxx:
In file included from /home/ruby/fedora-src/modules-in-clang/t2/foo.cxx:3:
In file included from /usr/include/c++/v1/iostream:43:
In file included from /usr/include/c++/v1/ios:222:
In file included from /usr/include/c++/v1/__locale:15:
In file included from /usr/include/c++/v1/__memory/shared_ptr.h:22:
In file included from /usr/include/c++/v1/__memory/allocation_guard.h:15:
In file included from /usr/include/c++/v1/__memory/allocator_traits.h:14:
In file included from /usr/include/c++/v1/__memory/construct_at.h:23:
In file included from /usr/include/c++/v1/new:99:
In file included from /usr/include/c++/v1/cstdlib:87:
In file included from /usr/include/c++/v1/stdlib.h:94:
In file included from /usr/include/stdlib.h:32:
/usr/include/c++/v1/stddef.h:17:15: fatal error: 'stddef.h' file not found
ninja: build stopped: subcommand failed.
❯ /opt/cmake/bin/cmake --version
cmake version 3.28.0-rc3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
Solution
The issue can be solved by adding -I/usr/lib/clang/17/include
to clang++ compiler option if setting C++ compiler with CXX=clang++
, which it will not point to the real location of C++ compiler. It will also can be solved by setting the real binary compiler, like -DCMAKE_CXX_COMPILER=/usr/bin/clang++
.
Ref: https://github.com/llvm/llvm-project/issues/61006 https://gitlab.kitware.com/cmake/cmake/-/issues/25180
It seems like some headers are missing. After confirming clang is installed I'd try running
or your distro's equivalent.