i thought i figured the technique of writing and calling C library. But apparently i was too quick.
What am i trying to achieve
i'm trying to write a C library and call it's functions from the .ino code.
What is my problem?
I can NOT understand how to write a C library for Arduino. And i do not understand why other standard C libraries ( files with .c extension) work and mine is not.
What is the code?
//test.ino
#ifdef __cplusplus
extern "C"{
#endif
#include <aaa.h>
#ifdef __cplusplus
} ;
#endif
int led = 13;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
// led = ardprintf('Q');
for(int i=0; i< 10; i++){
Serial.print(aaa(10));
delay(200);
}
}
aaa.h
int aaa(int a);
aaa.c
#include <Arduino.h>
#include <HardwareSerial.h>
#include "aaa.h"
int aaa(int a){
Serial.print("Im here");
return 0;
}
What is the error?
[Stino - Start building "test"...]
[ 3%] Creating /tmp/Stino_build/test/test.ino.cpp.o...
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=165 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-I/home/az/work/Arduino/Sketches/Examples/test" "-I/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino" "-I/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/variants/standard" "-I/home/az/work/Arduino/Sketches/Examples/libraries/aaa" "/tmp/Stino_build/test/test.ino.cpp" -o "/tmp/Stino_build/test/test.ino.cpp.o"
[ 6%] Creating /tmp/Stino_build/test/lib_aaa/aaa.c.o...
"avr-gcc" -c -g -Os -w -ffunction-sections -fdata-sections -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=165 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-I/home/az/work/Arduino/Sketches/Examples/test" "-I/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino" "-I/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/variants/standard" "-I/home/az/work/Arduino/Sketches/Examples/libraries/aaa" "/home/az/work/Arduino/Sketches/Examples/libraries/aaa/aaa.c" -o "/tmp/Stino_build/test/lib_aaa/aaa.c.o"
In file included from /home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Print.h:27:0,
from /home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Stream.h:26,
from /home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/HardwareSerial.h:29,
from /home/az/work/Arduino/Sketches/Examples/libraries/aaa/aaa.c:2:
/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Printable.h:25:1: error: unknown type name 'class'
class Print;
^
/home/az/bin/arduino-1.6.5-r5/hardware/arduino/avr/cores/arduino/Printable.h:33:1: error: unknown type name 'class'
class Printable
<skip>
/home/az/work/Arduino/Sketches/Examples/libraries/aaa/aaa.c: In function 'aaa':
/home/az/work/Arduino/Sketches/Examples/libraries/aaa/aaa.c:7:8: error: request for member 'print' in something not a structure or union
Serial.print('Im here');
What works?
If i do NOT use Serial ( in other words, if i don't use <HardwareSerial.h> ), and just have some simple function like:
int aaa(int a){
return a*a ;
}
code and library compiles and all works fine.
What else works?
if i rename the aaa.c to aaa.cpp and remove the "external C" from the test.ino - everything works great.