Skip to main content
ESpra.1
Senior
August 29, 2022
Question

"undefined reference to custom function"

  • August 29, 2022
  • 2 replies
  • 2371 views

I am unsure of why this function keeps triggering the "undefined reference to function" error, especially since it seems to be able to find it perfectly well when I deliberately leave out arguments.

The function in question is shown below:

dc_init(DC1, DC_IN_1_Pin, DC_IN_2_Pin, GPIOA, GPIOC);

Additionally, here are the relevant pieces from the .h and .cpp files

".h"

struct DC_Driver
{
	uint16_t pos_pin;
	uint16_t neg_pin;
	GPIO_TypeDef *pos_port;
	GPIO_TypeDef *neg_port;
}dc_driver;
 
void dc_init(struct DC_Driver driver, uint16_t Pos_Pin, uint16_t Neg_Pin, GPIO_TypeDef *Pos_Port, GPIO_TypeDef *Neg_Port);

".cpp"

void dc_init(struct DC_Driver driver, uint16_t Pos_Pin, uint16_t Neg_Pin, GPIO_TypeDef *Pos_Port, GPIO_TypeDef *Neg_Port) {
	driver.pos_pin = Pos_Pin;
	driver.neg_pin = Neg_Pin;
	driver.pos_port = Pos_Port;
	driver.neg_port = Neg_Port;
}

The dc_init function is meant to save the GPIO information for a motor driver, with a similar function stepper motors. The device is meant to control one DC motor and two stepper motors.

I'm sure it's probably something simple I'm missing, but I can't find it. I'll try to provide more information if needed.

This topic has been closed for replies.

2 replies

gbm
Lead III
August 29, 2022

My guess: it's a C++ function and you are trying to call it from C module. Define the function in a .C file.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
ESpra.1
ESpra.1Author
Senior
August 29, 2022

I tried that. Everything broke rather spectacularly, and I tried again after I found a possible error. The end result is practically everything in the HAL_Driver folder hitting me with a big red X. Also, all types (uint8_t, uint16_t, unsigned long, etc) are now considered undefined.

I'll keep experimenting with getting the code into a normal .c file and see what happens

Piranha
Principal III
August 29, 2022

You are passing a structure in a function parameter, which is a bad practice. The call makes a local copy of the structure and the function modifies the local copy. The the original structure will be left intact. To do what is typically necessary and recommended, look there:

https://stackoverflow.com/questions/10370047/passing-struct-to-function

ESpra.1
ESpra.1Author
Senior
August 31, 2022

Would that apply to all the functions, or just the initializers? Most of the functions just read the contents of the structs to know what GPIO ports and pins to write to

Piranha
Principal III
September 4, 2022

Passing just a pointer to the structure is more efficient than making a copy of the whole structure. Therefore this principle applies to 99,9% of the cases.