Undefined reference error in AVR programming for atmega328p

When compiling the example projects the majority of the functions return the compiling error “undefined reference …”. In this modified example of the motors1 example, the function set_motors returns the undefined reference error.

#include <pololu/orangutan.h>

#define F_CPU 20000000UL	// Baby Orangutan frequency (20MHz)

#include <util/delay.h>
/*
 * motors1: for for the Orangutan LV, SV, SVP, X2, Baby-O and 3pi robot.
 *
 * This example uses the OrangutanMotors functions to drive
 * motors in response to the position of user trimmer potentiometer
 * and blinks the red user LED at a rate determined by the trimmer
 * potentiometer position.  It uses the OrangutanAnalog library to measure
 * the trimpot position, and it uses the OrangutanLEDs library to provide
 * limited feedback with the red user LED.
 *
 * https://www.pololu.com/docs/0J20
 * https://www.pololu.com
 * https://forum.pololu.com
 */

unsigned long prevMillis = 0;

int main()
{
  while(1)
  {
    // note that the following line could also be accomplished with:
    // int pot = analogRead(7);

    int motorSpeed = 255;  // turn pot reading into number between -256 and 255

    set_motors(motorSpeed, motorSpeed);
  
    int ledDelay = motorSpeed;
	if(ledDelay < 0)
	  ledDelay = -ledDelay;  // make the delay a non-negative number
	ledDelay = 256-ledDelay; // the delay should be short when the speed is high

    red_led(1);       // turn red LED on
    _delay_ms(ledDelay);

    red_led(0);       // turn red LED off
	_delay_ms(ledDelay);
  }
}

thanks your help is much appreciated

Hello,

Could you try compiling an unmodified example and post the actual output, starting at the beginning? Also, please tell us what operating system you are using and how you installed the Pololu AVR Libraries.

-Paul

Hi,
Im using windows 7, and i installed the libraries using the install.bat as administrator and it said installation complete with no errors.
After using the motors1 example for the atmega 328p the output was:

------ Build started: Project: AVRGCC10, Configuration: Debug AVR ------
Build started.
Project "AVRGCC10.avrgccproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\AVR Studio 5.0\Vs\AvrGCC.targets" from project "c:\users\ian\documents\avrstudio\AVRGCC10\AVRGCC10\AVRGCC10.avrgccproj" (target "Build" depends on it):
	Using "RunAvrGCC" task from assembly "C:\Program Files\Atmel\AVR Studio 5.0\Vs\AvrGCCLib.dll".
	Task "RunAvrGCC"
		make all 
AVRGCC10.c
		Invoking: AVR/GNU C Compiler
		"C:/Program Files/Atmel/AVR Studio 5.0/extensions/Application/Avr ToolChain/bin/avr-gcc.exe" -funsigned-char -funsigned-bitfields -O0 -fpack-struct -fshort-enums -g2 -Wall -c  -mmcu=atmega328p   -MMD -MP -MF"AVRGCC10.d" -MT"AVRGCC10.d" -o"AVRGCC10.o" ".././AVRGCC10.c"
		Finished building: .././AVRGCC10.c
		Building target: AVRGCC10.elf
		Invoking: AVR/GNU C Linker
		"C:/Program Files/Atmel/AVR Studio 5.0/extensions/Application/Avr ToolChain/bin/avr-gcc.exe"  -mmcu=atmega328p   -o AVRGCC10.elf  AVRGCC10.o  
		AVRGCC10.o: In function `main':
c:\users\ian\documents\avrstudio\AVRGCC10\AVRGCC10\Debug/.././AVRGCC10.c(27,1): undefined reference to `set_motors'
		collect2: ld returned 1 exit status
		make: *** [AVRGCC10.elf] Error 1
	Done executing task "RunAvrGCC" -- FAILED.
Done building target "CoreBuild" in project "AVRGCC10.avrgccproj" -- FAILED.
Done building project "AVRGCC10.avrgccproj" -- FAILED.

Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

thanks for the fast reply

Hello,

It looks like you are using AVR Studio 5, which is a beta release that does not fully support our boards. Specifically, it does not yet work with our AVR programmers or with the Orangutan X2/SVP USB interfaces. What controller and programmer do you have?

Still, compiling should work. I have not used Studio 5, so I do not know exactly what you should do to make it work, but it looks like you are not using the compilation options specified in our project file. Did you open the project file in the motors1 folder or create a new project? If you must make a new project, see these instructions.

-Paul

By the way, I also noticed that in your modified program you were trying to use the avr-libc function _delay_ms(), which I believe has a maximum delay of 13 ms. You should probably just use our delay_ms() function described here.

-Paul

Thanks for letting me know about avr studio 5, i have now changed to avr studio 4 and reinstalled the libraries, but unfortunetly, I still get the same outcome. It still says:

Build started 9.5.2011 at 15:07:35
avr-gcc -mmcu=atmega328p -Wl,-Map=practice2.map practice2.o     -o practice2.elf
practice2.o: In function `main':
C:\Users\Ian\Documents\default/../practice2.c:27: undefined reference to `set_motors'
make: *** [practice2.elf] Error 1
Build failed with 1 errors and 0 warnings...

and this is from the code of the original motors1 example:

#include <pololu/orangutan.h>

/*
 * motors1: for for the Orangutan LV, SV, SVP, X2, Baby-O and 3pi robot.
 *
 * This example uses the OrangutanMotors functions to drive
 * motors in response to the position of user trimmer potentiometer
 * and blinks the red user LED at a rate determined by the trimmer
 * potentiometer position.  It uses the OrangutanAnalog library to measure
 * the trimpot position, and it uses the OrangutanLEDs library to provide
 * limited feedback with the red user LED.
 *
 * https://www.pololu.com/docs/0J20
 * https://www.pololu.com
 * https://forum.pololu.com
 */

unsigned long prevMillis = 0;

int main()
{
  while(1)
  {
    // note that the following line could also be accomplished with:
    // int pot = analogRead(7);
    int pot = read_trimpot();    // determine the trimpot position
    int motorSpeed = pot/2-256;  // turn pot reading into number between -256 and 255
	if(motorSpeed == -256)
		motorSpeed = -255; // 256 is out of range
    set_motors(motorSpeed, motorSpeed);
  
    int ledDelay = motorSpeed;
	if(ledDelay < 0)
	  ledDelay = -ledDelay;  // make the delay a non-negative number
	ledDelay = 256-ledDelay; // the delay should be short when the speed is high

    red_led(1);       // turn red LED on
    delay_ms(ledDelay);

    red_led(0);       // turn red LED off
	delay_ms(ledDelay);
  }
}

thanks again

after installling avr studio 4, it does work, but only sometimes. it worked for the print function, and for the set_motors. Then when I closed avr studio and came back to it a while later the same program came up with the undefined reference error again.

Hello,

Can you show me those errors by posting the entire compiler output that you get compiling an unmodified example?

-Paul