How to reference an external function call

Hello Everyone,

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.

Any help is greatly appreciated.

Thank you,

Dave

Looks like a linker error message, not compiler. Perhaps you should show us the code.

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

... and an associated .c / .cpp file where it would be defined?

well I assume that it exists indeed somewhere and that the linker will figure it out :slight_smile:

"Assuming" anything is a dangerous thing in these parts.

fair :slight_smile:

Yes. I have included that directly.

but where does the code of that function live? is it your code or a library?

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.

Thank you everyone for the help.

Dave

answer the question in #9

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.

Okay. Thank you. I agree, I can show snippets but that doesn't really tell anything.

I will post it later today.

Again, thank you for the help.

Dave

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

chbsp_board_init(grp_ptr);

is found in the chirp_bsp.h/.c.

void chbsp_board_init(ch_group_t *grp_ptr);

Thank you for the help.

Are you compiling with arduino’s IDE?

Where are chirp_bsp.h/.c located?

Can you rename the .c into .cpp just as a try?

But you didn't post those files. So, how are we to test it?

You also didn't tell us where they are located in the Arduino installation directory structure.

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.

They are in the root of the project folder.

I can zip up the files and add them. But there are a ton of them not all used.

Where'd you get them? What hardware do they support? Post a GitHub link.

Here is the chip: CH101 | TDK

What I am actually using with the Quiic board: Qwiic Chirp 101 Ultrasonic Rangefinder - SPX-17046 - SparkFun Electronics

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.