Go Down

Topic: Separate code into .h/.c files (Read 415 times) previous topic - next topic

maskrtnik01

Hello,

I would like to separate parts of my code to .h/.c files. I have followed a standard pattern used in C language. My code is compiled correctly, but does not link. Looks like Arduino compiler does not actually compile .c file.
I have also tried to package these two files into a library with library.properties file, this did not solve the issue.
I have checked signatures of my functions many times, everything is okay.
I am not willing to share my code, but here is error message:
Code: [Select]
/var/folders/pq/83xxgp8977l2pwns__t7ynrm0000gn/T//ccfvIWY0.ltrans0.ltrans.o: In function `setup':
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:8: undefined reference to `PWM(unsigned char, unsigned char, unsigned char)'
/var/folders/pq/83xxgp8977l2pwns__t7ynrm0000gn/T//ccfvIWY0.ltrans0.ltrans.o: In function `loop':
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:13: undefined reference to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)'
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:14: undefined reference to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)'
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:15: undefined reference to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)'
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:16: undefined reference to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)'
/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:17: undefined reference to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)'
/var/folders/pq/83xxgp8977l2pwns__t7ynrm0000gn/T//ccfvIWY0.ltrans0.ltrans.o:/Users/stanislavhusar/Library/Mobile Documents/com~apple~CloudDocs/ArduinoHeart/Program/Program.ino:18: more undefined references to `StmievacVsetky(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char)' follow
collect2: error: ld returned 1 exit status
exit status 1


Any ideas how should I correctly separate my code?


Thanks in advance,
Stanislav

pert

Arduino sketches are actually c++. The easiest solution would be to change the extension of your .c file to .cpp. The other option would be to wrap the function declarations in extern "C" {}

maskrtnik01

Oh, thank you. I was actually wondering, why it does not work, when I have already done it before.

Okay, compiled. I will burn it to my MCU tomorrow, and I will let you now if I run to any additional problems.

CrossRoads

#3
Dec 23, 2017, 08:55 pm Last Edit: Dec 23, 2017, 08:58 pm by CrossRoads
Why not try the Tabs feature of the IDE? It's the down arrow at the upper right of the IDE window.
That'll create new .ino files when you save and will put them in the same folder as your sketch.
Heres an example with a large sketch I have running on a '1284P
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


Go Up