如何将.o文件写入.elf文件?(复制)

时间:2022-01-21 19:18:08

This question already has an answer here:

这个问题已经有了答案:

I have a file named lab7.c and when I compile to make it a .o file everything is fine. However then I go to make it a .elf file with

我有一个名为lab7的文件。当我编译成。o文件时一切都很好。然后我把它变成一个。elf文件。

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

I get the error message of

我得到了错误信息。

    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

I have looked everywhere to try and fix this and cant find anything. all I am trying to do is upload this code to my breadboard. I am using terminal on macOS Sierra writing in C.

我四处寻找,试图解决这个问题,却什么也找不到。我所要做的就是把这段代码上传到我的面包板上。我在C的macOS Sierra上使用终端。

lab7.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

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 was given to me by my teacher, therefore I don't have a file named lcd.c

液晶显示器。h是我的老师给我的,所以我没有一个叫lcdc的文件。

1 个解决方案

#1


1  

The object file lab7.o only has the code from one of the c files. It is only part of the whole executable.

lab7的对象文件。o只有一个c文件的代码。它只是整个可执行文件的一部分。

To create the executable, you need to link together all the parts. From the code you show, it looks like you are missing the parts with the LCD functions. Do you have a file called lcd.o? Try

要创建可执行文件,需要将所有部分链接在一起。从您所展示的代码来看,您似乎缺少了带有LCD功能的部分。你有一个叫lcd.o的文件吗?试一试

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

It may also be that the LCD functions are in a library or archive file. In that case, you will need to link it in some other way, but it still needs to be part of the gcc command.

也可能是液晶显示功能在图书馆或档案文件中。在这种情况下,您需要以其他方式链接它,但它仍然需要成为gcc命令的一部分。

If you have all the source files (.c and .h), you could compile from source all the way to .elf with one gcc command.

如果您有所有的源文件(。c和.h),您可以通过一个gcc命令从源代码编译到.elf。

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

#1


1  

The object file lab7.o only has the code from one of the c files. It is only part of the whole executable.

lab7的对象文件。o只有一个c文件的代码。它只是整个可执行文件的一部分。

To create the executable, you need to link together all the parts. From the code you show, it looks like you are missing the parts with the LCD functions. Do you have a file called lcd.o? Try

要创建可执行文件,需要将所有部分链接在一起。从您所展示的代码来看,您似乎缺少了带有LCD功能的部分。你有一个叫lcd.o的文件吗?试一试

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

It may also be that the LCD functions are in a library or archive file. In that case, you will need to link it in some other way, but it still needs to be part of the gcc command.

也可能是液晶显示功能在图书馆或档案文件中。在这种情况下,您需要以其他方式链接它,但它仍然需要成为gcc命令的一部分。

If you have all the source files (.c and .h), you could compile from source all the way to .elf with one gcc command.

如果您有所有的源文件(。c和.h),您可以通过一个gcc命令从源代码编译到.elf。

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