[m-users.] FFI C Include files not appearing

Julien Fischer jfischer at opturion.com
Tue Jun 25 20:34:38 AEST 2019

Hi Sean,

On Tue, 25 Jun 2019, emacstheviking wrote:

> Hi,I have these two lines of code...
> :- pragma foreign_decl("C", "#include <SDL/SDL.h>").
> :- pragma foreign_decl("C", "#include <SDL/SDL_version.h>").
> at the top of my module in the implementation section and this code too:
> :- func sdl_up = int.
> :- pragma foreign_proc("C",
>                        sdl_up = (Result::out),
>                        [ will_not_call_mercury
>                        , not_thread_safe
>                        , promise_pure
>                        ],
>                        "Result = SDL_Init(SDL_INIT_EVERYTHING);").
> :- func sdl_revision = string.
> :- pragma foreign_proc("C",
>                        sdl_revision = (Version::out),
>                        [ will_not_call_mercury
>                        , not_thread_safe
>                        , promise_pure
>                        ],
>                        "Version = (MR_String*)SDL_GetRevision();").
> The call to SDL_Init() seems to work but the compiler produces an error message saying that it cannot finf SDL_GetRevision()
> ffi.m: In function ‘ffi__sdl_revision_0_f_0’:
> ffi.m:37:23: warning: implicit declaration of function ‘SDL_GetRevision’; did you mean ‘SDL_GetCursor’?
> [-Wimplicit-function-declaration]
>                         "Version = (MR_String*)SDL_GetRevision();").
>                        ^~~~~~~~~~~~~~~
>                        SDL_GetCursor

The error is coming from the C compiler when it attempts to compile the
generated C code.  (The Mercury compiler emits #file and #line directives
so that C compiler errors are mapped backed to their location in the
original Mercury source file.)

> When I look at the Mercury/cs/ffi.c file i can not see any of my
> includes and so I wonder how it worked at alL! the command line I used
> was:

Declarations from foreign_decl pragmas and alike are placed in a Mercury
header file (.mh) and #included in generated .c files.  Look for the file
ffi.mh.  The contents of the foreign_decl pragmas should be in that.

>      mmc -v -L /usr/lib/x86_64-linux-gnu/libSDL2.so -lSDL2 --make ffi

It looks as though the C compiler can't see the SDL header files.
You can use the Mercury compiler's --c-include-directory option to pass
their location through to the C compiler.


More information about the users mailing list