LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2009, 11:14 AM   #1
zack670303
LQ Newbie
 
Registered: Feb 2009
Posts: 23

Rep: Reputation: 15
Question Undefined reference to main error in winavr gcc plugin in avr studio


I am new to programming microcontroller.....I have a got a program from a friend in embedded c for ATmega8 and when i compile in avr studio i get the following error
"c:/winavr-20081205/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr4/crtm8.o.init9+0x0): undefined reference to `main'"
I know it is something very basic...but i can't understand it....It is a program on multiple number lock system....Please could anyone just tell if there is any mistake in basic structure...There are four functions(display,key detection,delay and password check) first and then main program... For those who have patience i give the program

# include <avr/io.h>
/*
PROGRAM FOR MULTIPLE NUMBER LOCK USING ATmega8.

************************************************************

Function for displaying a digit
*/


void display(int y)
{
if(y==0)
PORTB=0xC0;

else if(y==1)
PORTB=0x79;

else if(y==2)
PORTB=0x24;

else if(y==3)
PORTB=0x30;

else if(y==4)
PORTB=0x19;

else if(y==5)
PORTB=0x12;

else if(y==6)
PORTB=0x02;

else if(y==7)
PORTB=0x78;

else if(y==8)
PORTB=0x00;

else if(y==9)
PORTB=0x10;

else if(y==10)
PORTB=0x3F;

else if(y==11)
PORTB=0x7E;
}
/*
Function to detect a key from key board
*/

int keydetect(void)
{
unsigned char i,y,z;
int dgt=15;
while(dgt==15)
{
int k=0;
y=0x00;
PORTD=0x02;
while(y==0x00)
{
k=0;
i=PIND;
y=(i&0xE0);
if(PORTD==0x10)
{
k=1;
PORTD=0x01;
}

PORTD=(PORTD*0x02);
}
if(k!=1)
PORTD=PORTD/0x02;
else
PORTD=0x10;
z=PORTD;

if(z==0x02)
{
if(y==0x20)
dgt=1;
else if(y==0x40)
dgt=2;
else if(y==0x80)
dgt=3;
}

else if(z==0x04)
{
if(y==0x20)
dgt=4;
else if(y==0x40)
dgt=5;
else if(y==0x80)
dgt=6;
}

else if(z==0x08)
{
if(y==0x20)
dgt=7;
else if(y==0x40)
dgt=8;
else if(y==0x80)
dgt=9;
}

else if(z==0x10)
{
if(y==0x20)
dgt=10;
else if(y==0x40)
dgt=0;
else if(y==0x80)
dgt=11;
}
display(dgt);
while((PIND&0xE0)!=0);
}
return(dgt);
}
/*
Delay function
*/
void delay(int y)
{
int x,z;
for(x=0;x<y;x++)
{
for(z=0;z<30000;z++);
}
}

/*
Function t check a password
*/

int check(int y)
{
int x[3],i,j,e;
int p[3]={5,5,5};
int p2[3]={2,3,4};
int p3[3]={3,4,5};
int p4[3]={4,5,6};
int p5[3]={5,6,7};
int p6[3]={6,7,8};

PORTB=0x0C;
for(i=0;i<3;i++)
{
x[i]=keydetect();
PORTB=0x77;
for(j=0;j<1000;j++);
}
PORTB=0x7E;
e=0;
while(e!=10)
e=keydetect();
j=0;

if(y==0)
{
for(i=0;i<3;i++)
{
if(x[i]!=p[i])
j=1;
}
}

else if(y==1)
{
for(i=0;i<3;i++)
{
if(x[i]!=p1[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x01);
delay(2000);
}
}

else if(y==2)
{
for(i=0;i<3;i++)
{
if(x[i]!=p2[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x02);
delay(2000);
}
}

else if(y==3)
{
for(i=0;i<3;i++)
{
if(x[i]!=p3[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x04);
delay(2000);
}
}

else if(y==4)
{
for(i=0;i<3;i++)
{
if(x[i]!=p4[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x08);
delay(2000);
}
}

else if(y==5)
{
for(i=0;i<3;i++)
{
if(x[i]!=p5[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x10);
delay(2000);
}
}

else if(y==6)
{
for(i=0;i<3;i++)
{
if(x[i]!=p6[i])
j=1;
}
if(j==0)
{
PORTB=0x08;
PORTC=(PORTC^0x20);
delay(2000);
}
}

return(j);
}

/*
The main function
*/

int main(void)
{
DDRD=0x1E;
DDRB=0xFF;
DDRC=0x7F;
int y,i,j,e;

while(1)
{
i=check(0);

while(i==0)
{
PORTB=0x2B;
y=keydetect();
display(y);
e=0;
while(e!=10)
e=keydetect();
j=check(y);
if(j==1)
{
PORTB=0X86;
e=0;
while(e!=11)
e=keydetect();
}
}
}
return(1);
}

Last edited by zack670303; 02-26-2009 at 11:19 AM.
 
Old 02-26-2009, 12:52 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
When attempting to compile your code on a normal Linux machine, I was able to compile your code as long as I did the following:
Code:
/* Commented out this include because the header file
   does not exist on my system.
#include <avr/io.h>
*/

/* Added these definitions because they were not defined in the
   code posted by the OP
*/
unsigned char PORTB;
unsigned char PORTC;
unsigned char PORTD;
unsigned char PIND;
unsigned char p1[3];
unsigned char DDRD;
unsigned char DDRB;
unsigned char DDRC;

...
If you make the changes shown above to your code and it still does not compile, then the issue lies with your compiler, not with the code.

P.S. Don't double-post your query on this forum!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting undefined reference to main error?? Mistro116@yahoo.com Programming 14 07-29-2011 08:28 AM
'Undefined refrence to main error' in winavr gcc compiler???? zack670303 Programming 1 02-26-2009 12:47 PM
Undefined reference to 'main' myhanguk Programming 5 07-05-2007 03:32 AM
gcc ld error. Undefined reference redness Linux - Software 4 02-08-2005 02:01 AM
Undefined reference error using gcc with Redhat Linux 9.0 armesk Programming 2 08-28-2003 06:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration