Next: , Previous: , Up: Foreign language interface   [Contents]


14.8 Declaring Mercury exports to other modules

The declarations for Mercury predicates or functions exported to a foreign language using a ‘pragma foreign_export’ declaration are visible to foreign code in a ‘pragma foreign_code’ or ‘pragma foreign_proc’ declaration of the same module and also in those of any sub-modules. They are not visible to the foreign code in ‘pragma foreign_code’ or ‘pragma foreign_proc’ declarations in any other module. They can be made visible using a declaration of the form:

:- pragma foreign_import_module("Lang", ImportedModule).

where ImportedModule is the name of the module containing the ‘pragma foreign_export’ declarations.

If Lang is "C" this is equivalent to

:- pragma foreign_decl("C", "#include ""ImportedModule.mh""").

where ImportedModule.mh is the automatically generated header file containing the C declarations for the predicates and functions exported to C.

pragma foreign_import_module’ should be used instead of the explicit #include because ‘pragma foreign_import_module’ tells the implementation that ImportedModule.mh must be built before the object file for the module containing the ‘pragma foreign_import_module’ declaration.

A cycle of ‘pragma foreign_import_module’, where the language is ‘"C#"’ or ‘"Java"’, is not permitted.

Note that the Melbourne Mercury implementation often implicitly inserts ‘pragma foreign_import_module’ declarations but programmers should not write code that depends upon this behaviour; ‘pragma foreign_import_module’ declarations should always be explicitly included if needed.