如何在Linux中使用clang 17和CMake 3.28编译一个简单的带有c++20模块的hello world?

q5iwbnjs  于 7个月前  发布在  Linux
关注(0)|答案(1)|浏览(111)

我关注了一个导入CMake的官方博客;实验结束了!并使用博客中的模块创建了简单的hello world程序。编译时出现了/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++设置C编译器,可以通过在clang编译器选项中添加-I/usr/lib/clang/17/include来解决这个问题,它不会指向C++编译器的真实的位置。也可以通过设置真实的二进制编译器来解决,如-DCMAKE_CXX_COMPILER=/usr/bin/clang++
参考:https://github.com/llvm/llvm-project/issues/61006https://gitlab.kitware.com/cmake/cmake/-/issues/25180

0lvr5msh

0lvr5msh1#

似乎有些头文件丢失了。确认clang安装后,我会尝试运行

apt-get install libc6-dev

字符串
或者你的发行版的等价物。

相关问题