Author Topic: a problem compiling eeprom of avr(atmega16,winavr) program  (Read 31662 times)

zage2009

  • Guest
a problem compiling eeprom of avr(atmega16,winavr) program
« on: August 03, 2011, 02:39:22 pm »
I used CB to compile the avr program which wrote and read eeprom inside avr(atmege16) and couldn't compile sucessfully.The compiler is winavr-20100110 and the optimization level is zero.The error messages were undefined reference to __eewr_byte_m16 and undefined reference to __eerd_byte_m16,but compiling it in avrstudio4.18 is sucessfully and works.here is the promgram:
Code: [Select]
#include <avr/io.h>
#include <avr/eeprom.h>
void init(void);
void write_com(unsigned char com);
void write_data(unsigned char data);

int main(void)
{

    unsigned char i;
eeprom_busy_wait();
eeprom_write_byte(0x00,0x01);
eeprom_busy_wait();
i = eeprom_read_byte(0x00);
while(1);
}


I really don't how to do.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9508
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #1 on: August 03, 2011, 04:41:42 pm »
I really don't how to do.
Link against the libraries that provide these references in the right order. Consult the AVR SDK documentation which libs these are.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #2 on: August 03, 2011, 06:03:27 pm »
U have to add the library '(WinAVR path)\avr\lib\avr5\libc.a' to the 'link libraries' under 'project->build options->linker settings' for ur code to compile. It's quite strange though that the include file doesn't link to the corresponding library.

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7265
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #3 on: August 03, 2011, 06:39:04 pm »
U have to add the library '(WinAVR path)\avr\lib\avr5\libc.a' to the 'link libraries' under 'project->build options->linker settings' for ur code to compile. It's quite strange though that the include file doesn't link to the corresponding library.


How should an include file link to a library ?
As far as I know, this is not possible on gcc-based compilers.

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #4 on: August 03, 2011, 09:01:45 pm »
Sry that wasn't what I wanted to mean. What I tried to say was I'm surprised why the library itself doesn't care of this. I mean how can the programmer know which compiled library to add after including an include file. I think I'm missing something here. Linking the file manually shouldn't be the way although it works. How does GCC manage this? I tried adding the path to the library to the 'linker search directories' in the compiler settings with no luck. ;/

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9508
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #5 on: August 03, 2011, 09:48:05 pm »
I mean how can the programmer know which compiled library to add after including an include file. I think I'm missing something here.
Harhar... sorry to say that, but it's exactly the other way round. You as developer need to know the SDK you are responsible of using it  right. The documentation will tell what file to include and what lib to link against.

Why? Simple: Assume the following two libraries:
[LIB1:]
Code: [Select]
int my_calc(int i1, int i2)
{
  int i = i1-i2;
  return i;
}
[LIB2:]
Code: [Select]
int my_calc(int i1, int i2)
{
  int i = i1+i2;
  return i;
}
Both libs would provide the same function with the same interface. How on earth shall the compiler or linker know, which to link against? It's up to you and will always be up to you to setup your project right. Get used to it or you will sooner or later make serious mistakes.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #6 on: August 03, 2011, 11:46:20 pm »
I mean how can the programmer know which compiled library to add after including an include file. I think I'm missing something here.
Harhar... sorry to say that, but it's exactly the other way round. You as developer need to know the SDK you are responsible of using it  right. The documentation will tell what file to include and what lib to link against.

Why? Simple: Assume the following two libraries:
[LIB1:]
Code: [Select]
int my_calc(int i1, int i2)
{
  int i = i1-i2;
  return i;
}
[LIB2:]
Code: [Select]
int my_calc(int i1, int i2)
{
  int i = i1+i2;
  return i;
}
Both libs would provide the same function with the same interface. How on earth shall the compiler or linker know, which to link against? It's up to you and will always be up to you to setup your project right. Get used to it or you will sooner or later make serious mistakes.

I have no oppositions to that but that's not quite the case here.

