vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.82k stars 2.17k forks source link

Can't compile generated C #22762

Open downfall85 opened 1 week ago

downfall85 commented 1 week ago

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.

KeitoTobi1 commented 1 week ago

I Have Same Problem.

felipensp commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

Can you try again building from master?