I am writing a sketch that I have to use external code. I am able to get it to be recognized and in either the Arduino IDE or VSCode the headers/code are recognized. In VSCode, the intellisense actually finds the function and has tool tips associated with it. But if I try to call the function it fails with an error that the compiler cannot find the reference. I am very close to making the code and board working but I need to be able to call this function from the external code.
Test.ino:99: undefined reference to `board_init(group_t*)'
The parameter and type are correct but it just cannot find it when I try to call it.
is there a .h where board_init() would be declared ? if so, include this.
if all is resolved at link time, you could try to add at the start of your file extern void board_init(group_t*); // assuming it returns nothing otherwise adjust void for the right type
This actually got a new error, it appears to be a linker error.
C:\Users\UserName\AppData\Local\Temp\ccBDp0qO.ltrans0.ltrans.o: In function `setup':
e:\Workspace\test/Test.ino:102: undefined reference to `board_init(group_t*)'
collect2.exe: error: ld returned 1 exit status
exit status 1
I declared the extern before setup(). I am rusty at C and I have limited C++ experience. So I am guessing it is syntax or definition that is incorrect.
extern will solve for the file compilation, it tells the compiler not to worry about the code for this function and that will be resolved later... But if the linker can't find the code then it won't work.
Exactly like I told you in #2,
So, if you'd like help resolving it, I'd (again) suggest that you post a complete code that demonstrates the problem. Include all files and the directory structure that contains them.
Note, that we don't want to see a bunch of messy, cluttered, unrelated code. So, if that describes your project, please post an MRE instead.
Here is the code, the minimal amount I believe. If something is missing and it is critical I can add.
#include "soniclib.h"
#include "chirp_bsp.h"
#include "chirp_board_config.h"
struct ch_group_t {
uint8_t num_ports; /*!< Number of ports (max possible sensor connections) */
uint8_t num_i2c_buses; /*!< Number of I2C buses on this board */
uint8_t sensor_count; /*!< Number of sensors detected */
uint16_t i2c_drv_flags; /*!< Flags for special I2C handling by Chirp driver, from
\a chbsp_get_i2c_info()*/
uint16_t rtc_cal_pulse_ms; /*!< Real-time clock calibration pulse length (in ms) */
uint16_t pretrig_delay_us; /*!< Pre-trigger delay for rx-only sensors (in us) */
chdrv_discovery_hook_t disco_hook; /*!< Addr of hook routine to call when device found on bus */
ch_io_int_callback_t io_int_callback; /*!< Addr of routine to call when sensor interrupts */
ch_io_complete_callback_t io_complete_callback; /*!< Addr of routine to call when non-blocking I/O
completes */
ch_dev_t *device[CHIRP_MAX_NUM_SENSORS]; /*!< Array of pointers to ch_dev_t structures for
individual sensors */
uint8_t num_connected[CHIRP_NUM_I2C_BUSES]; /*!< Array of counters for connected sensors per bus */
chdrv_i2c_queue_t i2c_queue[CHIRP_NUM_I2C_BUSES]; /*!< Array of I2C non-blocking transaction
queues (one per bus) */
};
/* Configuration structure for group of sensors */
ch_group_t chirp_group;
ch_group_t *grp_ptr = &chirp_group;
uint8_t chirp_error = 0;
uint8_t num_ports;
uint8_t dev_num;
extern void chbsp_board_init(ch_group_t*); /* added */
void setup() {
Serial.begin(115200);
Serial.println("Init");
chbsp_board_init(grp_ptr); /* intellisense finds it, appears linker does not */
}
void loop() {
Serial.println("Start");
delay(1000);
}
I have not changed any of the code in this example. The call
I have tried it in both the Arduino's IDE and now using VSCode, hoping maybe the extensions available might help. Right now, I have everything (And I mean the kitchen sink) in the root of the project directory. Both headers and code. I have tried the cpp but then things got weird in the arduino IDE and I think it was wanted a constructor and public/private methods. Which there are none. But I haven't gone this far into the C++ rabbit hole.
It seems close to getting it to initialize, I can communicate with the i2c bus but the chip itself requires some initialization from talking with the manufacturer.