Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shemeshg opened this issue Apr 30, 2024 · 4 comments

Comments

@shemeshg
Copy link

shemeshg commented Apr 30, 2024

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
@shemeshg shemeshg changed the title how to produce Cpp2 type definitions and function declarations to .h file ? how to supperate Cpp2 type definitions and function declarations to .h file ? Apr 30, 2024
@shemeshg shemeshg changed the title how to supperate Cpp2 type definitions and function declarations to .h file ? how should I split Cpp2 type definitions and function declarations to .h file ? Apr 30, 2024
@shemeshg shemeshg changed the title how should I split Cpp2 type definitions and function declarations to .h file ? None Modules - how should I split Cpp2 type definitions and function declarations to .h file ? Apr 30, 2024
@alexreinking
Copy link
Contributor

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
Copy link
Author

shemeshg commented Jun 1, 2024

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
Copy link
Author

shemeshg commented Jun 8, 2024

I think the problem is here

hsutter/cppfront#594

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

@shemeshg
Copy link
Author

shemeshg commented Jun 10, 2024

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants