2016-11-23 60 views
-2

我有一個名爲lab7.c的文件,當我編譯使它成爲一個.o文件時,一切都很好。然而那我走了,使之成爲.elf文件,如何將.o文件製作爲.elf文件?

avr-gcc -mmcu=atmega324a -o lab7.elf lab7.o 

我得到的

lab7.o:(.data+0x8): undefined reference to `lcd_putc' 
    lab7.o: In function `main': 
    lab7.c:(.text.startup+0x0): undefined reference to `lcd_init' 
    collect2: error: ld returned 1 exit status 

我已經到處找,試圖解決這一問題,並不能找到任何錯誤消息。我想要做的就是將這段代碼上傳到我的麪包板。我使用的終端上的MacOS塞拉利昂書面C.

lab7.c

#include <avr/io.h>  
#include <stdio.h> 
#include "lcd.h" 

static FILE lcd_stdout=FDEV_SETUP_STREAM(lcd_putc,NULL,_FDEV_SETUP_WRITE); 

#define PUSHED 1 
#define RIGHT_BUTTON ((PINA&_BV(PIN3)) >> 3) 
#define LEFT_BUTTON ((PINA&_BV(PIN0)) >> 0) 

#define LEFTMOST 0b10000000 
#define RIGHTMOST 0b00000001 

int main(void) { 
enum states { left_serve, right_serve, moving_left, moving_right}; 



// Include the following variable declarations here 
char state;  // This variable holds the current state 
char leds;  // Current "court" --- inverse of PORTC 

lcd_init();  // If you want to write to the LCD 
stdout=&lcd_stdout; 

// Required setup for I/O pins 
DDRD = 0xFF;  // All PORTD pins are outputs 
DDRA = 0x10;  // PORTA pin 4 is an output, rest inputs 
PORTA |= 0x10;  // Only pin 4 is important - should be 1 

// Initialize "state" to "left_serve" here 
state=left_serve; 

if (LEFT_BUTTON == PUSHED) { 
    if (leds == LEFTMOST) { 
     state = moving_right; 
    } 
    else 
    { 
     state = right_serve; 
    } 
} 
if (RIGHT_BUTTON == PUSHED){ 
    if (leds == RIGHTMOST) { 
     state = moving_left; 
    } 
    else 
    { 
     state = left_serve; 
    } 
} 
if (RIGHT_BUTTON != PUSHED && LEFT_BUTTON != PUSHED && leds == 0x00) { 
    if (state == moving_right) { 
     state = left_serve; 
    } 
    else 
    { 
     state = right_serve; 
    } 
} 
switch (state) { 

    case moving_left: 
    leds = leds << 1; 
    break; 

    case moving_right: 
    leds = leds >> 1; 
    break; 

    case right_serve: 
    leds = RIGHTMOST; 
    break; 

    case left_serve: 
    leds = LEFTMOST; 
    break; 
}  
void setLEDs(int leds) { 
PORTD= (leds^0x00FF); 
PORTC= (((~leds)>>8)&0x0003)+(PORTC&0xFFFC); 
} 

}  

lcd.h用於

#ifndef __LCD_H__ 
#define __LCD_H__ 

// A. Sheaff 1/10/2008 
// 4 bit LCD interface. 

// Define LCD type. Choose one. 
// #define LCD_1LINE 
// #define LCD_2LINE 
#define LCD_4LINE 
// End choice. 

// Set line length 
#define LCD_LINELEN 16 
// Set New line address 
#define LCD_LINE2A 0x40 

// Register select, Read/Write, Clock 
#define LCD_RS PIN4 
#define LCD_RW PIN6 
#define LCD_E PIN7 
// Code assumes lower 4 bits are for data. 
#define LCD_DATW PORTB 
#define LCD_DATR PINB 

// LCD commands 
#define LCD_CLR  0x01 // LCD Clear 
#define LCD_HOME 0x02 // LCD Home 
#define LCD_SETDD 0x80 // LCD Set Data Address 
#define LCD_SHIFT 0x10 // Shift 
#define LCD_SCURS 0x00 // Shift Cursor 
#define LCD_SDISP 0x08 // Shift Dislay 
#define LCD_SRGHT 0x04 // Shift Right 
#define LCD_SLEFT 0x00 // Shift Left 

// LCD initialization 
void lcd_init(void); 

// Wait for LCD to finish current operation. Returnds DD RAM address. 
unsigned char lcd_busy_wait(void); 

// Write character data to LCD 
void lcd_dwrite(unsigned char d); 
int lcd_putc(char c, struct __file * f); 

// Write instruction data to LCD 
void lcd_iwrite(unsigned char d); 

// Read data memory 
unsigned char lcd_dread(void); 
#endif // __LCD_H__ 

lcd.h用於被我的老師給我的,所以我不t有一個名爲lcd.c的文件

+0

您有鏈接錯誤。添加你的make文件和聲明瞭lcd_putc和lcd_init的頭文件以及.c文件。 –

+0

我該如何去做這件事? –

+0

在運行avr-gcc的目錄中,應該是一個名爲Makefile的文件。您可以編輯問題並添加它。此外,鏈接器找不到'lcd_putc'和'lcd_init'(函數?),因此請在您調用,聲明和實現這些代碼/文件的位置添加源代碼/文件。 –

回答

1

目標文件lab7.o只包含來自其中一個c文件的代碼。它只是整個可執行文件的一部分。

要創建可執行文件,需要將所有部件鏈接在一起。從您顯示的代碼看來,您似乎缺少具有LCD功能的部件。你有一個名爲lcd.o的文件嗎?嘗試

avr-gcc -mmcu=atmega324a -o lab7.elf lab7.o lcd.o 

也可能是LCD功能在庫或歸檔文件中。在這種情況下,您需要以其他方式鏈接它,但仍需要成爲gcc命令的一部分。

如果您具有所有源文件(.c和.h),則可以使用一個gcc命令從源代碼一直編譯爲.elf。

avr-gcc -mmcu=atmega324a -o lab7.elf lab7.c lcd.c lcd.h 
+0

我沒有名爲lcd.o的文件,我只有一個名爲lcd.h的文件 –