Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ringabout
ringabout

Posted on

     

Wrap const char* in the Nim language

Nim language has a great FFI with C/C++ which makes it easy to use C/C++ libraries. This article introduces
how to wrapconst char* in C/C++ backend.

In the Nim world, we often play withstring which handles the dirty works for us such as resizing the buffer, creating/deleting the memory which can satisfy the most of our needs. However, if we want to interact with other languages, we need the power ofcstring. Note thatcstring doesn't mean "string in C", it means compatible string(char * in C/C++ backend, JS string in JavaScript backend and so on).

Nim doesn't provideconst char * for us. But it is easy to make our own one.

You need to importconst char* types from C/C++ backend usingimportc pragmas. Then make a distinct type for it.

typecstringConstImpl{.importc:"const char*".}=cstringconstChar*=distinctcstringConstImpl
Enter fullscreen modeExit fullscreen mode

Now, let's look at a simple example using it.

Firstemit the c function, then write the function declaration(nodecl pragmas means that we
do not generate the function declaration in the output codes). Finally we construct aconstChar object and
pass it to thefn proc.

{.emit:"""int fn(const char* a, char b) {  return 1;}""".}procfn(a:constChar,b:char):int{.importc:"fn",nodecl.}varx=constChar("abc")doAssertfn(x,'c')==1doAssertnotcompiles(fn("abc",'b'))doAssertnotcompiles(fn("abc".cstring,'b'))
Enter fullscreen modeExit fullscreen mode

We could see that thefn proc doesn't acceptstring andcstring types and only theconstChar type is allowed. After looking at the generated C codes usingnim c -r --nimcache:nimcache app.nim ornim cpp -r --nimcache:nimcache app.nim, it does generateconst char* x = "abc";.

Lastly let's look at an example with C++ backend, it has the same story as above.

Now we need to create a new.h file and overload thefn functions.

fun.h

intfn(constchar*a,charb){return1;}intfn(char*a,charb){return2;}
Enter fullscreen modeExit fullscreen mode

fun.nim

procfn(a:constChar,b:char):int{.importcpp:"$1(@)",header:"fun.h".}=discardprocfn(a:cstring,b:char):int{.importcpp:"$1(@)",header:"fun.h".}=discardvara=constChar("abs")doAssertfn(a,'b')==1varb=cstring("abc")doAssertfn(b,'b')==2
Enter fullscreen modeExit fullscreen mode

Run the codes above usingnim cpp -r fun.nim and it works!

Reference:
https://github.com/nim-lang/Nim/issues/3720

Nim is a great language. Let's port and wrap more libraries for it.

https://github.com/nim-lang/needed-libraries/issues

https://github.com/nothings/single_file_libs

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
apollodevs profile image
Ahmed Muhammed Galadima
A 16 year old learning data science.
  • Email
  • Location
    Jeddah,Saudi Arabia
  • Education
    Self-Taught
  • Work
    Data scientist (in-training) at none
  • Joined

im still a noob to nim just started learning this week but i just wanna say .. you are a really amazing person and i hope you keep up with this content! thanks hopefully one day nim becomes as popular as it should be

CollapseExpand
 
kaushalmodi profile image
Kaushal Modi
  • Joined

Hello, thanks for sharing this! I was led to this post fromthis recent question post on const char by me on Nim Forums.

As a follow up, I am some basic questions ..

  1. How can we make thisconstChar type main stream so that every C wrapper library does not need to define this. Those libraries would also need to export this type and then we will end up with type clashes if a user imports multiple of such libraries.
  2. As the type is a distinct type, we cannot use this type until we define$,[] and may be more low level ops.
  • Do you have a full-fledged library for these const types?
  • Is there a plan to bake this into a nim stdlib?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromringabout

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp