Next: , Previous: Using pragma foreign_export for C, Up: Interfacing with C   [Contents]


14.10.1.6 Using pragma foreign_decl for C

Any macros, function prototypes, or other C declarations that are used in ‘foreign_code’, ‘foreign_type’ or ‘foreign_proc’ pragmas must be included using a ‘foreign_decl’ declaration of the form

:- pragma foreign_decl("C", HeaderCode).

HeaderCode can be a C ‘#include’ line, for example

:- pragma foreign_decl("C", "#include <math.h>")

or

:- pragma foreign_decl("C", "#include ""tcl.h""").

or it may contain any C declarations, for example

:- pragma foreign_decl("C", "
        extern int errno;
        #define SIZE 200
        struct Employee {
                char name[SIZE];
        };
        extern int bar;
        extern void foo(void);
").

Mercury automatically includes certain headers such as <stdlib.h>, but you should not rely on this, as the set of headers which Mercury automatically includes is subject to change.

If a Mercury predicate or function exported using a ‘pragma foreign_export’ declaration is to be used within a ‘:- pragma foreign_code’ or ‘:- pragma foreign_proc’ declaration the header file for the module containing the ‘pragma foreign_export’ declaration should be included using a ‘pragma foreign_import_module’ declaration, for example

:- pragma foreign_import_module("C", exporting_module).