AVR USART file not recognized in C - c

I have the following test setup below. When I compile the code, I get the following error: The initUSART is not recognized, but I have included the file in the appropriate places. Did I miss out on anything?
AVR Development Stack
Get input main.c file...
Compile code and return elf file...
main.o: In function `main':
/Users/sebastianscharf/Documents/Hardware/AVR/main.c:14: undefined reference to `initUSART'
collect2: error: ld returned 1 exit status
avr-objcopy: 'main.elf': No such file
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: Expected signature for ATmega328 is 1E 95 14
Double check chip, or use -F to override this check.
avrdude done. Thank you.
main file
#include "config.h"
#include <avr/io.h>
#include <util/delay.h>
#include "USART.h"
int main(void) {
char serialCharacter;
// --- INITS --- //
DDRB = 0xff;
// Set up LED for output
initUSART();
return 0;
}
USART.h
/* These are defined for convenience */
#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0)
#define USART_READY bit_is_set(UCSR0A, UDRE0)
uint8_t receiveByte(void);
/* Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, and configures the hardware USART*/
void initUSART(void);
USART.c
#include "config.h"
#include <avr/io.h>
#include <util/setbaud.h>
#include "USART.h"
void initUSART(void) {
// Requires BAUD
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
// Enable USART
UCSR0B = (1 << TXEN0) | (1 << RXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit
}
bash file
#!/bin/bash
source bashColors.sh
echo -e "${IGreen}AVR Development Stack${Reset}"
echo -e "${BIBlue}Get input main.c file...${Reset}"
avr-gcc -g -Os -mmcu=atmega328p -c main.c USART.c &&
echo -e "${BIBlue}Compile code and return elf file...${Reset}"
avr-gcc -g -mmcu=atmega328p -o main.elf main.o &&
echo -e "${BIBlue}Convert elf file to hex...${Reset}"
# avr-objcopy converts into hex file. -j indicates that we want the information from the .text and .data segment extracted.
avr-objcopy -j .text -j .data -O ihex main.elf out.hex &&
echo -e "${BIBlue}Uploading data to microcontroller...${Reset}"
avrdude -c usbtiny -p m328

You compiled both source files properly to .o with:
avr-gcc -g -Os -mmcu=atmega328p -c main.c USART.c
Now the main.o file contains an external reference to initUSART (partly thanks to the #include "USART.h" directive which provides the proper prototype)
but this link line:
avr-gcc -g -mmcu=atmega328p -o main.elf main.o &&
only references main.o. You need to add USART.o so the symbol is resolved by the linker (the linker doesn't care about .h include files), like this:
avr-gcc -g -mmcu=atmega328p -o main.elf main.o USART.o &&

Related

AVR ATtiny814 program executes first function, ignoring main

I'm trying to write software for an ATtiny814 (tiny avr 1-series) microcontroller, but I encounter a strange problem: whatever the first function in main.c is, gets executed and the remaining code gets ignored - including the main() function.
I'm using the avr-toolchain from the Arduino IDE on macOS, but I'm not using the IDE, I just added the bin/ directory of the avr-toolchain to the PATH variable. The code just compiles fine without errors or warnings. Using pyupdi I can successfully flash the program to the chip, and again it works fine - except that only code from the first function in the main.c is executed.
Makefile:
TARGET = project
CLOCK = 2000000 # configured in main
SOURCES = main.c
OBJECTS = $(SOURCES:.c=.o)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -D__AVR_DEVICE_NAME__=attiny814 -D__AVR_DEV_LIB_NAME__=tn814
# compiling and linking, target is the finished hex file
all: $(TARGET).hex
# compile source files to object files
.c.o:
$(COMPILE) -c $< -o $#
# link the object files together
$(TARGET).elf: $(OBJECTS)
$(COMPILE) $(OBJECTS) -o $(TARGET).elf
# convert elf file to hex file
$(TARGET).hex: $(TARGET).elf
avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex
clean:
rm -f $(TARGET).hex $(TARGET).elf $(OBJECTS)
Example 1
main.c:
#include <avr/io.h>
void test1() {
// turn on PB1
PORTB.DIR |= PIN1_bm;
PORTB.OUT |= PIN1_bm;
}
int main() {
// disable protection to configure clock frequency
CCP = CCP_IOREG_gc;
// configure CPU frequency
CLKCTRL.MCLKCTRLA = CLKCTRL_CLKSEL_OSC20M_gc; // use 20 MHz internal clock as source
CLKCTRL.MCLKCTRLB = CLKCTRL_PDIV_10X_gc | CLKCTRL_PEN_bm; // divide by 10 and enable divider
// turn on PB0
PORTB.DIR |= PIN0_bm;
PORTB.OUT |= PIN0_bm;
// main program loop
while(1) {};
return 0;
}
Here, only test1() is executed, as only PB1 turns on.
Example 2
main.c:
#include <avr/io.h>
// prototype
void test1();
int main() {
// disable protection to configure clock frequency
CCP = CCP_IOREG_gc;
// configure CPU frequency
CLKCTRL.MCLKCTRLA = CLKCTRL_CLKSEL_OSC20M_gc; // use 20 MHz internal clock as source
CLKCTRL.MCLKCTRLB = CLKCTRL_PDIV_10X_gc | CLKCTRL_PEN_bm; // divide by 10 and enable divider
// turn on PB0
PORTB.DIR |= PIN0_bm;
PORTB.OUT |= PIN0_bm;
// main program loop
while(1) {};
return 0;
}
void test1() {
// turn on PB1
PORTB.DIR |= PIN1_bm;
PORTB.OUT |= PIN1_bm;
}
Here, main() gets skipped and again test1() is executed, turning PB1 on.
Example 3
main.c:
#include <avr/io.h>
void test0() {
// turn on PB0
PORTB.DIR |= PIN0_bm;
PORTB.OUT |= PIN0_bm;
}
void test1() {
// turn on PB1
PORTB.DIR |= PIN1_bm;
PORTB.OUT |= PIN1_bm;
}
No main function at all. No compiler errors. Only test0() gets executed and PB0 turns on.
I have no clue whats going on here. Btw, using the same avr-toolchain setup, I can write software for an ATtiny841 (it's a different architecture/series) as expected.
Okay, found a solution: When I compile it as described here https://github.com/vladbelous/tinyAVR_gcc_setup#example-of-compiler-usage, it works.
So in my case:
avr-gcc -c -Os -DF_CPU=2000000 -mmcu=attiny814 main.c
avr-gcc -mmcu=attiny814 -o main.elf main.o
avr-objcopy -j .text -j .data -j .rodata -O ihex main.elf main.hex
Fixed Makefile:
TARGET = project
MCU = attiny814
DEVICE = tiny814
# clock settings applied in main.c
CLOCK = 2000000
PROGRAMMER = /dev/tty.usbserial-A50285BI
SOURCES = main.c
OBJECTS = $(SOURCES:.c=.o)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(MCU)
# compiling and linking, target is the finished hex file
all: $(TARGET).hex
# compile source files to object files
.c.o:
$(COMPILE) -c $< -o $#
# link the object files together
$(TARGET).elf: $(OBJECTS)
$(COMPILE) $(OBJECTS) -o $(TARGET).elf
# convert elf file to hex file
$(TARGET).hex: $(TARGET).elf
avr-objcopy -j .text -j .data -j .rodata -O ihex $(TARGET).elf $(TARGET).hex
# flash (call make flash), requires pyupdi installed
flash: $(TARGET).hex
python3 -m updi.pyupdi -d $(DEVICE) -c $(PROGRAMMER) -f "$(shell pwd)/$(TARGET).hex"
clean:
rm -f $(TARGET).hex $(TARGET).elf $(OBJECTS)

Undeclared Identifiers in header file with AVR-GCC

I created the following header file with definitions for AVR pins to be used in my code.
#define LED_PORT PORTB
#define LED_PIN PINB
#define LED_DDR DDRB
#define LED0 PB0
I encounter two failures I am not able to solve.
1) I have two issues in this header file, shown here:
2) Also the functions I created in the header file for USART in the USART.h file, I implemented, are not being recognized
I actually do not understand why that is. As the code clearly has the header file implemented.
#ifndef F_CPU
#define F_CPU 1000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include "pinDefinition.h"
#include "USART.h"
int main(void) {
char serialCharacter;
LED_DDR = 0xff;
initUSART();
printString("Hello World!\r\n");
while (1) {
serialCharacter = receiveByte();
transmitByte(serialCharacter);
LED_PORT = serialCharacter;
}
return (0);
}
The compiler I am using is AVR-GCC.
Furthermore, when I include the USART.c directly, then everything is working fine. I don't understand why the header file is not working though.
I created a script that runs the compiler/linker:
#!/bin/bash
avr-gcc -g -Os -mmcu=atmega328p -c code.c util.c USART.c
avr-gcc -g -mmcu=atmega328p -o code.elf code.o
avr-objcopy -j .text -j .data -O ihex code.elf code.hex
avr-size --format=avr --mcu=atmega328p code.elf
This returns the above mentioned error.
The USART.h looks like this:
/* These are defined for convenience */
#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0)
#define USART_READY bit_is_set(UCSR0A, UDRE0)
#include <stdint.h>
void initUSART(void);
void transmitByte(uint8_t data);
uint8_t receiveByte(void);
void printString(const char myString[]);
void readString(char myString[], uint8_t maxLength);
void printByte(uint8_t byte);
void printWord(uint16_t word);
void printBinaryByte(uint8_t byte);
char nibbleToHex(uint8_t nibble);
char nibbleToHexCharacter(uint8_t nibble);
void printHexByte(uint8_t byte);
uint8_t getNumber(void);
I really require some help as I am trying to solve this now for three days and read a lot of different sources (I am getting my ropes together still with C so please be mindful).C
I created a script that runs the compiler/linker:
avr-gcc -g -Os -mmcu=atmega328p -c code.c util.c USART.c
This command generates three files: code.o, util.o and USART.o, whereas the following link command only links one object file:
avr-gcc -g -mmcu=atmega328p -o code.elf code.o
Hence, all functions from the latter two compilation units are missing, which leads tu bunch of "undefined reference to" from the linker. Fix is to link all three object files:
avr-gcc -g -mmcu=atmega328p -o code.elf code.o util.o USART.o
Undeclared Identifiers in header file
A header file (module.h) in C just contains declarations of functions in the module (module.c), but the implementation / definition of the functions is in the *.c file.
(One exception are static inline functions which are defined in the header, but this is not the case for the undefined references from your code.)

issues with linking undefined reference help ! very stuck

I am getting a "undefined reference to 'i2c_master_init' " i think this is to do some linking but not to sure how to correct ?
UPDATE :
iv been trying to find the answer for this for a while now. and have noticed a few bit but just not sure what i am doing wrong ! sorry it must be getting hard to read now !
This is from i2c_master.h and give me no issues
static inline void i2c_master_enable(const struct i2c_master_module *const module){
/* Sanity check of arguments */
Assert(module);
Assert(module->hw);
SercomI2cm *const i2c_module = &(module->hw->I2CM);
/* Timeout counter used to force bus state */
uint32_t timeout_counter = 0;
/* Wait for module to sync */
_i2c_master_wait_for_sync(module);
/* Enable module */
i2c_module->CTRLA.reg |= SERCOM_I2CM_CTRLA_ENABLE;
#if I2C_MASTER_CALLBACK_MODE == true
/* Enable module interrupts */
system_interrupt_enable(_sercom_get_interrupt_vector(module->hw));
#endif
/* Start timeout if bus state is unknown */
while (!(i2c_module->STATUS.reg & SERCOM_I2CM_STATUS_BUSSTATE(1))) {
timeout_counter++;
if(timeout_counter >= (module->unknown_bus_state_timeout)) {
/* Timeout, force bus state to idle */
i2c_module->STATUS.reg = SERCOM_I2CM_STATUS_BUSSTATE(1);
/* Workaround #1 */
return;
}
This is in the same file and is giving me issues
enum status_code i2c_master_init(struct i2c_master_module *const module,
Sercom *const hw, const struct i2c_master_config *const config);
this is my code.....
#include <asf.h>
void initialize(void)
{
system_init();
delay_init();
ioport_init();
}
//I2c slave address
#define AM2315_ADDRESS 0xB8 //temp and humity sensor
#define DATA_LENGTH 10
#define TIMEOUT 1000 //number of time to try and send the packet before it
fails
static uint8_t write_buffer[DATA_LENGTH] = {0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09};
static uint8_t read_buffer[DATA_LENGTH];
struct i2c_master_module i2c_master_instance;
void configure_i2c_master(void)
{
/* Initialize config structure and software module. */
struct i2c_master_config config_i2c_master;
i2c_master_get_config_defaults(&config_i2c_master);
/* Change buffer timeout to something longer. */
config_i2c_master.buffer_timeout = 10000;
/* Initialize and enable device with config. */
i2c_master_init(&i2c_master_instance, SERCOM1, &config_i2c_master);
i2c_master_enable(&i2c_master_instance);
}
int main (void)
{
initialize();
configure_i2c_master();
uint8_t timeout = 0 ;
struct i2c_master_packet packet ={
.address = AM2315_ADDRESS,
.data_length = DATA_LENGTH,
.data = write_buffer,
.ten_bit_address = false,
.hs_master_code = 0x0,
};
ioport_set_pin_dir(PIN_PA27, IOPORT_DIR_OUTPUT);
while(1)
{
delay_ms(1000);
ioport_set_pin_level(PIN_PA27, true);
delay_ms(1000);
ioport_set_pin_level(PIN_PA27, false);
//write buffer to slave untill success
while(i2c_master_read_packet_wait(&i2c_master_instance, &packet) !=
STATUS_OK){
if (timeout++ ==TIMEOUT){
break;
}
}
//Read from slave until success.
packet.data = read_buffer;
while (i2c_master_read_packet_wait(&i2c_master_instance, &packet) !=
STATUS_OK){
if (timeout++ ==TIMEOUT){
break;
}
}
}
}
this is my output...
------ Build started: Project: GccBoardProject1, Configuration: Debug ARM --
Build started.
Project "GccBoardProject1.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition;
('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files
(x86)\Atmel\Studio\7.0\Vs\Compiler.targets" from project
"C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\GccBoardProject1.cproj"
(target "Build" depends on it):
Task "RunCompilerTask"
Shell Utils Path C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils
C:\Program Files (x86)\Atmel\Studio\7.0\shellUtils\make.exe all --jobs 4 --
output-sync
C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\src\main.c(35,6): warning: no
previous prototype for 'initialize' [-Wmissing-prototypes]
void initialize(void)
^~~~~~~~~~
C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\src\main.c(58,6): warning: no
previous prototype for 'configure_i2c_master' [-Wmissing-prototypes]
void configure_i2c_master(void)
^~~~~~~~~~~~~~~~~~~~
Building file: ../src/main.c
Invoking: ARM/GNU C Compiler : 6.3.1
"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\arm\arm-gnu-
toolchain\bin\arm-none-eabi-gcc.exe" -x c -mthumb -D__SAMD21G18A__ -DDEBUG
-DBOARD=USER_BOARD -DARM_MATH_CM0PLUS=true -DSYSTICK_MODE -
DI2C_MASTER_CALLBACK_MODE=true -I"../src/ASF/common/boards" -
I"../src/ASF/sam0/utils" -I"../src/ASF/sam0/utils/header_files" -
I"../src/ASF/sam0/utils/preprocessor" -
I"../src/ASF/thirdparty/CMSIS/Include" -
I"../src/ASF/thirdparty/CMSIS/Lib/GCC" -I"../src/ASF/common/utils" -
I"../src/ASF/sam0/utils/cmsis/samd21/include" -
I"../src/ASF/sam0/utils/cmsis/samd21/source" -
I"../src/ASF/sam0/drivers/system"
-I"../src/ASF/sam0/drivers/system/clock/clock_samd21_r21_da_ha1" -
I"../src/ASF/sam0/drivers/system/clock" -
I"../src/ASF/sam0/drivers/system/interrupt" -
I"../src/ASF/sam0/drivers/system/interrupt/system_interrupt_samd21" -
I"../src/ASF/sam0/drivers/system/pinmux" -
I"../src/ASF/sam0/drivers/system/power" -
I"../src/ASF/sam0/drivers/system/power/power_sam_d_r_h" -
I"../src/ASF/sam0/drivers/system/reset" -
I"../src/ASF/sam0/drivers/system/reset/reset_sam_d_r_h" -
I"../src/ASF/common2/boards/user_board" -I"../src" -I"../src/config" -
I"../src/ASF/common2/services/delay" -
I"../src/ASF/common2/services/delay/sam0" -
I"../src/ASF/common/services/ioport" -I"../src/ASF/sam0/drivers/sercom" -
I"../src/ASF/sam0/drivers/sercom/i2c" -O1 -fdata-sections -ffunction-
sections -mlong-calls -g3 -Wall -mcpu=cortex-m0plus -c -pipe -fno-strict-
aliasing -Wall -Wstrict-prototypes -Wmissing-prototypes -Werror-implicit-
function-declaration -Wpointer-arith -std=gnu99 -ffunction-sections -fdata-
sections -Wchar-subscripts -Wcomment -Wformat=2 -Wimplicit-int -Wmain -
Wparentheses -Wsequence-point -Wreturn-type -Wswitch -Wtrigraphs -Wunused -
Wuninitialized -Wunknown-pragmas -Wfloat-equal -Wundef -Wshadow -Wbad-
function-cast -Wwrite-strings -Wsign-compare -Waggregate-return -Wmissing-
declarations -Wformat -Wmissing-format-attribute -Wno-deprecated-
declarations -Wpacked -Wredundant-decls -Wnested-externs -Wlong-long -
Wunreachable-code -Wcast-align --param max-inline-insns-single=500 -MD -MP -
MF "src/main.d" -MT"src/main.d" -MT"src/main.o" -o "src/main.o"
"../src/main.c"
Finished building: ../src/main.c
src/main.o: In function `configure_i2c_master':
C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\src\main.c(69,1): error:
undefined reference to `i2c_master_init'
src/main.o: In function `main':
C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\src\main.c(103,1): error:
undefined reference to `i2c_master_read_packet_wait'
collect2.exe(0,0): error: ld returned 1 exit status
make: *** [GccBoardProject1.elf] Error 1
Building target: GccBoardProject1.elf
Invoking: ARM/GNU Linker : 6.3.1
"C:\Program Files (x86)\Atmel\Studio\7.0\toolchain\arm\arm-gnu-
toolchain\bin\arm-none-eabi-gcc.exe" -o GccBoardProject1.elf
src/ASF/sam0/drivers/sercom/sercom.o
src/ASF/sam0/drivers/sercom/sercom_interrupt.o
src/ASF/common2/services/delay/sam0/systick_counter.o
src/ASF/common2/boards/user_board/init.o
src/ASF/common/utils/interrupt/interrupt_sam_nvic.o
src/ASF/sam0/drivers/system/clock/clock_samd21_r21_da_ha1/clock.o
src/ASF/sam0/drivers/system/clock/clock_samd21_r21_da_ha1/gclk.o
src/ASF/sam0/drivers/system/interrupt/system_interrupt.o
src/ASF/sam0/drivers/system/pinmux/pinmux.o
src/ASF/sam0/drivers/system/system.o
src/ASF/sam0/utils/cmsis/samd21/source/gcc/startup_samd21.o
src/ASF/sam0/utils/cmsis/samd21/source/system_samd21.o
src/ASF/sam0/utils/syscalls/gcc/syscalls.o src/main.o -mthumb -Wl,-
Map="GccBoardProject1.map" --specs=nano.specs -Wl,--start-group -
larm_cortexM0l_math -lm -Wl,--end-group -
L"../src/ASF/thirdparty/CMSIS/Lib/GCC" -Wl,--gc-sections -mcpu=cortex-
m0plus -Wl,--entry=Reset_Handler -Wl,--cref -mthumb -
T../src/ASF/sam0/utils/linker_scripts/samd21/gcc/samd21g18a_flash.ld
C:\Users\benvo\Documents\Atmel
Studio\7.0\GccBoardProject1\GccBoardProject1\Debug\Makefile(333,1): error:
recipe for target 'GccBoardProject1.elf' failed
The command exited with code 2.
Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "GccBoardProject1.cproj" --
FAILED.
Done building project "GccBoardProject1.cproj" -- FAILED.
Build FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========
TAGS:
Tools, Atmel Software Framework (ASF) and Atmel Start (ASFv4), all
architectures

Linking multiple object files into a binary AVR GCC

I have the following headers and source files:
AVR_comunic.h
#ifndef AVR_COMUNIC_H_
#define AVR_COMUNIC_H_
#include<avr/io.h>
#define FOSC 20000000UL // Clock Speed
#define BAUD 19200
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int);
int USART_WriteChar(unsigned char);
// int USART_Receive( unsigned char *);
void USART_Flush(void);
#endif /* AVR_COMUNIC_H_ */
AVR_comunic.c
#include "AVR_comunic.h"
void USART_Flush( void )
{
...
}
void USART_Init( unsigned int ubrr)
{
...
}
int USART_WriteChar(unsigned char data)
{
...
}
and main_f.c
#include"AVR_comunic.h"
void init_ports()
{
DDRD|= (1<<PD7);
}
int main(void)
{
init_ports();
...
while(1)
{
// now what?
}
return 1;
}
I wrote a batch file in Notepad++ to obtain the .hex file for this program
COMPILE_BUILD.bat
ECHO OFF
ECHO ----------------------------------------------------------
ECHO ----- executing your commands : compile and build -------
ECHO ----------------------------------------------------------
avr-gcc -g -Wall -Os -DF_CPU=20000000UL -mmcu=atmega88p -c AVR_comunic.c
avr-gcc -g -Wall -Os -DF_CPU=20000000UL -mmcu=atmega88p -c main_f.c
avr-gcc -g -mmcu=atmega88p -o AVR_comunic.elf main_f.o AVR_comunic.o
avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst
avr-gcc -g -mmcu=atmega8 -Wl,-Map,AVR_comunic.map -o AVR_comunic.elf AVR_comunic.o
avr-objcopy -j .text -j .data -O ihex AVR_comunic.elf AVR_comunic.hex
ECHO ----------------------------------------------------------
ECHO ---------------------- happy ? ---------------------------
ECHO ----------------------------------------------------------
pause
I get the following error:
... : undefined reference to 'main'
avr-objcopy: 'AVR_comunic.elf' no such file
I understand from the information on internet that the linker does thinks that there is no "main" function ... but it is ... what am I doing wrong? Or how should I write to compile multiple source files?
I understand from the information on internet that the linker does thinks that there is no "main" function ... but it is ... what am I doing wrong?
This command:
avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst
attempts to extract info from AVR_communic.elf, which isn't produced yet. You want to move it after this command:
avr-gcc -g -mmcu=atmega8 -Wl,-Map,AVR_comunic.map -o AVR_comunic.elf AVR_comunic.o
The above command fails with undefined reference to: 'main' because you are not linking main_f.o into it.
It appears that what you really want is:
avr-gcc -g -mmcu=atmega88p -o AVR_communic.elf main_f.o AVR_comunic.o
avr-objdump -h -S AVR_comunic.elf > AVR_comunic.lst
avr-objcopy -j .text -j .data -O ihex AVR_comunic.elf AVR_comunic.hex

undefined reference to function called 'full_adder(BIT A, BIT B, BIT Ci, BIT *Co)'

I am a CS student learning how to program in C. I have 3 files and a Makefile. I have
logic.c logic.h main.c
the top of logic.c is:
#include "logic.h"
//use this function in 4 bit adder
//BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co);
int adder(int O1, int O2, BIT carryIn, BIT *carryOut){
printf("in adder");
return 0x0;
}
BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co){
BIT S = FALSE;
//implementation of if else's based on Truth Table <br>
if((A==FALSE)&&(B==FALSE)&&(Ci==FALSE)){
S=FALSE;
*Co = FALSE;
} <br>
return S;
}
the top of main.c is:
#include "logic.h"
BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co);
in main.c I have my main function, at the bottom of main function I have :
assert((full_adder(FALSE,FALSE,FALSE,&Co)==FALSE) && (Co==FALSE));
adder(FALSE,FALSE,FALSE,&Co2);
return 0;
I am getting the error:
main.o: In function 'main': main.c:158: undefined referenceto 'full_adder'
collect2: ld returned 1 exit status <br>
make: *** [pa3] Error 1 <br>
./bscript: line 3: pa3: command not found
This is homework, but I have spent a good deal of time on this problem and am looking for some help. I have the correct return type for adder, full_adder, and I declare the function in the top of both logic.c and main.c.
The questions that did not answer my question were:
Undefined Reference to a function
"Undefined reference to function" error
Any help is appreciated.
EDIT:
logic.c is getting compiled and it does create a logic.o file.
EDIT: my prof says that I cannot edit logic.h
EDIT: makefile:
# Makefile template for CS 270 (specialized for PA3)
# List of files
C_SRCS = main.c logic.c
C_OBJS = main.o logic.o
C_HEADERS = logic.h
OBJS = ${C_OBJS}
EXE = pa3
# Compiler and loader commands and flags
GCC = gcc
GCC_FLAGS = -g -std=c99 -Wall -O0 -c
LD_FLAGS = -g -std=c99 -Wall -O0
# Target is the executable
pa3 : $(OBJS)
#echo "Linking all object modules ..."
$(GCC) $(LD_FLAGS) $(OBJS) -o $(EXE)
#echo ""
# Recompile C objects if headers change
${C_OBJS}: ${C_HEADERS}
# Compile .c files to .o files
.c.o:
#echo "Compiling each C source file separately ..."
$(GCC) $(GCC_FLAGS) $<
#echo ""
# Clean up the directory
clean:
#echo "Cleaning up project directory ..."
rm -f *.o $(EXE) core a.out
#echo ""
EDIT: I compile my code with the script:
#!/usr/bin/sh
make
pa3
First of all, separate definitions from declarations:
logic.h:
#ifndef _LOGIC_H_
#define _LOGIC_H_
/* Define BIT as before */
int adder(int O1, int O2, BIT carryIn, BIT *carryOut);
BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co);
#endif
logic.c:
#include <stdio.h>
#include "logic.h"
int adder(int O1, int O2, BIT carryIn, BIT *carryOut){
printf("in adder");
return 0x0;
}
BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co){
BIT S = FALSE;
//implementation of if else's based on Truth Table
if((A==FALSE)&&(B==FALSE)&&(Ci==FALSE)){
S=FALSE;
*Co = FALSE;
}
return S;
}
main.c:
#include "logic.h"
int main(int argc, char* argv[]) {
/* Do your stuff */
return 0;
}
Now things are where they should be and your logic.h supplies the needed information to the compiler.
Your makefile should not need any fancy stuff with only these three files.
This needs to be done via the makefile:
gcc -c -o main.o main.c
gcc -c -o logic.o logic.c
gcc -o main main.o logic.o
(The logical problems that you have in your full_adder are left for your to solve...)
I have seen linkers not-find/find functions depending on the order of the object files.
In your makefile you may want to try changing the order:
C_OBJS = logic.o main.o
I declare the function in the top of both logic.c and main.c.
The function prototype
BIT full_adder(BIT A, BIT B, BIT Ci, BIT *Co);
should only occur once, in logic.h.
The error you are getting is from the linker.
You may want to just try and compile/link it outside the Makefile:
gcc -c logic.c
gcc -o main main.c logic.o

Resources