hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.24k stars 224 forks source link

[BUG] Trying to use pure mode with a header file #1067

Closed mike919192 closed 1 week ago

mike919192 commented 1 month ago

Describe the bug When cppfront is run in pure mode it doesn't seem to handle including h2 file from cpp2 file correctly.

To Reproduce hello.cpp2

#include "hello.h2"

main: () = {

    std::cout << "Hello " << name() << "\n";
}

hello.h2

name: () -> std::string = {
    s: std::string = "world";
    decorate(s);
    return s;
}

decorate: (inout s: std::string) = {
    s = "[" + s + "]";
}

Compiler output:

abc@4e0512100c8d:~/workspace/test_cppfront$ ../cppfront/source/cppfront -p *.cpp2 *.h2
hello.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks)

hello.h2... ok (all Cpp2, passes safety checks)

abc@4e0512100c8d:~/workspace/test_cppfront$ g++ -I../cppfront/source -std=c++20 -g *.cpp -o hello
/usr/bin/ld: /tmp/ccVKnHmH.o: in function `main':
/config/workspace/test_cppfront/hello.cpp2:8: undefined reference to `name[abi:cxx11]()'
collect2: error: ld returned 1 exit status

I noticed that in pure mode, the h2 file generates both a h file and a hpp file. I don't know if this is the best solution but it can be fixed by adding an #include "hello.hpp to the generated cpp file.

hello.cpp

#define CPP2_IMPORT_STD          Yes

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

#include "cpp2util.h"

#line 1 "hello.cpp2"

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

#line 1 "hello.cpp2"
//#include <iostream>
//#include <string>

#include "hello.h"
#include "hello.hpp" //this was missing, added to temporarily fix

#line 6 "hello.cpp2"
auto main() -> int;

//=== Cpp2 function definitions =================================================

#line 1 "hello.cpp2"

#line 6 "hello.cpp2"
auto main() -> int{

    std::cout << "Hello " << name() << "\n";
}

Additional context Both the release 0.7.0 and the head behave the same.

Discussed in https://github.com/hsutter/cppfront/discussions/1066

Originally posted by **mike919192** May 12, 2024 Hi. I've had success with cppfront in mixed mode, but decided to try a simple example in pure mode and I'm running into an issue. I can use pure mode with a single cpp2 file, but I am getting an error trying to include a h2 file. hello.cpp2 ``` #include "hello.h2" main: () = { std::cout << "Hello " << name() << "\n"; } ``` hello.h2 ``` name: () -> std::string = { s: std::string = "world"; decorate(s); return s; } decorate: (inout s: std::string) = { s = "[" + s + "]"; } ``` Compiler output: ``` abc@4e0512100c8d:~/workspace/test_cppfront$ ../cppfront/source/cppfront -p *.cpp2 *.h2 hello.cpp2... ok (mixed Cpp1/Cpp2, Cpp2 code passes safety checks) hello.h2... ok (all Cpp2, passes safety checks) abc@4e0512100c8d:~/workspace/test_cppfront$ g++ -I../cppfront/source -std=c++20 -g *.cpp -o hello /usr/bin/ld: /tmp/ccVKnHmH.o: in function `main': /config/workspace/test_cppfront/hello.cpp2:8: undefined reference to `name[abi:cxx11]()' collect2: error: ld returned 1 exit status abc@4e0512100c8d:~/workspace/test_cppfront$ ``` I also noticed that the h2 file is generating both a h file and also hpp file. Am I doing something wrong trying to include the header? If I move the function definitions into the cpp2 then the pure mode is working.
hsutter commented 1 week ago

I think this was repeated in #1066, closing as duplicate.