With 'avrlib' (that's the library comes with avrgcc) the programmer needs to the define the mcu and include the 'io.h' file which includes the corresponding header file for the target mcu type according to the mcu definition. And in the case of 'eeprom.h', the eeprom read or write functions are also defined according to the defined mcu type. After all these heavy scripting I don't think it's intended to link the file manually which actually resides in a deeply nested folder. I still think I'm missing something here, I'll check the documentation and try some printf'ing for further investigation.

Offline Aelxx

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #7 on: August 03, 2011, 11:59:12 pm »
@zage2009
Hi, I use C::B during 3 years for AVR programming, it works fine. What you got is probably some problem with paths. Make sure you correctly added avr-gcc to C::B - go to C::B menu \Settings>Compiler and Debugger> in openen window select "GNU AVR GCC compiler", then select tab "Toolchain executables" and check if "Compiler's installation directory" is correct. Do NOT use "Auto-detect"!!!
BTW avr-gcc automatically links to libc.a if it's in the link path.

zage2009

  • Guest
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #8 on: August 04, 2011, 02:07:12 am »
U have to add the library '(WinAVR path)\avr\lib\avr5\libc.a' to the 'link libraries' under 'project->build options->linker settings' for ur code to compile. It's quite strange though that the include file doesn't link to the corresponding library.


Thank you,your method is correct and the program works well in  my atmega16,of course thanks a lot for other firends' discussions.Another question is  why the same program can be compiled well in avrstudio(an atmel avr IDE) and can't compiled sucessfully in C:B using the same gcc compiler(no especially mannual modification).I guess whether C:B doesn't do well in somewhere.

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #9 on: August 04, 2011, 03:21:41 pm »
BTW avr-gcc automatically links to libc.a if it's in the link path.
Can u explain this in detail pls, how do u manage that exactly?

Offline Aelxx

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #10 on: August 05, 2011, 08:44:56 am »
Don't know what details you expect. I do everything default way. No quirks and cludges here.
OK. What I done is:
1) Just installed WinAVR, AVR-Studio, CodeBlocks.
2) Configured GNU AVR GCC compiler in C::B 'compiler and debugger' menu
3) Created project and wrote a program
4) Compile it. Done.
Here is example of compilation output:
Quote
Running project pre-build steps
avr-gcc.exe -mmcu=atmega32 -O3 -Wall -std=gnu99 -fno-strict-aliasing -DF_CPU=14745600UL  -gdwarf-2  -W -Wall -std=gnu99   -I"C:\Program Files\Atmel\AVR Studio 5.0\extensions\Application\AVR Toolchain\avr\include"  -S main.c -o bin\Debug\main.s

-------------- Build: Debug in KIv05 pH ---------------

avr-gcc.exe -mmcu=atmega32 -O3 -Wall -std=gnu99 -fno-strict-aliasing -DF_CPU=14745600UL  -gdwarf-2  -W -Wall -std=gnu99   -I"C:\Program Files\Atmel\AVR Studio 5.0\extensions\Application\AVR Toolchain\avr\include"  -c main.c -o obj\Debug\main.o
avr-g++.exe  -o bin\Debug\KIv05_pH.elf obj\Debug\main.o   -mmcu=atmega32 -Wl,-Map=bin\Debug\KIv05_pH.elf.map,--cref  -lm -lm
Output size is 55,61 KB
Running project post-build steps
avr-objcopy -O ihex -R .eeprom -R .eesafe bin\Debug\KIv05_pH.elf bin\Debug\KIv05_pH.elf.hex
avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex bin\Debug\KIv05_pH.elf bin\Debug\KIv05_pH.elf.eep.hex
avr-size --mcu=atmega32 --format=avr bin\Debug\KIv05_pH.elf
AVR Memory Usage
----------------
Device: atmega32
Program:    9448 bytes (28.8% Full)
(.text + .data + .bootloader)
Data:        148 bytes (7.2% Full)
(.data + .bss + .noinit)
cmd /R "avr-objdump bin\Debug\KIv05_pH.elf -h -S > bin\Debug\KIv05_pH.elf.lss"
Process terminated with status 0 (0 minutes, 5 seconds)
0 errors, 0 warnings
Note no "-lc" in linking. There are two!! "-lm" but it's completely different story.
Maybe you have something wrong with WinAVR or Windows PATHs/Registry?
 a)Try reinstall WinAVR?
 b)If doesn't help - look at Windows PATH for avr-gcc. What it shows?

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #11 on: August 05, 2011, 11:11:34 am »
Can u pls try to compile the code in the first post and post the output here? I can't debug the problem without the code.

