modern-cmake / cppfront

CMake wrapper around the cppfront repository
BSD 3-Clause "New" or "Revised" License
69 stars 16 forks source link

None Modules - how should I split `Cpp2 type definitions and function declarations` to `.h` file ? #139

Closed shemeshg closed 1 week ago

shemeshg commented 2 months ago

I've created test.cpp2

gogo: () -> int = {
    return 2123;
}

I do not use modules, how do I include that?

I've created manually test.h with #pragma once in the same source folder with the

#pragma once

//=== Cpp2 type declarations ====================================================

#include "cpp2util.h"

#line 1 "/Volumes/RAM_Disk_4G/TestCppFront2/src/test.cpp2"

//=== Cpp2 type definitions and function declarations ===========================

#line 1 "/Volumes/RAM_Disk_4G/TestCppFront2/src/test.cpp2"
[[nodiscard]] auto gogo() -> int;

but I'm not sure it is the right way of doing it, because I have to update that file manually whenever gogo signature changes.

I've been thinking maybe sed can help, because cppfront has no switch todo so

echo '#pragma once' > test.h
sed -n '/=== Cpp2 type declarations/,/=== Cpp2 type definitions and function declarations/p' test.cpp >> test.h
alexreinking commented 1 month ago

I'm sorry it's been a bit since I looked at cpp2... can you help me create an MRE? Not sure what the intended workflow is

shemeshg commented 1 month ago
mkdir test
cd test

CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
project(example)

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_executable(main main.cpp2 test.cpp2)

test.cpp2

gogo: () -> int = {
    return 2123;
}

main.cpp2

#include "test.cpp"
main: () = {
    s: std::string = "Fred";
    std::cout <<gogo()<< "Hello, world!\n";
}

openning vscode build yield

[build] ld: 1 duplicate symbols
[build] clang: error: linker command failed with exit code 1

Creating .h from cpp

echo '#pragma once' > test.h
sed -n '/=== Cpp2 type declarations/,/=== Cpp2 type definitions and function declarations/p' build/_cppfront/test.cpp >> test.h

change main.cpp2 to

#include "test.h"
main: () = {
    s: std::string = "Fred";
    std::cout <<gogo()<< "Hello, world!\n";
}
shemeshg commented 3 weeks ago

I think the problem is here

https://github.com/hsutter/cppfront/issues/594

No way to produce separated .h files without -pure-cpp2, however export in modules is not supported yet.

shemeshg commented 3 weeks ago

ok, thanks to https://github.com/isidorostsa/RayTrayCpp2

I think settings the CMakeLists.txt to

cmake_minimum_required(VERSION 3.23)
project(example)

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_executable(main main.cpp2)

set(cpp2headers 
    test.h2
)
#set(CPPFRONT_FLAGS "-pure-cpp2")
cppfront_generate_cpp(cpp1headers ${cpp2headers})

target_sources(main PUBLIC ${cpp1headers})

actually works, it does not produce include files but it compiles, unlike putting h2 files directly to target_sources...