使用clang toolchain时,无法同时开启coroutines与module特性
Xmake 版本
v2.9.4+20240730
操作系统版本和架构
6.5.0-41-generic #41~22.04.2-Ubuntu
描述问题
- 将toolchain指定为clang 2.1. 没有引用自定义的module时,可以正常引用coroutines头文件并编译成功 2.2. 引用自定义的module后,编译报错:"the coroutine header requires -fcoroutines"
- 将toolchain指定为gcc后,同时使用modules与coroutines可以正常工作
大部分情况下,compile_commands.json 都无法正常生成,偶尔生成一次,可以发现 "-fcoroutines-ts" 选项并没有添加上
期待的结果
main.cpp中引入自定义的modules后,依旧可以编译成功
工程配置
add_rules("mode.debug", "mode.release")
set_languages("c++20")
target("test")
set_kind("binary")
-- add_files("src/*.cpp")
add_files("src/*.cpp", "src/*.ixx")
add_cxxflags("-fcoroutines")
add_cxxflags("-fcoroutines-ts")
module;
export module foo;
export namespace foo {
auto max(int a, int b) -> int { return a > b ? a : b; }
} // namespace foo
#include <coroutine>
#include <iostream>
import foo;
int main(int argc, char **argv) {
std::cout << "Hello world!" << std::endl;
std::cout << "foo::max(1, 2) = " << foo::max(1, 2) << std::endl;
return 0;
}
附加信息和错误日志
❯ xmake --diagnosis --dry-run
[ 0%]:
stack traceback: [C]: in function 'error' [@programdir/core/base/os.lua:973]: [@programdir/core/sandbox/modules/os.lua:273]: [@programdir/core/sandbox/modules/os.lua:291]: in function 'vrunv' [...c++/modules/modules_support/clang/dependency_scanner.lua:74]: in function 'preprocess_file' [.../modules/modules_support/clang/../dependency_scanner.lua:321]: in function 'fallback_generate_dependencies' [...c++/modules/modules_support/clang/dependency_scanner.lua:64]: in function 'callback' [@programdir/modules/core/project/depend.lua:217]: in function 'on_changed' [...c++/modules/modules_support/clang/dependency_scanner.lua:34]: in function 'generate_dependency_for' [...rules/c++/modules/modules_support/dependency_scanner.lua:227]: in function 'jobfunc' [@programdir/modules/async/runjobs.lua:241]:
stack traceback: [C]: in function 'error' @programdir/core/base/os.lua:973: in function 'base/os.raiselevel' (...tail calls...) @programdir/core/main.lua:329: in upvalue 'cotask' @programdir/core/base/scheduler.lua:406: in function <@programdir/core/base/scheduler.lua:399> warning: std and std.compat modules not found ! disabling them for the build, maybe try to add --sdk=<PATH/TO/LLVM> warning: add -v for getting more warnings ..
Bot detected the issue body's language is not English, translate it automatically.
Title: When using clang toolchain, coroutines and module features cannot be enabled at the same time.
大部分情况下,compile_commands.json 都无法正常生成,偶尔生成一次,可以发现 "-fcoroutines-ts" 选项并没有添加上
默认 add_cxxflags 会自动调用 clang 去尝试编译,检测 flags 是否被 clang 支持,不支持的 会自动忽略。。
说明你这 clang 并不支持 这个 flag ,仅仅支持 -fcoroutines 。。
当然你可以通过 add_cxxflags("-fcoroutines-ts", {force = true}) 去强制加上,绕过自动检测,但是 clang 不支持这个 flag,即使你加上了也没用,一样要报错。。这是编译器的问题,跟 xmake 无关。。
像 '-fcoroutines-ts' 这种还是早期的 clang flags 了,新版本 clang 多半已经移除了。
error: error: unknown argument: '-fcoroutines-ts'
Error while scanning dependencies for src/main.cpp:
error: unknown argument: '-fcoroutines-ts'
另外我这里,即使没 -fcoroutines 都能编译,没啥问题,要么就是你这编译器版本低或者其他编译器问题。
target("test")
set_kind("binary")
add_files("src/*.cpp", "src/*.ixx")
ruki:test2 ruki$ xmake -r
[ 0%]: <test> generating.module.deps src/main.cpp
[ 0%]: <test> generating.module.deps src/foo.ixx
[ 0%]: <test> generating.module.deps /usr/local/Cellar/llvm/18.1.5/bin/../lib/c++/../../share/libc++/v1/std.cppm
[ 0%]: <test> generating.module.deps /usr/local/Cellar/llvm/18.1.5/bin/../lib/c++/../../share/libc++/v1/std.compat.cppm
[ 42%]: <test> compiling.module.release foo
[ 71%]: compiling.release src/main.cpp
[ 85%]: linking.release test
[100%]: build ok, spent 2.002s
Bot detected the issue body's language is not English, translate it automatically.
In addition, in my case, it can be compiled even without -fcoroutines, and there is no problem. Either your compiler version is low or there are other compiler problems.
target("test")
set_kind("binary")
add_files("src/*.cpp", "src/*.ixx")
ruki:test2 ruki$ xmake -r
[ 0%]: <test> generating.module.deps src/main.cpp
[ 0%]: <test> generating.module.deps src/foo.ixx
[ 0%]: <test> generating.module.deps /usr/local/Cellar/llvm/18.1.5/bin/../lib/c++/../../share/libc++/v1/std.cppm
[ 0%]: <test> generating.module.deps /usr/local/Cellar/llvm/18.1.5/bin/../lib/c++/../../share/libc++/v1/std.compat .cppm
[42%]: <test> compiling.module.release foo
[71%]: compiling.release src/main.cpp
[85%]: linking.release test
[100%]: build ok, spent 2.002s
/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/12/coroutine:361:2: error: "the coroutine header requires -fcoroutines"
你这里是 linux ,用了 clang ,却是默认找了 gcc 链的 c++ 头文件。。
可以尝试安装 clang 对应的 libc++ 库,然后切到 libc++。。
xmake f --toolchain=clang --runtime=c++_shared -c
xmake -rv
否则有可能默认用的还是 gcc 的 stdc++ 库。
anything else ? can we close this ?