Different compile behavior when using .cpp files in IDE

This test shows a problem I encountered with the IDE 1.8.19 trying to use a .cpp file and a header file:
First attempt:
I place a header file test2.h in the working directory
I copy the contents of my code file, test2.cpp, and paste it in the skecth as per exhibit 1.
Hit the Verify icon and the compile works.
Next I want to test using a file instead of the copy and paste process:
I place the file named test2.cpp in the working directory.
I modify the sketch per exhibit 2
Compile fails. The compiler no longer recognize int8_t/int16_t as data types.
To get this process to work, I have to do 2 things, First I have to replace all int8_t and int16_t with unsigned char and int respectively. Replacing int8_t with byte was not accepted. Second, to get the line of code:
micro_snap = TCNT1;
to compile, I had to add to test2.cpp:
#include <arduino.h>
As an aside, I still have not figured out a way to incorporate the line
TCCR1A = 0;
in the test2.cpp file.
Questions: Why does the compiler no longer accept int8_t as data type, or byte for that matter.
Why the need to explicitly include the arduino library
How do I move TCCR1A=0 within one of the files.

//###################################################################
//Exhibit 1 Copy and paste code from test2.cpp in sketch. test2.h in directory

void setup() {
  TCCR1A = 0;
  Serial.begin(115200);
  event_check();
}

//Section below exclusive of void loop(){} is the content of test2.cpp

#include "test2.h"

  int8_t old_Btim = 0;
  int8_t sum_32 = 0;
  int8_t sum_2 = 0;


void event_check(){
  int8_t Btimer;
  int8_t delta;
  int16_t micro_snap;
  int8_t next = 0;
  
  micro_snap = TCNT1;
  Btimer = ((micro_snap>>8) & 0x7f);
  delta = Btimer - old_Btim;
  if (delta<0){delta += 128;}
  old_Btim = Btimer;
  sum_2 += delta;
  if (sum_2 > 1){
    sum_2 -= 2; //2.048ms threshold with 1.024ms resolution
    next = 1;
  }
  sum_32 += delta;
  if (sum_32 > 31){
    next = 2;   //32.768ms threshold with 1.024ms resolution
    sum_32 -= 32;
  }
  return next;
}

void loop() {
}

//#####################################
contents of test2.h:

	extern int8_t old_Btim;
	extern int8_t sum_32;
	extern int8_t sum_2;

Compilation result:
Sketch uses 1506 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 187 bytes (9%) of dynamic memory, leaving 1861 bytes for local variables. Maximum is 2048 bytes.

//###################################################################
//Exhibit 2. Both test2.cpp and test2.h are in directory

void event_check();
void setup() {
  TCCR1A = 0;
  Serial.begin(115200);
  event_check();
}


void loop() {
}

Sketch uses 1502 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 187 bytes (9%) of dynamic memory, leaving 1861 bytes for local variables. Maximum is 2048 bytes.

For a .ino file (a sketch) the ide inserts several standard #include statements, so that the new programmer doesn’t have to deal with such things. But for a .cpp file, the code is treated as an ordinary C++ source code file, and you have to do all of those includes yourself.

The uint8_t definitions would normally come from stdint.h
(It’s pretty normal to just add “#include <arduino.h>“, which will in turn pull in a lot of the standard-ish other .h files.)

Thanks for your quick reply.
I've searched for a tutorial with this level of detail to no avail. Any suggestion there?

Please, the file is named "Arduino.h". You might be able to get away with arduino.h when using a case insensitive file system like on Windows, but it won't be pleasant when you share your code with our Linux using friends.

The minimal preprocessing done to the .ino files of the sketch is documented here:

https://arduino.github.io/arduino-cli/latest/sketch-build-process/

The Build Process is described here: Sketch build process - Arduino CLI
The location of the various "standard" definitions is part of, well, "standard C", so it isn't documented separately by Arduino. The avr-libc is documented here: avr-libc: AVR Libc, but it's a little backward in that it tells you what things are in which include files, rather than which include files are needed for specific definitions. It can be easier to just search for the terms: for example using google to search for "uint8_t C" turns up Integers (The GNU C Library) as the first hit.

Thanks all. Very helpful. Arduino.h from now on :smile:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.