Offline Aelxx

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #12 on: August 06, 2011, 08:06:10 am »
OK. Test project added in archieve. All OK.
Build log:
Quote
-------------- Build: Debug in Test ---------------
avr-gcc.exe -Wall -mmcu=atmega16 -DF_CPU=16000000UL  -g  -W -Wall -std=gnu99   -I"C:\Program Files\Atmel\AVR Studio 5.0\extensions\Application\AVR Toolchain\avr\include"  -c main.c -o obj\Debug\main.o
avr-g++.exe  -o bin\Debug\Test.elf obj\Debug\main.o   -mmcu=atmega16 -Wl,-Map=bin\Debug\Test.elf.map,--cref 
Output size is 5,73 KB
Running project post-build steps
avr-size --mcu=atmega16 --format=avr bin\Debug\Test.elf
AVR Memory Usage
----------------
Device: atmega16
Program:     226 bytes (1.4% Full)
(.text + .data + .bootloader)
Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)
avr-objcopy -O ihex -R .eeprom -R .eesafe bin\Debug\Test.elf bin\Debug\Test.elf.hex
avr-objcopy --no-change-warnings -j .eeprom --change-section-lma .eeprom=0 -O ihex bin\Debug\Test.elf bin\Debug\Test.elf.eep.hex
cmd /c "avr-objdump -h -S bin\Debug\Test.elf > bin\Debug\Test.elf.lss"
Process terminated with status 0 (0 minutes, 3 seconds)
0 errors, 0 warnings
« Last Edit: August 06, 2011, 08:09:02 am by Aelxx »

Offline scarphin

  • Lives here!
  • ****
  • Posts: 644
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #13 on: August 06, 2011, 06:02:40 pm »
Ok I traced the problem, thnx for the help Aelxx. Unfortunately I don't have a solution and I don't know if that's related to CB or the compiler. The problem is the 'winavr_install_dir\avr\lib\libc.a' linker file. This file doesn't have the `__eewr_byte_m16' and '__eerd_byte_m16' functions to link but the linker tries to link to that file by default and can't find the corresponding functions. The correct file having those functions is 'winavr_install_dir\avr\lib\avr5\libc.a' and if I rename or delete 'winavr_install_dir\avr\lib\libc.a', the linker somehow finds the correct file ('winavr_install_dir\avr\lib\avr5\libc.a') and links to it without a problem.

I also tried with the 'AVR Toolchain v3.2.3' and had the same linker error. Deleting or renaming the 'libc.a' in the root folder gives a succesful build too. I'm confused here.

The problem doesn't seem to be CB specific but 'AVR Studio' can build the project correctly, I guess there is some setting to be done to CB.

Aelxx: Are u sure u don't have some extra options or paths in ur compiler or linker settings?


Offline Aelxx

  • Multiple posting newcomer
  • *
  • Posts: 10
Re: a problem compiling eeprom of avr(atmega16,winavr) program
« Reply #14 on: August 07, 2011, 10:16:26 am »
Why C::B links to wrong library? Don't see any specific options in my configuration changing which would change behaviour of avr-gcc not to link correctly, anyway added part of my C::B configuration file (thru cb_share_config).
Btw, if CodeBlocks does't link to correct lib, but AVR Studio links - have you tried to look at build log of both of them and find difference? Or better run it yourself from command line.