Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to add Nim-lang code to C/C++ code - TUTORIAL

How to add Nim-lang code to C/C++ code


Note 1: This tutorial is for Linux users only, but if you understand the idea, you can use it on all systems and it will work as required :).
Note 2: Nim-lang version used in this tutorial is 1.0.4 (To get Nim-lang version nim -v)

To add Nim-lang code to C/C++ code you have 3 choices :

  • Compile Nim-lang code as a (C/C++ header File). THIS IS WHAT WE WILL TAKE TODAY.
  • Compile Nim-lang code as a (C/C++ static library).
  • Compile Nim-lang code as a (C/C++ dynamic library).

Compile Nim-lang code as a (C/C++ header File) and add it to C code :

1- Create a nimproj folder (Which contains all files).

mkdir nimproj
cd nimproj

2- Create a lib.nim file and main.c file

touch lib.nim 
touch main.c
But before we start writing any codes, there are a few things to keep in mind :
  • Any code in Nim to add to any other language must be inside a function.
  • Some errors may occur due to data types because the data types in Nim are not suitable to be used outside the Nim code, so there are some data types intended for use outside the code (meaning more accurately there are data types for Nim code only and other for the languages to which the Nim code is added).
We will look at this in examples

Example 1:

Open lib.nim with yout favorite code editor

code lib.nim

And write this code inside

proc HelloFromNim(): cstring {.exportc.} = 
    return "Hello, World From Nim\n"

In this code we have created a function called HelloFromNim() that does not take any parameters and returns "Hello, World From Nim" and the type of this word is cstring

This is what we talked about earlier.

cstring is the type for text strings outside the Nim code, and we used code {.exportc.} For the compiler to configure this variable (Refer from function) for use outside the Nim code in order not to cause any problems or the program stops while running. and \n to go to new line. Now we will compile lib.nim code as a header file to use it in C code. We will compile it using

nim c --noMain --noLinking --header:lib.h lib.nim

The first command nim runs the Nim compiler with three special options --noMain avoid generating a main() function in the generated files--no Linking avoid linking the object files into a final binary, and --header explicitly generate a header file for C integration. All the generated files are placed into the ~/.cache/nim directory.

Now we have finished the Nim section.

Open main.c with yout favorite code editor

code main.c

And write this code inside

#include <stdio.h>
#include "lib.h"

int main(){
    NimMain();
    printf(HelloFromNim());
}

In this code we have added the library of input and output in C to use the printf function, and then we add the header file that the Nim compiler make it Then we create the main() function known in C language and write it inside NimMain() This function is to prepare the Nim code for C compiler to understand it and the compiler can interact with and compile with the rest of the code. printf(HelloFromNim()); In this line we will see the result from the Nim code, not from the c code, and here we used to print the variable returned from the Nim code function directly. Because the printf() function takes a string variable, and the function in Nim returns a string variable, and when adding the Nim function to the printf function, it prints the return from the Nim code.

Now we will compile the C code to see the results

gcc -w  -I~/.choosenim/toolchains/nim-1.0.4/lib -I~/.cache/nim/lib_d/ ~/.cache/nim/lib_d/*.c main.c -o main

Explanation of building commands as follows

  • gcc(the compiler)
  • -w(hide all warnings) (Because there are a lot of warnings for Nim functions when converting them to C, they do not return values)
  • -I(to nim lib path) (To include nim header files that we will need it in our code )
  • -I(to cache files path) (To include lib.h file )
  • to cache files path (*.c Get all files ending with .c) (These files were created by Nim compiler and they are required for the program to work properly and we are building it with main.c file and get the final executable using them all).
  • main.c (The file we created earlier)
  • -o main (Executable file name is main)

Finally.

we run the final executable file.

./main

Then we will get in the terminal :

Hello, World From Nim

TODO:

  • I will add some examples from time to time and every example has a new idea.

  • I will add C++ Codes

If you have any questions, unclear things, or any extras

Do not hesitate to communicate with me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment