Next: , Previous: Defining library interfaces, Up: C Interface


5.25.5 Declaring OS-level libraries

For calling some C functions, you need to link with a specific OS-level library that contains that function. E.g., the sin function requires linking a special library by using the command line switch -lm. In our C iterface you do the equivalent thing by calling add-lib as follows:

     clear-libs
     s" m" add-lib
     \c #include <math.h>
     c-function sin sin r -- r

First, you clear any libraries that may have been declared earlier (you don't need them for sin); then you add the m library (actually libm.so or somesuch) to the currently declared libraries; you can add as many as you need. Finally you declare the function as shown above. Typically you will use the same set of library declarations for many function declarations; you need to write only one set for that, right at the beginning.

Note that you must not call clear-libs inside c-library...end-c-library; however, c-library performs the function of clear-libs, so clear-libs is not necessary, and you usually want to put add-lib calls inside c-library...end-c-library.

clear-libs              gforth       “clear-libs”

Clear the list of libs

add-lib       c-addr u –         gforth       “add-lib”

Add library libstring to the list of libraries, where string is represented by c-addr u.