Linking errors with includes in C++ nested modules

40 views Asked by At

Here a toy project that produces the error.

mod1.ccm

module;
#include <string> // producing the linking error
export module mod1;

mod2.ccm

module;
#include <string>
export module mod2;

import module mod1;
export std::string s = "mystring";

main.cc

#include <iostream>
import mod2;
int main(){
    std::cout << s << std::endl;
    return 0;
}

The project's configuration is done with the following CMakeLists.txt file

cmake_minimum_required(VERSION 3.28.3)

project(project LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)

add_library(core)
target_sources(core
  PUBLIC
    FILE_SET CXX_MODULES FILES
    FILES
      src/core/mod1.ccm
      src/core/mod2.ccm
      
)

add_executable(main src/main.cc)

set_target_properties(core
  PROPERTIES
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
    COMPILE_WARNINGS_AS_ERRORS ON)
set_target_properties(main
  PROPERTIES
    CXX_STANDARD 20
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
    COMPILE_WARNINGS_AS_ERRORS ON)

target_link_libraries(main PRIVATE core)

When building it I get the following linking error:

[8/8] Linking CXX executable main
FAILED: main 
: && /usr/local/bin/clang++ -O3 -DNDEBUG  CMakeFiles/main.dir/src/main.cc.o -o main  libcore.a && :
/usr/bin/ld: libcore.a(layers.ccm.o): in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()':
layers.pcm:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED2Ev[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED2Ev]+0x18): undefined reference to `std::allocator<char>::deallocate(char*, unsigned long)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Built the project with clang++ 19 and MSVC (both with CMake 3.28), same error persists. This suggests me the issue isn't compiler-specific when it comes to c++ modules support.

0

There are 0 answers