Next: Adding foreign definitions, Previous: Adding foreign declarations, Up: Foreign language interface [Contents]
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 submodules. By default, they are not visible to the foreign code in ‘pragma foreign_code’ or ‘pragma foreign_proc’ declarations in any other module, but this default can be overridden (giving access to all other modules) 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.
Note that the Melbourne Mercury implementation often behaves as if ‘pragma foreign_import_module’ declarations were implicitly added to modules. However, programmers should not should not depend on this behaviour; they should always write explicit ‘pragma foreign_import_module’ declarations wherever they are needed.