Add my own library

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, this is maybe a simple question, but I can't figure it out...

 

I have created a simple library called test.h wich contains a simple function definition, and a test.c wich implements it.

 

When I compile, It cannot find my library. I suppose this is because of my makefile. Here is the line I use to compile :

 

avrdude -c arduino -P COM4 -b 19200 -F -p atmega328p -U flash:w:main.hex:i

Do I need to add something else so my library header is recognize?

 

I get the following error:

 

main.o: In function `main':
main.c:(.text+0x5a): undefined reference to `myFunction'

 

Thanks.

This topic has a solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That line does not compile! avrdude is the command to program what you built into the AVR. 

 

Compilation usually consists of

avr-gcc -c file1.c file1.o
avr-gcc -c file2.c file2.o
etc.

avr-gcc file1.o file2.o ... -o project.elf
avr-objcopy -O ihex -j .text -j .data project.elf project.hex

Notice how the multiple .c files are each compiled using -c to create .o files then all those files are linked together without using -c

 

It would be far easier to Google "AVR MFILE"  and use that! Or,  better yet,  get an IDE. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PS @Michael, you seriously need to try this Web site using an Android tablet and SwiftKey. It took about 15 minutes to type the post above - close to unusable! 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I actually already use a Makefile. Maybe that it will be easier for you what is wrong with it. I use the "make flash" to compile and send it to the MCU...

 

# Name: Makefile
# Author: <insert your name here>
# Copyright: <insert your copyright message here>
# License: <insert your license reference here>
 
# DEVICE ....... The AVR device you compile for
# CLOCK ........ Target AVR clock rate in Hertz
# OBJECTS ...... The object files created from your source files. This list is
#                usually the same as the list of source files with suffix ".o".
# PROGRAMMER ... Options to avrdude which define the hardware you use for
#                uploading to the AVR and the interface where this hardware
#                is connected.
# FUSES ........ Parameters for avrdude to flash the fuses appropriately.
 
DEVICE     = atmega328p
CLOCK      = 8000000
PROGRAMMER = -c arduino -P COM4 -b 19200 -F
OBJECTS    = main.o
# CLOCK IS NOT DIVIDED BY 8 => 8Mhz on ATMega328p (lfuse = 0xE2)
FUSES      = -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m -U efuse:w:0x07:m
 
 
######################################################################
######################################################################
 
# Tune the lines below only if you know what you are doing:
 
AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE = avr-gcc -std=c99 -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
 
# symbolic targets:
all:	main.hex
 
.c.o:
	$(COMPILE) -c $< -o $@
 
.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.
 
.c.s:
	$(COMPILE) -S $< -o $@
 
flash:	all
	$(AVRDUDE) -U flash:w:main.hex:i
 
fuse:
	$(AVRDUDE) $(FUSES)
 
install: flash fuse
 
# if you use a bootloader, change the command below appropriately:
load: all
	bootloadHID main.hex
 
clean:
	rm -f main.hex main.elf $(OBJECTS)
 
# file targets:
main.elf: $(OBJECTS)
	$(COMPILE) -o main.elf $(OBJECTS)
 
main.hex: main.elf
	rm -f main.hex
	avr-objcopy -j .text -j .data -O ihex main.elf main.hex
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.
 
# Targets for code debugging and analysis:
disasm:	main.elf
	avr-objdump -d main.elf
 
cpp:
	$(COMPILE) -E main.c

Thanks !

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In that case change:

OBJECTS    = main.o test.o

and as you add more .c files just add a reference to their .o file here too. 

 

Oh and that Makefile has:

cpp:
	$(COMPILE) -E main.c

I guess it does no harm but it makes little sense either. 

 

BTW I would use "make all" or just "make" rather than "make flash" until the compilation succeeds as there's no point asking for the flash to be programmed while you still get build errors. 

 

(this does not strike me as a particularly well thought out Makefile, I'd still go with Mfile or an IDE) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It works now with the test.o added to OBJECTS. Thanks!

 

I don't know MFILE, I'll look at it.