Using Structs with QPC

The structs package is divided into two parts:

The former file is not (usually) needed while your application is running, while the latter part certainly is. By separating structs into two files, you may avoid including the structs declaration code in your application.

In order to declare a foreign type or use foreign types in a foreign function declaration, you must first load the file library(structs_decl). Ordinarily, you would probably do this by including an ensure_loaded/1 or use_module/[1,2,3] call in your file. Unfortunately, this will not allow qpc to compile your file. In order both to use your file in the development system, and to compile it with qpc, put the following line in your files that define foreign types or use foreign types in foreign function declarations:

         :- load_files(library(structs_decl),

The when(compile_time) tells qpc to load library(structs_decl) into qpc, and not to record a dependency on it. This means that library(structs_decl) will not be part of your statically linked application.

If you accidentally use

          :- ensure_loaded(library(structs_decl)).

it will compile in the development system, but when you qpc the file you will get a warning.

Files that just use structs are much simpler. Just add this to those files:

         :- ensure_loaded(library(structs)).

There is another important complication. If you have type declarations in one file (call it A) that use types declared in another file (B), you must declare (at least) a compile_time dependency. So in file A, you'd need to have the line:

         :- load_files('B', [when(compile_time),if(changed)]).

This does not allow predicates in A to call predicates in B. If you need this, too, you should instead include in file A the line:

         :- load_files('B', [when(both),if(changed)]).

You will also need to ensure that B is compiled to a QOF file before trying to qpc A. This requires that if A is a module-file, so must B be. If A is not a module-file, then B need not be a module-file (but it may be). If you use the make utility to maintain object files, you might then want to add the following line to your Makefile:

         A.qof:      B.qof