[Solved] Code modularization and includes

Hi!
I have my main code file "include_test.ino"

#include "core.c"

void setup() {
  dht.setup(12, DHTesp::DHT22);
}

void loop() {
  // put your main code here, to run repeatedly:

}

And my "core.c" file

#include "DHTesp.h"

DHTesp dht;

void temperature() {
  TempAndHumidity dht22 = dht.getTempAndHumidity();
}

Code compilation errors on

AArduino: 1.8.9 (Mac OS X), Board: "FireBeetle-ESP8266, 80 MHz, 921600, 4M (3M SPIFFS)"

WARNING: library DHT_sensor_library_for_ESPx claims to run on (esp8266, esp32, arduino-esp32) architecture(s) and may be incompatible with your current board which runs on (firebeetle8266) architecture(s).
In file included from sketch/core.c:1:0:
/Users/nico/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:94:3: error: expected specifier-qualifier-list before 'inline'
inline bool isTooHot(float temp, float humidity) {return (temp > (humidity * m_tooHot_m + m_tooHot_b));}
^
/Users/nico/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:105:1: error: unknown type name 'class'
class DHTesp
^
/Users/nico/Documents/Arduino/libraries/DHT_sensor_library_for_ESPx/DHTesp.h:106:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '{' token
{
^
core.c:3:1: error: unknown type name 'DHTesp'
DHTesp dht;
^
sketch/core.c: In function 'temperature':
core.c:6:3: error: unknown type name 'TempAndHumidity'
TempAndHumidity dht22 = dht.getTempAndHumidity();
^
core.c:6:30: error: request for member 'getTempAndHumidity' in something not a structure or union
TempAndHumidity dht22 = dht.getTempAndHumidity();
^
exit status 1
unknown type name 'DHTesp'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

But compilation works OK if I just set all the code at "include_test.ino"

#include "DHTesp.h"

DHTesp dht;

void temperature() {
  TempAndHumidity dht22 = dht.getTempAndHumidity();
}

void setup() {
  dht.setup(12, DHTesp::DHT22);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Is there any way to modularize my code to extract the temperature() function to another file?

Thanks.

Since you named it "core.c" it is going to be compiled both inline, where you included it, AND separately as a C source. You can't use C++ features like the DHTesp object in a C source. I think that is why you are getting compile errors.

Change the name to "core.ino" and it will be merged with your sketch before compiling without needing to be "included".

OR, change the name to core.h and include it. The .h (header) extension lets the IDE know that you don't want it compiled on its own. Since you would be including it in a .ino file it will be compiled as C++ and the DHTesp object will compile.

You can make your other file a .uno file in the same directory and the IDE will combine them for you. They will be on separate tabs when you open the file in the IDE.

If it was named core.cpp?

I've had some problems before with included files not behaving as I expected from years of writing Borland C++ 3.1. That was >5 years ago (maybe I was still using 0023, I did until 1.05 or 1.06 came out), some things didn't link properly and the explanation then had to do with Java in the compiler. So what the last 2 posts seem to tell me is good news.

Thank you people! I just renamed core.c to core.ino.

If you're really interested in code modularity, then I don't recommend the mulit-.ino technique as it will not achieve that goal. Proper methods for doing this with .c, .cpp, and .h files have been established over decades of serious C / C++ development. I see no reason do deviate from these when coding for the hobbyist Arduino ecosystem. See Reply #3 Here.