mardi 7 janvier 2020

Problems using `#define` inside of `#ifndef` with enum constant

I have an issue where moving to a different version of an API library has renamed one of the constants. In particular, version 5.04 uses GRADIENT_DESCENT_LEARNING and version 6.07 uses GRADIENT_ASCENT_LEARNING.

No, problem, I thought. I'll just use a #define to set the old constant to the new constant value, and then I'll compile against it. So, I put this at the top of the relevant C file:

#include Netica.h
#if defined(GRADIENT_DESCENT_LEARNING)
/* Version 5.04 uses descent*/
#else 
/* Fix for label changes from version 5.04 to 6.07 */  
#define GRADIENT_DESCENT_LEARNING  GRADIENT_ASCENT_LEARNING
#endif

This is working fine when I compile against the 6.07 code (using GRADIENT_ASCENT_LEARNING) but when I try to compile against the earlier 4.05 library (with GRADIENT_DESCENT_LEARNING) defined I get the following series of error messages:

Experience.c:98:19: error: use of undeclared identifier
      'GRADIENT_ASCENT_LEARNING'; did you mean 'GRADIENT_DESCENT_LEARNING'?
      algorithm = GRADIENT_DESCENT_LEARNING;
                  ^~~~~~~~~~~~~~~~~~~~~~~~~
                  GRADIENT_DESCENT_LEARNING
Experience.c:14:36: note: expanded from macro 'GRADIENT_DESCENT_LEARNING'
#define GRADIENT_DESCENT_LEARNING  GRADIENT_ASCENT_LEARNING
                                   ^
/Users/ralmond/software/Netica_API_504/src/Netica.h:149:51: note: 
      'GRADIENT_DESCENT_LEARNING' declared here
typedef enum {COUNTING_LEARNING=1, EM_LEARNING=3, GRADIENT_DESCENT_LEARNI...
                                                  ^

The middle line confuses me. It sounds like the redefinition is happening even through the GRADIENT_DESCENT_LEARNING should have been defined by the enum in Netica.h. (The 6.07 version has GRADIENT_ASCENT_LEARNING in place of GRADIENT_DESCENT_LEARNING).

How can I test if the enum constant is defined or not?

I'm having this problem using clang version 11.0.0 (Xcode 4.2.1) under MacOS and a very similar issue (with slight variation on the wording of the error message) using GCC 4.9.3 under Windows (MinGW-w64).

Aucun commentaire:

Enregistrer un commentaire