Hi, I am quite new to PlatformIO and migrating from the ArduinoIDE, so please bear with me.
I am moving over a project I did in the ArduinoIDE to PlatformIO, but have an error that I cannot get fixed. I made a minimal reproducible example shown in the code below. I am trying to distribute the functions I am using in different files, and that’s probably where the error is in. The error states:
< artificial>(.text.startup+0xc0): undefined reference to `readByte(unsigned char, unsigned char)'
Of course I want the error fixed, but I also want to learn how to have the program in multiple files in a ‘proper’ way. From the numerous guides on the internet I found and studied, this is what I came up with, but I suspect that there is tonnes of things to improve/change, so please do not hesitate to point me to anything that can be improved.
The files:
main.cpp:
#include "main_head.h"
void setup() {
// put your setup code here, to run once:
}
void loop() {
bool succes = readAccel(&xMeas,&yMeas,&zMeas);
}
main_head.h:
#ifndef MAIN_HEAD_H
#define MAIN_HEAD_H
#include <Arduino.h>
#include <Wire.h>
//global var:
float xAccel, yAccel, zAccel;
float xMeas, yMeas, zMeas;
int KXTJ3 = 0x0E;
//other functions:
extern bool readAccel(float *,float *,float *);
#endif
accel_readout.cpp:
#include "accel_read_head.h"
bool readAccel(float *xAccel,float *yAccel,float *zAccel){
bool succes = false;
byte x0Data, x1Data, y0Data, y1Data, z0Data, z1Data;
if(
readByte(byte(0x06), x0Data)&&
readByte(byte(0x07), x1Data)&&
readByte(byte(0x08), y0Data)&&
readByte(byte(0x09), y1Data)&&
readByte(byte(0x0A), z0Data)&&
readByte(byte(0x0B), z1Data)
){
succes = true;
}
*xAccel = x0Data+x1Data;
*yAccel = x0Data+x1Data;
*zAccel = x0Data+x1Data;
return succes;
}
accel_read_head.h:
#ifndef ACCEL_READOUT_H
#define ACCEL_READOUT_H
#include "main_head.h"
extern bool readByte(byte, byte);
#endif
read.cpp:
#include "read_head.h"
bool readByte(byte register_address, byte &data){
bool success = false; // set default to no success
Wire.beginTransmission(KXTJ3);
Wire.write( register_address); // select register
int error = Wire.endTransmission( false); // false for repeated start
if( error == 0){ // no error ? then data can be requested
int n = Wire.requestFrom(KXTJ3, 1);
if( n == 1){ // received the bytes that were requested ?
data = (byte) Wire.read();
success = true; // only now success is true
}
}
return(success);
}
read_head.h:
#ifndef READ_H
#define READ_H
#include "main_head.h"
#endif
Any help is greatly appreciated.