Hi, AMoura.

I don't care about language popularity, university ranking and the like, 
specially the kind of popularity that one measures with tools, such as TIOBE 
index and Webometrics. Let me tell you why. Let us consider university ranking. 
There is a very low ranked university in Russia, which is Bauman State 
Technical University. In The Times Higher Education - World University Ranking, 
the Bauman University is bundled in the 801-1000 bracket, together with 
University of Lagos, São Paulo State University, Rochester Institute of 
Technology, Catholic University of Peru, and 200 other low impact institutions. 
You will probably conclude that alumni of such a low ranked school don't even 
know the periodic table... Well, they not only know the periodic table, but 
they invented it. The periodic table was discovered by Dmitri Mendeleev, who 
happened to be a professor at Bauman. You are receiving my answer thanks to the 
miracle of satellite-relayed long distance communications, which was invented 
by people from Bauman University. By the way, Sergei Korolev from Bauman 
created the first satellite, and the second, and the third probe to reach the 
moon, and the second, and the first probe to reach Venus, and the second, and 
all nine probes that reached Venus. The first man in orbit was another feat of 
Sergei Korolev, the father of astronautics. Sergei Korolev and Dmitri Mendeleev 
were not exceptions, sinc Bauman University has other bright alumni: Pavel 
Sukhoi (airplane designer), Andrey Tupolev (first supersonic commercial jet), 
Vladmir Chelomei (aerospace engineer), Nikolay Dollezhal (first nuclear reactor 
for producing electricity), Sergei Lebedev (created the first practical 
computer, which as used to launch Sputnik 1, the first artificial satellite), 
Alexander Kermudzhian (interplanetary rovers), Zou Jiahua (industrialization of 
China), etc. Anyway, we are here for discussing Nim. Then, let us discuss Nim. 
Here is how to call a C procedure from Nim:

1 -- Write the C procedure, and save it in, say, addints.c
    
    
    // File: addints.c
    int addTwoIntegers(int a, int b) {
      return a + b; }
    
    
    Run

2 -- Compile the C procedure into a library:
    
    
    › gcc -c addints.c
    › ls *.o
    addints.o
    › ar rvs addlib.a addints.o
    › ls *.a
    addlib.a
    
    
    Run

2 -- Write a Nim program:
    
    
    # File: addthem.nim
    import os, strutils
    {.compile: "addints.c".}
    proc addTwoIntegers(a, b: cint): cint {.importc.}
    
    when isMainModule:
      echo addTwoIntegers(paramStr(1).parseInt.cint, 7)
    
    
    Run

3 -- Compile it and run:
    
    
    › nim c --passL:addlib.a --nimcache:xx -d:release -o:sum.x addthem.nim
    Hint: used config file '/etc/nim/nim.cfg' [Conf]
    Hint: system [Processing]
    Hint: widestrs [Processing]
    Hint: io [Processing]
    Hint: addthem [Processing]
    CC: addints
    CC: stdlib_io.nim
    CC: stdlib_system.nim
    CC: addthem.nim
    Hint:  [Link]
    Hint: [SuccessX]
    › ./sum.x 35
    42
    
    
    Run

It seems that Nim poses no problem for linking an application to C. Let us see 
the other way around. Let us link Nim to Lisp, which happens to be my favorite 
Language. I am using a Macintosh, therefore, I will link Nim programs to a 
.dylib dynamic library. The Lisp I will choose is Chez Scheme, one of the 
nicest compilers in existence:

1 -- Let us start with a Makefile, since the command line is somewhat long:
    
    
    # Makefile
    all:
             nim c -d:danger --noMain --header\
                  --deadCodeElim:on --app:lib\
                  --opt:size --nimcache:./lixo\
                      -o:Fibonacci.dylib --gc:none fib.nim
    
    
    Run

2 -- Here is the nim program:
    
    
    # This module provides a simple "Fibonacci"
    # function to test Nim FFI.
    
    # An asterisk * exports an identifier and
    # makes it available for use by modules or
    # other languages. E.g. FibNum* makes
    # FibNum available with the "nfib" name.
    
    proc FibNum*(n: int): int {.exportc: "nfib"} =
      if n < 2: 1
      elif n < 3: n
      else:
        FibNum(n - 3) +
        FibNum(n - 2) +
        FibNum(n - 2)
    
    
    Run

3 -- Finally, let us run the Nim programa from another language:
    
    
    ~/nim/libs$ scheme
    Chez Scheme Version 9.5.3
    Copyright 1984-2019 Cisco Systems, Inc.
    
    > (load-shared-object "Fibonacci.dylib")
    > (define fb (foreign-procedure "nfib" (int) int))
    > (fb 5)
    8
    > (time (fb 42))
    (time (fb 42))
        no collections
        0.001091174s elapsed cpu time
        0.001445000s elapsed real time
        0 bytes allocated
    433494437
    
    
    Run

Reply via email to