#ifdef in fluid

15 views
Skip to first unread message

supsm17

unread,
Apr 29, 2022, 12:01:14 AM4/29/22
to fltk.general
My program has two "modes" in which it can be compiled: one with a gui using fltk and one without a gui (no fltk). I would like my fluid generated files to contain an #ifdef at the very top (and #endif at the bottom) so that it only compiles when the preprocessor definition is present. I tried using the "Declaration Block" in fluid, but the #includes, extern declarations, and function callbacks are still outside the block. Is there an easy way to accomplish this task?

Matthias Melcher

unread,
Apr 29, 2022, 4:46:23 AM4/29/22
to fltk.general
Put a "Declaration" field at the top of your file, setting it to "#ifdef MY_MACRO" and "in source file only". Do the same with "#endif" at the bottom. This should be enough to no generate any code from the FL files. 

You can do the same again and set it to "in header file only" to also exclude whatever is in the header. Anything that is left outside (#include <FL/Fl.H>, maybe a few other lines) will not generate any actual code. 

If you want to exclude even that, check the first line in your generated file. It should say something like "#ifndef my_file_h". So if you define the macro "my_file_h", the header will no longer be loaded.

But the best solution is probably the modify your Makefile/CMake/whatever file to not generate and compile the Fluid files in the first place.

duncan

unread,
Apr 29, 2022, 1:15:13 PM4/29/22
to fltk.general
My program has two "modes" in which it can be compiled: one with a gui using fltk and one without a gui (no fltk). I would like my fluid generated files to contain an #ifdef at the very top (and #endif at the bottom) so that it only compiles when the preprocessor definition is present. I tried using the "Declaration Block" in fluid, but the #includes, extern declarations, and function callbacks are still outside the block. Is there an easy way to accomplish this task?

Why does this need to be added to fluid? Why not just have a C++ file that is just a wrapper around the fluid generated file:

app_wrapper.cxx:
#ifdef WITH_GUI
    #include app_fluid.cpp
#endif

This seems so obvious that I'm wondering whether I'm missing something?
E.g. does your IDE object to cpp files including other cpp files?
Or do you have problems with conflicting "main()" functions?

supsm17

unread,
May 2, 2022, 2:16:51 PM5/2/22
to fltk.general
@matthias I tried that as I said in the original post, but the extern declarations were outside of the #if blocks which resulted in linking issues

@duncan I hadn't considered that, but it does work and is relatively elegant. Thanks for the suggestion!

Reply all
Reply to author
Forward
0 new messages