not understanding #ifdef, libraries and functions?

I found a library for the MCP6725 DAC which (I think) contains the functions I need. The library is at Arduino/libraries/MCP4725 at master · RobTillaart/Arduino · GitHub. There appears to be be no .zip file, so I copied the contents of the individual files (.H, .ccp, .json, .properties) to my PC and Zipped them, then added them as a library in the arduino editor. The library is called "MCP6725".

The example files compile OK, so I assume the library is good.

However there's something I don't understand. The example files contain statements such as the following:

#ifdef MCP4725_EXTENDED
.......
DAC.writeDAC(0, true);
.......
#endif

The DAC.writeDAC function is the one I need. If I remove the #ifdat and #endif statements, then there's a compilation error that tells me 'class MCP4725' has no member named 'writeDAC'. Presumably the program doesn't attempt to execute the function with #IFdef in place because it hasn't been defined and so there's no compiler error? Just a guess here.

Some functions are fine, such as "DAC.setValue(200)", so "setValue" is a member of the class and the library works fine for that.

It seems there are a set of functions which compile without issues, but there are some "extended" functions which don't and I can't figure out how to enable these extended functions.

Looking at the .cpp file, it too has lines like

#ifdef MCP4725_EXTENDED
...definitions...

It seems like there should be a switch to turn on these "_EXTENDED" functions but I can't figure out how to do that. There also seems to be a set of functions preceded by:

#ifdef MCP4725_POWERDOWNMODE

which define a further set of functions (if enabled??).

I hope this explanation of my problem is clear enough to understand what I'm asking about. Obviously there's something about libraries or functions or extended library functions I don't understand.

Any help or pointers would be greatly appreciated.

At this line...

...add this...

#define MCP4725_EXTENDED  1

... or uncomment line 48:

// conditional to minimize footprint.
//#define MCP4725_EXTENDED
//#define MCP4725_POWERDOWNMODE

The #ifdef preprocessor directive is normally used to toggle one or more lines of code into a program. Consider the following program:

#define DEBUG

void setup() {
  int i;
  int myArray[100];
  
  Serial.begin(9600);
  randomSeed(analogRead(0));  // Seed random number generator

  for (i = 0; i < 100; i++) {
    myArray[i] = random(0, 100);
#ifdef DEBUG
  if (i % 10 == 0) {
    Serial.println();
  }
  Serial.print(myArray[i]);
  Serial.print(" ");
#endif 
  }
}

void loop(){
}

Now comment out the #define:

//#define DEBUG

What happens? Because DEBUG is no longer defined in the program, the lines between #ifdef (i.e., if defined) and #endif (end of if defined statement block) are no longer compiled into the program. This is a quick and easy way to toggle debug code into and out of a program without having to retype it each time.

Thanks to all. I now understand better what’s going on and the use of the #ifdef preprocessor directive. I uncommented line 48 as suggested, but that seem to have lead to another problem :frowning:

I no longer get the class related error message, so it how seems to be happy with “DAC.writeDAC”. However when I run the sample program I get an error

C:\Users\Bob\Documents\Arduino\libraries\MCP4725a\MCP4725.cpp:195:9: error: assignment of read-only parameter ‘reg’
reg = reg | (_powerDownMode << 1);

This relates to this section of the .ccp file in the library

// PAGE 19 DATASHEET
// reg = MCP4725_DAC | MCP4725_EEPROM
int MCP4725::writeRegisterMode(const uint16_t value, const uint8_t reg)
{
    uint8_t h = (value / 16);
    uint8_t l = (value & 0x0F) << 4;
    Wire.beginTransmission(_deviceAddress);
reg = reg | (_powerDownMode << 1);
    WIRE_WRITE(reg);
    WIRE_WRITE(h);
    WIRE_WRITE(l);
    return Wire.endTransmission();
}

I understand the line

KA1GT:
C:\Users\Bob\Documents\Arduino\libraries\MCP4725a\MCP4725.cpp:195:9: error: assignment of read-only parameter 'reg'
[/quote]
It's because the parameter is const:
* *int writeRegisterMode(const uint16_t value, const uint8_t reg);* *
You need to remove the const keyword from reg in the function declaration and definition.
> KA1GT:
> This relates to this section of the .ccp file in the library
It's .cpp, not .ccp. .cpp is the extension for C++ source files (c plus plus).

I've submitted a pull request to fix the problem: