@@ -47,7 +47,7 @@ The commands to compile to either C, C++ or Objective-C are:
4747The most significant difference between these commands is that if you look
4848into the ``nimcache `` directory you will find ``.c ``, ``.cpp `` or ``.m ``
4949files, other than that all of them will produce a native binary for your
50- project. This allows you to take the generated code and place it directly
50+ project. This allows you to take the generated code and place it directly
5151into a project using any of these languages. Here are some typical command-
5252line invocations::
5353
@@ -110,8 +110,8 @@ Nim code calling the backend
110110Nim code can interface with the backend through the `Foreign function
111111interface <manual.html#foreign-function-interface> `_ mainly through the
112112`importc pragma <manual.html#foreign-function-interface-importc-pragma >`_.
113- The `` importc ` ` pragma is the *generic * way of making backend symbols available
114- in Nim and is available in all the target backends (JavaScript too). The C++
113+ The `importc ` pragma is the *generic * way of making backend symbols available
114+ in Nim and is available in all the target backends (JavaScript too). The C++
115115or Objective-C backends have their respective `ImportCpp
116116<manual.html#implementation-specific-pragmas-importcpp-pragma> `_ and
117117`ImportObjC <manual.html#implementation-specific-pragmas-importobjc-pragma >`_
@@ -232,11 +232,6 @@ Also, C code requires you to specify a forward declaration for functions or
232232the compiler will assume certain types for the return value and parameters
233233which will likely make your program crash at runtime.
234234
235- The Nim compiler can generate a C interface header through the ``--header ``
236- command-line switch. The generated header will contain all the exported
237- symbols and the ``NimMain `` proc which you need to call before any other
238- Nim code.
239-
240235
241236Nim invocation example from C
242237~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -255,9 +250,10 @@ Create a ``maths.c`` file with the following content:
255250
256251.. code-block :: c
257252
258- #include "fib.h"
259253 #include <stdio.h>
260254
255+ extern int fib(int a);
256+
261257 int main(void)
262258 {
263259 NimMain();
@@ -270,26 +266,27 @@ Now you can run the following Unix like commands to first generate C sources
270266from the Nim code, then link them into a static binary along your main C
271267program::
272268
273- $ nim c --noMain --noLinking --header:fib.h fib.nim
274- $ gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
269+ .. code :: cmd
270+
271+ nim c --noMain --noLinking fib.nim
272+ gcc -o m -I$HOME/.cache/nim/fib_d -Ipath/to/nim/lib $HOME/.cache/nim/fib_d/*.c maths.c
275273
276274 The first command runs the Nim compiler with three special options to avoid
277- generating a ``main() `` function in the generated files, avoid linking the
278- object files into a final binary, and explicitly generate a header file for C
279- integration. All the generated files are placed into the ``nimcache ``
275+ generating a `main() `:c: function in the generated files and to avoid linking the
276+ object files into a final binary. All the generated files are placed into the ``nimcache ``
280277directory. That's why the next command compiles the ``maths.c `` source plus
281278all the ``.c `` files from ``nimcache ``. In addition to this path, you also
282279have to tell the C compiler where to find Nim's ``nimbase.h `` header file.
283280
284281Instead of depending on the generation of the individual ``.c `` files you can
285282also ask the Nim compiler to generate a statically linked library::
286283
287- $ nim c --app:staticLib --noMain --header fib.nim
288- $ gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
284+ nim c --app:staticLib --noMain fib.nim
285+ gcc -o m -Inimcache -Ipath/to/nim/lib libfib.nim.a maths.c
289286
290287The Nim compiler will handle linking the source files generated in the
291288``nimcache `` directory into the ``libfib.nim.a `` static library, which you can
292- then link into your C program. Note that these commands are generic and will
289+ then link into your C program. Note that these commands are generic and will
293290vary for each system. For instance, on Linux systems you will likely need to
294291use ``-ldl `` too to link in required dlopen functionality.
295292
0 commit comments