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
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'))
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;}
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
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.
Top comments(2)

- Email
- LocationJeddah,Saudi Arabia
- EducationSelf-Taught
- WorkData 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

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 ..
- How can we make this
constChar
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. - 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?
For further actions, you may consider blocking this person and/orreporting abuse