Linux下用Clang17+CMake3.28编译C++20模块报错stddef.h未找到
C++20模块编译错误:stddef.h文件未找到
参考CMake官方博客《import CMake; the Experiment is Over!》编写基于C++20模块的HelloWorld程序时,编译出现如下错误:/usr/include/c++/v1/stddef.h:17:15: fatal error: 'stddef.h' file not found
相关文件内容
CMakeLists.txt
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
// 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
import foo; int main() { foo f; f.helloworld(); return 0; }
编译命令及报错信息
❯ 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).
解决方案
- 若通过
CXX=clang++设置编译器(未指向真实路径),可添加-I/usr/lib/clang/17/include到clang++编译选项 - 直接指定真实编译器路径,例如使用
-DCMAKE_CXX_COMPILER=/usr/bin/clang++参数
内容的提问来源于stack exchange,提问作者Topazus
相关产品推荐
相关产品推荐

