Step by step guideline for v calling c library #20274
Replies: 6 comments 7 replies
-
The simplest thing to do is use the https://modules.vlang.io/db.sqlite.html wrapper that already exists. That page also tells you what to install to make it work. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot.I managed to get it works.
In the example guidline,I notice this line:
'''
rc := C.sqlite3_exec(db, &char(query_all_cities.str), my_callback, voidptr(7), &error_msg)
'''
May I ask what voidptr(7) the number '7' is supposed to mean here?
…________________________________
发件人: JalonSolov ***@***.***>
发送时间: 2023年12月26日 23:08
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Author ***@***.***>
主题: Re: [vlang/v] Step by step guideline for v calling c library (Discussion #20274)
Please read though this section of the docs: https://github.com/vlang/v/blob/master/doc/docs.md#v-and-c
There should be enough there to get you started.
Other examples will be in the V source, in any .c.v file.
―
Reply to this email directly, view it on GitHub<#20274 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FWSMJDALJM7RLAR6E3YLLR5NAVCNFSM6AAAAABBC3IH62VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSNBZGU3DA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Appreciated !
It seems there is something wrong with internet to github at my side.At the moment I can only read & reply mail from a external Email client,but not directly in github since I can't open it.
With the very precise and patient guidline,I can have sqlite3 work in V both with db.sqlite or directly interop with C.I felt happy!!
But when I tried to interop with curl,the complier complained "sys/socket.h" not found which should never be in Windows per my understanding.So I guess there is some other issue caused this.
This morning I read 'v help build-c' and then tried below:
v testsendmail_0.v -m64 -os windows -cc gcc
Now it passed.
Hope this is the right way to interop with curl.
```v
#flag windows ***@***.***/thirdparty/curl/include ***@***.***/thirdparty/curl/lib
#flag -lcurl
//#flag -lcrypto
//#flag -lssh2
//#flag -lssl
//#flag -lz
//#flag -lzstd
//#flag -lhttp2
//#flag -lhttp3
//#flag -ltcp2
#include "curl/curl.h"
pub struct C.CURL {
}
pub struct C.curl_slist {
}
pub struct C.upload_status
{
bytes_read int
}
fn main()
{
println('hello from curl')
}
```
Thanks and best regards,
Peter
…________________________________
发件人: JalonSolov ***@***.***>
发送时间: 2023年12月27日 22:28
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Author ***@***.***>
主题: Re: [vlang/v] Step by step guideline for v calling c library (Discussion #20274)
It is documented in the sqlite3 docs as
void *, /* 1st argument to callback */
In the example, the first argument to the callback is never used, so in this case, it's just a placeholder, to match the API.
That example was written a long time ago... it would be more proper V to write the callback function as
fn my_callback(_ voidptr, howmany int, cvalues &&char, cnames &&char) int {
to show explicitly that the value is ignored.
—
Reply to this email directly, view it on GitHub<#20274 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FXQTT62GC4M2V6AJFTYLQWBDAVCNFSM6AAAAABBC3IH62VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSNJXGE3DK>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I am asking help again by sending email because my network doesn't resume--
When I try to further get familar with v interop c,I met this kind of issue,that is macro in c .Given below code snippet,I don't know how to translate code from c to v,or how to set up a communication channel between them before starting communication,just because there is a macro CURLOPT which been used by enum CURLoption which in turn been used by curl_easy_setoption which is a variadic function.
curl defs begin:
//CURLOPT <-----------How to handle with this macro in V ?
#define CURLOPT(na,t,nu) na = t + nu
//CURLoption <----How to handle CURLoption because it uses macro CURLOPT?
typedef enum {
/* ...... */
CURLOPT(CURLOPT_VERBOSE, CURLOPTTYPE_LONG, 41),
CURLOPT(CURLOPT_PASSWORD, CURLOPTTYPE_STRINGPOINT, 174),
CURLOPT(CURLOPT_READDATA, CURLOPTTYPE_CBPOINT, 9),
CURLOPT(CURLOPT_SSL_VERIFYPEER, CURLOPTTYPE_LONG, 64),
CURLOPT(CURLOPT_SSL_VERIFYPEER, CURLOPTTYPE_LONG, 64),
CURLOPT(CURLOPT_USE_SSL, CURLOPTTYPE_VALUES, 119),
CURLOPT(CURLOPT_USERNAME, CURLOPTTYPE_STRINGPOINT, 173),
CURLOPT(CURLOPT_PASSWORD, CURLOPTTYPE_STRINGPOINT, 174),
CURLOPT(CURLOPT_MAIL_FROM, CURLOPTTYPE_STRINGPOINT, 186),
CURLOPT(CURLOPT_MAIL_RCPT, CURLOPTTYPE_SLISTPOINT, 187),
/* .........*/
} CURLoption;
curl defs end.
For easy reference,I paste the entire c source code here,which I have tested with gcc -o testmail testmail.c libcurl.a -lcurl.dll under windows 10 64 bit.
Thanks for the help in advance.
sendmail.c source begin:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define SMTP_TYPE "smtp://real.smtp.com:25"
#define FROM_ADDR ***@***.***"
#define PASSWORD "real_pass_word"
#define TO_ADDR ***@***.***"
#define CC_ADDR ***@***.***"
#define FROM_MAIL "name_or_email_address_from_sender"
#define TO_MAIL "name_or_email_address_of_receiver"
#define CC_MAIL "name_or_email_address_of_cc_receiver"
static const char *payload_text =
"To: " TO_MAIL "\r\n"
"From: " FROM_MAIL "\r\n"
"Cc: " CC_MAIL "\r\n"
"Subject: SMTP example message\r\n"
"\r\n" /* empty line to divide headers from body, see RFC 5322 */
"The body of the message starts here.\r\n"
"\r\n"
"It could be a lot of lines, could be MIME encoded, whatever.\r\nGood Luck!\r\nGood Luck!\r\n"
"Check RFC 5322.\r\n";
struct upload_status {
size_t bytes_read;
};
static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
{
struct upload_status *upload_ctx = (struct upload_status *)userp;
const char *data;
size_t room = size * nmemb;
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
return 0;
}
data = &payload_text[upload_ctx->bytes_read];
if(data) {
size_t len = strlen(data);
if(room < len)
len = room;
memcpy(ptr, data, len);
upload_ctx->bytes_read += len;
return len;
}
return 0;
}
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *recipients = NULL;
struct upload_status upload_ctx = { 0 };
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,SMTP_TYPE );
curl_easy_setopt(curl,CURLOPT_USERNAME,FROM_MAIL);
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM_ADDR);
curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);
recipients = curl_slist_append(recipients, TO_ADDR);
recipients = curl_slist_append(recipients, CC_ADDR);
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl,CURLOPT_VERBOSE,1);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl,CURLOPT_USE_SSL,CURLUSESSL_ALL);
curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
}
getchar();
return (int)res;
}
sendmail.c source end.
…________________________________
发件人: shove ***@***.***>
发送时间: 2023年12月28日 8:19
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Author ***@***.***>
主题: Re: [vlang/v] Step by step guideline for v calling c library (Discussion #20274)
Congratulations
—
Reply to this email directly, view it on GitHub<#20274 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FTCPI4W3OREGGCTSRDYLS3ITAVCNFSM6AAAAABBC3IH62VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSNRRGEYDI>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Could you please elaborate please?
…________________________________
发件人: JalonSolov ***@***.***>
发送时间: 2024年1月2日 9:56
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Author ***@***.***>
主题: Re: [vlang/v] Step by step guideline for v calling c library (Discussion #20274)
I think the simplest thing to do in this case would be to just run the C pre-processor step, saving the output to a file.
That will expand the macros into the actual values. Then use those actual values in your V source.
―
Reply to this email directly, view it on GitHub<#20274 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FXMC3ZFD7GUAYJWOADYMNSOLAVCNFSM6AAAAABBC3IH62VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSOBZHEYDM>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Get it.I prefer to wait for version 0.5 release.
Thank you.
…________________________________
发件人: JalonSolov ***@***.***>
发送时间: 2024年1月2日 10:33
收件人: vlang/v ***@***.***>
抄送: LukyGuyLucky ***@***.***>; Author ***@***.***>
主题: Re: [vlang/v] Step by step guideline for v calling c library (Discussion #20274)
It will depend on your C compiler. There is a way to run the pre-processor without completely compiling the code. Instead, it can output the C code after all includes have been included, and all macros expanded.
Do that, then look for that typedef enum to see what the values became once the macros were expanded. Use those values in your V code.
You'll have to look through the documentation for your C compiler to find out how to just run the pre-processor and output the result.
―
Reply to this email directly, view it on GitHub<#20274 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2QC2FQIDXKVIP4PEPUKDKDYMNWWHAVCNFSM6AAAAABBC3IH62VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM3TSOJQGAZTE>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I tried to play around with sqlite3 c libs but wherever I placed sqlite3 package and whatever I set the system environment PATH variable for sqlite3,the v compiler just complains he/she can't find sqlite3.h.I have tried to place the sqlite3 package folder as sqlite/all_c_and_h_files in the same folder of v.exe,in ~/vmouldes,in v_root_folder/thirdparty,in this samples folder under src as src/sqlite/,or say,e:/sqlite and set system environment PATH to add this path in.Trying to place sqlite3.h in the same folder with this v source file the compiler still can't see it.
What should I do exactly?
Thanks for the help in advance.
Beta Was this translation helpful? Give feedback.
All reactions