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.8k stars 2.16k forks source link

Macos uses db.mysql,C error found #21555

Open Avey777 opened 5 months ago

Avey777 commented 5 months ago

V doctor:

V full version: V 0.4.6 bfd53a9.2b1be90
OS: macos, macOS, 13.2.1, 22D68
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz

getwd: /Users/avey/Documents/Dev/vprod
vexe: /Applications/v/v
vexe mtime: 2024-05-23 12:47:40

vroot: OK, value: /Applications/v
VMODULES: OK, value: /Users/avey/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.2 (Apple Git-143)
Git vroot status: weekly.2024.16.1-259-g2b1be908
.git/config present: true

CC version: Apple clang version 14.0.3 (clang-1403.0.22.14.1)
thirdparty/tcc status: thirdparty-macos-amd64 46662e20

What did you do? v -run main.v

import db.mysql

fn main() {
    mut conn := mysql.connect(
        host: 'localhost'
        port: 3306
        username: 'root'
        password: 'xxxxxx'
        dbname: 'development'
    )!
    res := conn.query('show tables')!
    for row in res.rows() {
        println(row.vals.join(', '))
    }
    conn.close()
}

What did you expect to see?

What did you see instead?

avey@avey vprod % v run .
==================
                                                           ^
/tmp/v_501/vprod.01HYJRCRR78K1BKP31CYHGT5E0.tmp.c:39654:6: error: call to undeclared function 'mysql_refresh'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        if (mysql_refresh(db->conn, options) != 0) {
            ^
/tmp/v_501/vprod.01HYJRCRR78K1BKP31CYHGT5E0.tmp.c:39715:42: warning: passing 'const char *' to parameter of type 'u8 *' (aka 'unsigned char *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        string _t1 = db__mysql__resolve_nil_str(mysql_info(db->conn));
                                                ^~~~~~~~~~~~~~~~~~~~
/tmp/v_501/vprod.01HYJRCRR78K1BKP31CYHGT5E0.tmp.c:6964:55: note: passing argument to parameter 'ptr' here
VV_LOCAL_SYMBOL string db__mysql__resolve_nil_str(u8* ptr);
                                                      ^
/tmp/v_501/vprod.01HYJRCRR78K1BKP31CYHGT5E0.tmp.c:39723:26: warning: passing 'const char *' to parameter of type 'u8 *' (aka 'unsigned char *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        string _t1 = u8_vstring(mysql_get_host_info(db->conn));
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
avey@avey vprod %

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

Avey777 commented 5 months ago

@JalonSolov
Can you help me with this issue? I'm going to change my development machine to MacOS, but the same code doesn't work properly.

spytheman commented 5 months ago

Do brew install mariadb-connector-c . After that, the compilation should succeed.

Avey777 commented 5 months ago

Do . After that, the compilation should succeed.brew install mariadb-connector-c

@spytheman

avey@avey vprod % brew install mariadb-connector-c
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
vedic
==> New Casks
blitz-gg                         font-playwrite-es-deco           font-playwrite-nz
font-playwrite-au-nsw            font-playwrite-fr-moderne        font-playwrite-tz
font-playwrite-au-qld            font-playwrite-id                font-playwrite-us-modern
font-playwrite-au-sa             font-playwrite-ie                font-playwrite-us-trad
font-playwrite-au-tas            font-playwrite-in                font-playwrite-za
font-playwrite-au-vic            font-playwrite-it-moderna        hopper-disassembler
font-playwrite-ca                font-playwrite-it-trad           mac-mouse-fix@2
font-playwrite-de-la             font-playwrite-mx
font-playwrite-es                font-playwrite-ng-modern

==> Fetching mariadb-connector-c
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/mariadb-connector-c-3.3.8.ventura.bo
########################################################################################## 100.0%
==> Pouring mariadb-connector-c-3.3.8.ventura.bottle.tar.gz
🍺  /usr/local/Cellar/mariadb-connector-c/3.3.8: 154 files, 1.4MB
==> Running `brew cleanup mariadb-connector-c`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
avey@avey vprod % v run .
==================
                                                           ^
/tmp/v_501/vprod.01HYTDZGRX2RM40608QES0N7XJ.tmp.c:35966:6: error: call to undeclared function 'mysql_refresh'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        if (mysql_refresh(db->conn, options) != 0) {
            ^
/tmp/v_501/vprod.01HYTDZGRX2RM40608QES0N7XJ.tmp.c:36018:42: warning: passing 'const char *' to parameter of type 'u8 *' (aka 'unsigned char *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        string _t1 = db__mysql__resolve_nil_str(mysql_info(db->conn));
                                                ^~~~~~~~~~~~~~~~~~~~
/tmp/v_501/vprod.01HYTDZGRX2RM40608QES0N7XJ.tmp.c:6966:55: note: passing argument to parameter 'ptr' here
VV_LOCAL_SYMBOL string db__mysql__resolve_nil_str(u8* ptr);
                                                      ^
/tmp/v_501/vprod.01HYTDZGRX2RM40608QES0N7XJ.tmp.c:36023:26: warning: passing 'const char *' to parameter of type 'u8 *' (aka 'unsigned char *') discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
        string _t1 = u8_vstring(mysql_get_host_info(db->conn));
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
avey@avey vprod %

I have executed brew install mysql-connector-c before, but I still get this error. It should not be a problem with the mysql environment configuration.

Avey777 commented 5 months ago

1、

run find /opt/homebrew/ |grep mysql.h$ and paste the results
if that fails, do
find /usr/local/Cellar/ |grep mysql.h$ and paste the results

2、 brew install pkg-config

3、

pkg-config libmariadb --cflags --libs
pkg-config mysqlclient --cflags --libs

4、

brew install mariadb-connector-c

instead of

brew install mysql-clinet

5、

v -cflags '-I/usr/local/Cellar/mariadb-connector-c/3.3.8/include/mariadb/ -L/usr/local/Cellar/mariadb-connector-c/3.3.8/lib/mariadb/ -lmariadb' run main.v

eg: the mysql 8.3 headers lack the mysql_refresh function and some others (they were deprecated)

medvednikov commented 5 months ago

I'll fix the C driver issue for all 3 DBs today.

The C driver will be included, and none of this will be needed.

Avey777 commented 5 months ago

I'll fix the C driver issue for all 3 DBs today.

The C driver will be included, and none of this will be needed.

Has this been fixed? I didn't find a similar fix code commit

spytheman commented 5 months ago

No, it is not fixed.

The C driver will be included, and none of this will be needed.

It can be done cleanly only for db.sqlite, since SQLite provides an amalgamation that contains the whole DB. You can use v install sqlite and then import sqlite, instead of import db.sqlite.

This issue however is for mysql, and mysql's client side libraries, are not unfortunately as easy to compile and use, as SQLite ones are, that is why we try to use pkgconfig, so that we can link to the already compiled ones.

Given this strategy, the issue happens, because our version of pkgconfig, on macos looks for /opt/homebrew/lib/pkgconfig for the .pc files on macos, while brew on macos for Intel, stores its files under /usr/local/Cellar, not under /opt/homebrew .

ttytm commented 5 months ago

A thing you could do already, is adding the different flags directly in you program.

To give an example of the concept:

// Intel, M1 brew, and MacPorts
#flag darwin -I/usr/local/opt/openblas/include -I/opt/homebrew/opt/openblas/include -I/opt/local/opt/openblas/include
#flag darwin -L/usr/local/opt/openblas/lib -L/opt/homebrew/opt/openblas/lib -L/opt/local/opt/openblas/lib
#flag darwin -L/usr/local/opt/lapack/lib -L/opt/homebrew/opt/lapack/lib -L/opt/local/opt/lapack/lib

The above is taken from: https://github.com/vlang/vsl/blob/main/vlas/cflags_d_vsl_vlas_cblas.v:

Also checking for the existence of a pkg-config is possible:

$if $pkgconfig('mysqlclient') {
    #pkgconfig mysqlclient
} $else $if $pkgconfig('mariadb') {
    #pkgconfig mariadb
}