Including 'header' type files

edited June 2014 in Programming Questions

I would like to add a file into a Processing sketch that only has a list of 'static final int'.

In 'C' I would do this by:

'#include ".h".

Is there a way of doing this so that the file can be in the same folder as the pde sketch?

Thanks

Tagged:

Answers

  • Technically, no. #include actually adds the content of the file to the current file, like a copy / paste operation. Java cannot do that.

    What is traditionally done is putting the definitions in an interface, in a .java file of same name. Eg.

    interface Constant // In Constant.java
    {
      String PATH = "C:/Temp";
      int THE_ANSWER = 42;
    }
    

    If you put the file in a sketch folder, it will load as a tab, and you can access the constants like:

    int val = Constant.THE_ANSWER;
    

    and so on.

    Note that variables in an interface are implicitly public static final.

  • edited June 2014

    Processing got a pre-processor which converts Processing's Java Mode into a valid Java program in order to be compiled.

    IMO, it'd be trivial to add some basic parser directives like #define, #ifdef, #ifndef to Processing's pre-processor.
    But it seems most of Processing devs consider such as Java heresy, even though Processing isn't bound to Java only!

    Actually, Processing's pre-processor needs an urgent revamp in order to make it compatible w/ Java 8 and fix very old outstanding bugs!
    Even today, pre-processor isn't compatible w/ Java 5's enum! Neither accept Unicode names for variables, classes & methods!

  • "most of Processing devs consider such as Java heresy"
    Really? You made a proposal which have been rejected because it isn't a Javaism?

  • I've never formally made the proposal myself. But others have already tried to ask for directive inclusions in order to make a Processing code flexible enough to be "compiled" for other modes beyond "Java Mode"!
    But the refusal is in the lines that "I don't feel that doing so is Java style". [-(

Sign In or Register to comment.