Skip to content

module support for cpp2 files #162

Open
@adam-ce

Description

@adam-ce

if i understand correctly, this is still something for the future, but to my understanding it's not possible to make libraries with cpp2 files.

e.g., i followed a module example:
cmake file:

cmake_minimum_required(VERSION 3.30)

project(cpp2test LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -lc++abi")
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)

FetchContent_Declare(
    cppfront
    GIT_REPOSITORY https://github.com/modern-cmake/cppfront.git
    GIT_TAG main  # or an actual git SHA if you don't like to live dangerously
)

FetchContent_MakeAvailable(cppfront)


add_library(foo)
# Add the module file to the library
target_sources(foo
  PUBLIC
    FILE_SET CXX_MODULES FILES
      Shape.cpp
)
set_target_properties(foo PROPERTIES LINKER_LANGUAGE CXX)
# Create an executable
add_executable(cpp2test main.cpp2)
# Link to the library foo
target_link_libraries(cpp2test PUBLIC foo)

with Shape.cpp

export module Shape;

export int add(int a, int b) {
    return a + b + 42;
}

and main.cpp2

import Shape;

main : () = {
           std::cout << "Hello, world! " << add(1, 2) << "\n";
}

this compiles and runs. but i get errors as soon as i rename Shape.cpp to Shape.cpp2:

[cmake] CMake Error in CMakeLists.txt:
[cmake]   Target "foo" has source file
[cmake] 
[cmake]     /home/madam/Documents/cpp2test/Shape.cpp2
[cmake] 
[cmake]   in a "FILE_SET TYPE CXX_MODULES" but it is not scheduled for compilation.

i already added set_target_properties(foo PROPERTIES LINKER_LANGUAGE CXX), but it appears that that's not enough.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions