Issue with C++ modules and gcc
Hello, the following behaves as expected with clang but not with gcc trunk. I had a look at what ninja is doing but I am not able to figure out if this a ninja or cmake or gcc issue.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.28) # With 3.28, no need for set(CMAKE_CXX_SCAN_FOR_MODULES ON).
project(std_module_example CXX)
set(CMAKE_CXX_STANDARD 20)
# Default to C++ extensions being off. Clang's modules support have trouble
# with extensions right now and it is not required for any other compiler
set(CMAKE_CXX_EXTENSIONS OFF)
add_library(foo)
target_sources(foo
PRIVATE
foo_impl.cxx
PUBLIC
FILE_SET cxx_modules TYPE CXX_MODULES FILES
foo.cxx
)
add_executable(hello main.cxx)
target_link_libraries(hello PRIVATE foo)
main.cxx:
import foo;
int main() {
foo f;
f.helloworld();
return 0;
}
foo.cxx:
module;
#include <iostream>
export module foo;
export class foo {
public:
foo();
~foo();
void helloworld();
};
export void fun() {
std::cout << "abc" << std::endl;
}
foo_impl.cxx:
module; // Start of the globl module fragment where #includes can happen
#include <iostream>
module foo;
foo::foo() = default;
foo::~foo() = default;
void foo::helloworld() { std::cout << "hello worldad \n"; }
The issue is that the second ninja invocation below rebuilds main.o and foo.o with gcc (clang handles this correctly):
rm -rf CMakeFiles/ CMakeCache.txt build.ninja libfoo.a hello cmake_install.cmake
cmake -DCMAKE_BUILD_TYPE=Debug -GNinja
ninja
touch foo_impl.cxx
ninja -v -d explain
Versions:
cmake --version
cmake version 3.28.0
ninja --version
1.12.0.git
# Build from source...
g++ -v
gcc version 14.0.0 20231211 (experimental) (GCC)
clang -v
clang version 16.0.0