V: Calling C Functions from V doesn't work

Created on 23 Jun 2019  路  6Comments  路  Source: vlang/v

According with the documentation the example code which uses c libraries inside V must work, but that not works, when i try to compile i get:

panic: sqlix.v:6
use struct sqlite3 { instead of type sqlite3 struct {
exit():

To understand how to deal with this, will be great working / updating examples

Bug

Most helpful comment

Hello,

I'm aware of this bug, it will be fixed asap.

All 6 comments

Yes, i have i try that but after that a next problem, that's why i post an issue (probably will be another and another):

#flag -lsqlite3

#include "sqlite3.h"

struct C.sqlite3 {}
struct C.sqlite3_stmt {} 

fn C.sqlite3_column_int(C.sqlite_stmt, int) int {}

fn main() {
    path := 'sqlite3_users.db' 
    db := &C.sqlite3{} 
    C.sqlite3_open(path.cstr(), &db)
    query := 'select count(*) from users' 
    stmt := &C.sqlite3_stmt{} 
    C.sqlite3_prepare_v2(db, query.cstr(), - 1, &stmt, 0)
    C.sqlite3_step(stmt) 
    nr_users := C.sqlite3_column_int(res, 0)
    C.sqlite3_finalize(res)
    println(nr_users) 
} 

Result now:

check()
next token = fn
pass=1 fn=` panic: sqlix.v:10 expectedNAMEbut got}`

I'm trying to implement a socket module and am running into a similar issue

module socket

#include <sys/socket.h>

pub fn socket(domain int, _type int, proto int) {
        sockfd := C.socket(domain, _type, proto)
}

When I compile I get

check()
next token = domain
pass=2 fn socket__socket
panic: socket.v:6
expected . but got (

If I compare p.tok and DOT here, before entering the check they'll match (as expected), but if I compare p.tok and DOT inside the check (right here) they differ

Hello,

I'm aware of this bug, it will be fixed asap.

In many cases, for now the following hack will work:

add an empty function fn foobar() {} after your C function definitions:

fn C.fn1()
fn C.fn2()

fn foobar() {}

...

I am closing this issue because it comes from v 0.1.7 and we are at 0.1.18 already. Many things have changed, especially concerning this topic. If these problems are still persistent, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

oleg-kachan picture oleg-kachan  路  3Comments

taojy123 picture taojy123  路  3Comments

penguindark picture penguindark  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments