xmake icon indicating copy to clipboard operation
xmake copied to clipboard

C++20 Module Compilation Fails Due to Missing fmt Include Path in Xmake Build

Open lizh1999 opened this issue 1 year ago • 3 comments

Xmake Version

xmake v2.9.5+HEAD.d30de52e9

Operating System Version and Architecture

Windows 10 19045.2846

Describe Bug

When attempting to build a project using C++20 modules with the fmt library, xmake fails to find the header file fmt/core.h during the compilation of the mod.mpp module. The error logs indicate that the include path for the fmt library is not properly passed to the compiler, resulting in a fatal error: C1083: Cannot open include file: 'fmt/core.h': No such file or directory. This suggests that the include path for fmt is not being added to the compilation command for the module, even though fmt is correctly required in xmake.lua.

Expected Behavior

The project should compile successfully, and the program should output the formatted string Hello world! as expected.

Project Configuration

-- xmake.lua
add_rules("mode.debug", "mode.release")

add_rules("plugin.compile_commands.autoupdate")

add_requires("fmt")

set_languages("c++20")

target("mod", function () 
  set_kind("moduleonly")
  add_files("mod.mpp", { public = true })
  add_packages("fmt")
  set_policy("build.c++.modules", true)
end)

target("main", function () 
  set_kind("binary")
  add_files("main.cpp")
  add_deps("mod")
  set_policy("build.c++.modules", true)
end)
// main.cpp
#include <iostream>

import mod;

int main() {
  std::cout << mod::format("Hello {}", "world!") << std::endl;
  return 0;
}
// mod.mpp
module;

#include "fmt/core.h"

export module mod;

export namespace mod {
using fmt::format;
}

Additional Information and Error Logs

checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe
checking for Microsoft Visual Studio (x64) version ... 2022        
checking for Microsoft C/C++ Compiler (x64) version ... 19.40.33812
checking for zig ... no
checking for zig ... no
checking for nim ... no
checking for nim ... no
checking for git ... ok
checking for gzip ... no
checking for 7z ... C:\xmake\winenv\bin\7z
git rev-parse HEAD
checking for cmake ... no
checking for cmake ... no
checking for cmake ... no
checking for xmake::fmt ... fmt 11.0.2
checking for cl.exe ... C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe
checking for the c++ compiler (cxx) ... cl.exe
checking for link.exe ... C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\link.exe
checking for the linker (ld) ... link.exe
checking for C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe ... ok
checking for flags (cl_scan_dependencies) ... ok
checking for flags (cl_ifc_output) ... ok
[  0%]: <main> generating.module.deps mod.mpp
checking for flags (-O2) ... ok
checking for flags (-std:c++20) ... ok
checking for flags (-DNDEBUG) ... ok
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe -nologo -O2 -std:c++20 /EHsc -DNDEBUG -TP -scanDependencies build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567\mod.mpp.module.json mod.mpp -ifcOutput build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567 -Fobuild\.objs\main\windows\x64\release\mod.mpp.obj
mod.mpp
mod.mpp(3): fatal error C1083: 无法打开包括文件: “fmt/core.h”: No such file or directory
[  0%]: <main> generating.module.deps mod.mpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe -nologo -O2 -std:c++20 /EHsc -DNDEBUG -TP -scanDependencies build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567\mod.mpp.module.json mod.mpp -ifcOutput build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567 -Fobuild\.objs\main\windows\x64\release\mod.mpp.obj
mod.mpp
mod.mpp(3): fatal error C1083: 无法打开包括文件: “fmt/core.h”: No such file or directory
error: execv(C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\bin\HostX64\x64\cl.exe -nologo -O2 -std:c++20 /EHsc -DNDEBUG -TP -scanDependencies build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567\mod.mpp.module.json mod.mpp -ifcOutput build\.gens\main\windows\x64\release\rules\bmi\cache\modules\2e1fd567 -Fobuild\.objs\main\windows\x64\release\mod.mpp.obj) failed(2)

lizh1999 avatar Sep 30 '24 03:09 lizh1999

moduleonly target 本身不会编译,是在父 target 里面编译这些 mpp的,所以 package 你也得配置上 public ,目前还不支持对 moduleonly 自动设置 public

waruqi avatar Oct 03 '24 15:10 waruqi

Bot detected the issue body's language is not English, translate it automatically.


The moduleonly target itself will not be compiled. These mpps are compiled in the parent target, so you must also configure public for the package. Currently, it is not supported to automatically set public for moduleonly.

Issues-translate-bot avatar Oct 03 '24 15:10 Issues-translate-bot

with

-- xmake.lua
add_rules("mode.debug", "mode.release")

add_rules("plugin.compile_commands.autoupdate")

add_requires("fmt")

set_languages("c++20")

target("mod", function () 
  set_kind("moduleonly")
  add_files("mod.mpp", { public = true })
  add_packages("fmt", { public = true }) -- <---- change here
  set_policy("build.c++.modules", true)
end)

target("main", function () 
  set_kind("binary")
  add_files("main.cpp")
  add_deps("mod")
  set_policy("build.c++.modules", true)
end)

it's working, anything else ? can we close this ?

Arthapz avatar May 07 '25 14:05 Arthapz