|
[Sponsors] |
Make parts of code version dependent (check and toggle with `#ifdef`s?) |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
July 30, 2014, 01:48 |
Make parts of code version dependent (check and toggle with `#ifdef`s?)
|
#1 |
Member
Christian Butcher
Join Date: Jul 2013
Location: Japan
Posts: 85
Rep Power: 13 |
EDIT: tl;dr: Can I access something like $WM_PROJECT_VERSION inside C++ code to be compiled with wmake (libso)?
I am (still, sort of, part time) working on a 2D amr library and everything is moving along (slowly). When someone downloaded the source files via github, they emailed me and told me it wouldn't compile. Because of the time difference between us, they had already found the problem when I read this, and sent a second email stating that my files compiled for OF-2.3.0 (which I updated to, and made some changes to support), but not for OF-2.2.1 (I initially started writing the library in OF-2.2.2). Now, obviously I can just write that the library will require OF-2.3.0, but since the changes are relatively small (referring to the function "topoChanging", whose implementation changed between 2.2.2 and 2.3.0), writing something along the lines of Code:
#ifdef V2.2.X_OF // lines of code with old function calls #endif #ifdef V2.3.X_OF+ // current, newer style lines of code #endif Is it possible to write code which can detect the OF version, and selectively choose a #define pre-processor command? The version is usually held in the headers of files, like the controlDict, but relying on this seems unhelpful (since it can easily be wrong, if the OF version is updated but the files are kept, or you can as far as I'm aware, just write whatever you want there...) and if I use Code:
if (someVar) { topoChanging(); } elseif (someOtherVar) { changingTopo(); } If I could define based on the existence of the function, I could possibly use Code:
#ifdef EARLIER_OF bool topoChanging(const bool) { return changing(bool); } bool topoChanging() { return changing(); } #endif Any help would be much appreciated. Last edited by chrisb2244; July 30, 2014 at 01:54. Reason: Found an environment variable that might be useful |
|
July 30, 2014, 01:58 |
|
#2 |
Member
Christian Butcher
Join Date: Jul 2013
Location: Japan
Posts: 85
Rep Power: 13 |
And with much ado about nothing - getenv() in <stdlib.h> is likely to be something I can use to get the value of $WM_PROJECT_VERSION, which solves much of the problem.
Now just to work out if that can get me a conditional #define or #include statement (since they should occur even if in an if() branch that isn't chosen, right? ) |
|
July 30, 2014, 02:55 |
|
#3 |
Member
Christian Butcher
Join Date: Jul 2013
Location: Japan
Posts: 85
Rep Power: 13 |
Solution:
In Make/options, before my Code:
EXE_INC = \ ... Code:
ifeq ($(WM_PROJECT_VERSION),2.3.0) MACRO_DEFINED=-DnameOfMyDefinedVar else MACRO_DEFINED= endif EXE_INC = \ $(MACRO_DEFINED) \ -I$(LIB_SRC)/..... ..... Code:
#ifdef nameOfMyDefinedVar Link to helpful page about conditional makefile variables: http://www.chemie.fu-berlin.de/chemn...ke/make_7.html (although once you know what you want to find, google is your friend) |
|
August 4, 2014, 12:40 |
|
#4 |
Retired Super Moderator
Bruno Santos
Join Date: Mar 2009
Location: Lisbon, Portugal
Posts: 10,981
Blog Entries: 45
Rep Power: 128 |
Greetings Christian,
There are two implementations I'm aware of and neither are present in OpenFOAM, or at least not yet.
Bruno
__________________
|
|
August 4, 2014, 21:44 |
|
#5 |
Member
Christian Butcher
Join Date: Jul 2013
Location: Japan
Posts: 85
Rep Power: 13 |
Dear Bruno,
Thank you for taking the time to read this and adding the links as reference. It seems like the mantis link contains a comment (from mwild) that is roughly the same as what I've done, although it also seems like the point of the 'bug' report/feature request is to avoid doing that - "#ifdef is BAAAAD". Certainly, it does make code very "Ugly". I confess that I'm at a loss as to how you would go about using the 'dog-tag' style code in 3rd party libraries/solvers/applications etc though. Doesn't generating a set of Code:
#define FOAM_VERSION 2 #define FOAM_VERSION_MiNOR 0 #define FOAM_VERSION_PATCHLEVEL 1 Is the suggestion that a version dependencies file of some kind, containing only #define commands, be included everywhere? For example, taking the mathmaticalConstants idea, something like Code:
// Version Defines // Get tagfile, which defines FOAM_VERSION style tags #if FOAM_VERSION==2 #define MATHCONSTS constants::mathematical #endif #if FOAM_VERSION==1 #define MATHCONSTS mathematicalConstants #endif Code:
switch (FOAM_VERSION) { case 2: #define MATHCONSTS constants::mathematical break; case 1: #define MATHCONSTS mathematicalConstants break; } |
|
August 5, 2014, 13:05 |
|
#6 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37 |
Hallo Christian,
I might add another source of inspiration, since waves2Foam also tries to be cross-version compatible. In waves2Foam I transform the version number to a 3 digit number and applies "==", "<" etc. booleans in the pre-processor statements. It is not too bad with the amount of #if statements, but specifically for PI, I use M_PI instead, because it is too common to bother about the change in syntax. In waves2Foam you will also find a whole class called crossVersionCompatibility or something along those lines, which is an attempt to gather all naming changes in one place. Good luck, Niels
__________________
Please note that I do not use the Friend-feature, so do not be offended, if I do not accept a request. |
|
August 5, 2014, 22:06 |
|
#7 | |
Member
Christian Butcher
Join Date: Jul 2013
Location: Japan
Posts: 85
Rep Power: 13 |
Quote:
Thank you for this suggestion - I took a look at the class you wrote and it indeed looks very useful. My current implementation is similar, but messy - implementing the changes as function calls returning 'word's is a much better plan! Best, Christian |
||
August 6, 2014, 05:14 |
|
#8 |
Senior Member
Niels Gjoel Jacobsen
Join Date: Mar 2009
Location: Copenhagen, Denmark
Posts: 1,903
Rep Power: 37 |
Good morning,
I might add that I simply pass the integer as Code:
-DOFVERSION Kind regards, Niels
__________________
Please note that I do not use the Friend-feature, so do not be offended, if I do not accept a request. |
|
Tags |
openfoam, version-specific code |
|
|