Skip to content
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

Can't compile generated C #22762

Open
downfall85 opened this issue Nov 4, 2024 · 4 comments
Open

Can't compile generated C #22762

downfall85 opened this issue Nov 4, 2024 · 4 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@downfall85
Copy link

downfall85 commented Nov 4, 2024

Describe the bug

I would like to write a shared library in V and compile it in C to then use it in a C++ program.
When I try to compile the C++ program, I have errors that are found in the C file that was generated by V

Reproduction Steps

Here is the V file:


@[export: 'get_name']
fn get_name() &char {
    return c'Test'
}

I used the following command to compile it to name.c: v -shared -gc none -o name.c name.v.

Here is the cpp file:

#include <iostream>

extern "C" {
    char* get_name(void);
}

int main(int argc, char *argv[])
{
    std::cout << get_name() << '\n';
}

And here is the command I use to compile the whole thing: cl main.cpp name.c /Fe:main.exe.

And finally the error (sorry it's in french).

Copyright (C) Microsoft Corporation. Tous droits réservés.

main.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\ostream(787): warning C4530: Gestionnaire d'exceptions C++ utilisé, mais les sémantiques de déroulement n'ont pas été activées. Spécifiez /EHsc
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\ostream(787): note: le contexte d’instanciation du modèle (le plus ancien) est
main.cpp(9): note: voir la référence à l'instanciation de la fonction modèle 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' en cours de compilation
Génération de code en cours...
Compilation en cours...
name.c
name.c(1724): error C2143: erreur de syntaxe : absence de ')' avant '('
name.c(1724): error C2059: erreur de syntaxe : ')'
name.c(1724): error C2059: erreur de syntaxe : ')'
name.c(1724): error C2146: erreur de syntaxe : absence de ')' avant l'identificateur 'unhandled_exception_handler'
name.c(1724): error C2091: fonction retournée par la fonction
name.c(1724): error C2061: erreur de syntaxe : identificateur 'unhandled_exception_handler'
name.c(1724): error C2059: erreur de syntaxe : ';'
name.c(1724): error C2059: erreur de syntaxe : '<parameter-list>'
name.c(7255): warning C4028: paramètre formel 1 différent de la déclaration
name.c(7623): warning C4477: 'fprintf' : la chaîne de format '%ld' nécessite un argument de type 'long', mais l'argument variadique 1 est de type 'isize'
name.c(7623): note: utilisez '%td' dans la chaîne de format
name.c(8109): error C2143: erreur de syntaxe : absence de ')' avant '('
name.c(8109): error C2059: erreur de syntaxe : ')'
name.c(8109): error C2059: erreur de syntaxe : ')'
name.c(8109): error C2146: erreur de syntaxe : absence de ')' avant l'identificateur 'unhandled_exception_handler'
name.c(8109): error C2091: fonction retournée par la fonction
name.c(8109): error C2061: erreur de syntaxe : identificateur 'unhandled_exception_handler'
name.c(8109): error C2059: erreur de syntaxe : ';'
name.c(8109): error C2059: erreur de syntaxe : '<parameter-list>'
name.c(8125): error C2065: 'unhandled_exception_handler' : identificateur non déclaré
name.c(8125): warning C4312: 'cast de type' : la conversion de 'int' en 'voidptr' d'une taille supérieure

Expected Behavior

I should be able to compile the C++ file.

Current Behavior

It seems that msvc is not able to compile the file generated c file because I have compilation errors.

Possible Solution

I works with the option -cc g++: v -shared -gc none -cc g++ -o name.c name.v

Note that with this solution, the code compiles but I still have the warnings

Additional Information/Context

No response

V version

V 0.4.8 6c94c24

Environment details (OS name and version, etc.)

Windows 11
Visual Studio 17 2022

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@downfall85 downfall85 added the Bug This tag is applied to issues which reports bugs. label Nov 4, 2024
@KeitoTobi1
Copy link
Contributor

I Have Same Problem.

@felipensp
Copy link
Member

felipensp commented Nov 5, 2024

On Linux (as shared library):

# v -cc gcc -skip-unused -d no_main -no-builtin -shared -gc none -o name.so name.v
# g++ main.cpp name.so
# LD_LIBRARY_PATH=. ./a.out 
Test

@felipensp
Copy link
Member

felipensp commented Nov 5, 2024

Compiling .c and .cpp files separately and linking later (object files):

# v -d no_main -skip-unused -no-builtin -gc none -o name.c name.v
# g++ -x c -c name.c -o name.o
# g++ -c main.cpp -o main.o
# g++ main.o name.o
# ./a.out 
Test

@felipensp felipensp added the Unit: cgen Bugs/feature requests, that are related to the default C generating backend. label Nov 5, 2024
@felipensp
Copy link
Member

Can you try again building from master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

3 participants