Code::Blocks

User forums => Embedded development => Topic started by: zage2009 on August 03, 2011, 02:39:22 pm

Title: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: zage2009 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: MortenMacFly 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: jens 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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. ;/
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: MortenMacFly 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: Aelxx 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: zage2009 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: Aelxx 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?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: Aelxx 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
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin 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?

Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: Aelxx 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.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 07, 2011, 01:10:24 pm
It's not CB that links to the wrong library, it's gcc that links to the 'libc.a' in the root folder (lib/) that doesn't have the corresponding functions. The correct one being in the 'lib/avr5/' folder has the corresponding functions and gcc links to it if it can't access to the one in the root (lib/). Or if u change the 'lib/libc.a' with the correct one 'lib/avr5/libc.a' everything goes fine with the linkage. It's quite clear that gcc looks for the 'libc.a' first in the root folder and finds first the wrong 'libc.a' version and stops linking. I'm guessing u have a setting somewhere that bypasses the root folder 'lib/' somehow. I don't think it's related to CB but I think it's a missing setting in CB.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: zage2009 on August 07, 2011, 02:50:03 pm
I really hope that the link problems of avr-gcc and other aspect's problems aren't too many.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: jens on August 07, 2011, 03:47:35 pm
Is it possible, that avr-gcc and mingw is mixed, both using the same root-folder ?
Just a wild guess.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 07, 2011, 04:53:41 pm
Worth a try, I'll check it out but don't think so.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 08, 2011, 12:55:23 pm
Aelxx: I checked ur settings file and couldn't find any specific setting regarding the issue. It also didn't build and gave the same linker error. Thnx for the help though.

Jens: I checked for an interference between my mingw and avr-gcc installations and found nothing. Thnx for the idea.

I ran out of ideas, I think I'll try 'avrfreaks' when I have time.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 11, 2011, 07:19:04 pm
After further tweaking I think I've found the reason. I had the '$winavr/avr/lib' folder in the 'search directories' for the linker. When this is removed, linker finds the correct 'libc.a' somehow and everything builds ok. Aelxx's configuration doesn't have this folder included that was the reason he didn't have linker problems. I'm not sure if it's CB or me added this folder in the 'linker search dirs' but if it is CB, it shouldn't do it anymore for the avr-gcc. Shoud I make a bug report about this?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: zage2009 on August 12, 2011, 02:47:59 am
I reinstall cb10.05 and WinAVR-20100110 in  windows xp and windows7 which are installed in vmware Workstation and I find that in winxp there still is the '$winavr/avr/lib' folder in the 'search directories' for the linker,but in win7  the 'search directories' for the linker is empty and everything builds ok.I guess that Aelxx probably uses CB and WinAVR in win7.Just the difference between the operating system causes the problem?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 12, 2011, 09:38:51 am
Did u also delete the CB settings in the 'C:\Documents and Settings\$user$\Application Data\codeblocks' folder when reinstalling on XP? Those are the settings CB uses when launched. But afaik CB adds the folders 'avr/include/' and 'avr/lib/'. From what it seems the latter shouldn't be added. The reason that these are not added on Win7 might be something else. Can any dev help us out here pls?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: Aelxx on August 12, 2011, 09:45:19 am
scarphin - Glad you found your problem's solution.
Zage2009 - No. I use WinXP SP3, C::B rev6454 or Later. I keep my old C::B configurations after reinstalling C::B. And also after reinstalling winAvr I always do manual compiler configuration in C::B, cause C::B doesn't autodetect avr-gcc (IMHO), maybe at this step I remove '$winavr/avr/lib' - can't say, cause I don't remember. Guess scarphin is right and it's some C::B's AVR-GCC compiler configuration bug, and I was lucky not to hit it  :D
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: jens on August 12, 2011, 09:50:33 am
scarphin - Glad you found your problem's solution.
Zage2009 - No. I use WinXP SP3, C::B rev6454 or Later. I keep my old C::B configurations after reinstalling C::B. And also after reinstalling winAvr I always do manual compiler configuration in C::B, cause C::B doesn't autodetect avr-gcc (IMHO), maybe at this step I remove '$winavr/avr/lib' - can't say, cause I don't remember. Guess scarphin is right and it's some C::B's AVR-GCC compiler configuration bug, and I was lucky not to hit it  :D

Thes settings are made if you use autodetection, otherwise they stay empty.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 12, 2011, 10:01:45 am
Well we are all lucky one of us hit that bug. ;) Can someone pls try building on linux and see if 'avr/lib' should be there in the search path or not, possibly a dev pls?
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: zage2009 on August 12, 2011, 12:26:57 pm
scarphin - Glad you found your problem's solution.
Zage2009 - No. I use WinXP SP3, C::B rev6454 or Later. I keep my old C::B configurations after reinstalling C::B. And also after reinstalling winAvr I always do manual compiler configuration in C::B, cause C::B doesn't autodetect avr-gcc (IMHO), maybe at this step I remove '$winavr/avr/lib' - can't say, cause I don't remember. Guess scarphin is right and it's some C::B's AVR-GCC compiler configuration bug, and I was lucky not to hit it  :D

Thes settings are made if you use autodetection, otherwise they stay empty.
I reinstall the win7 in vmware Workstation and install the WinAVR,CB10.05(rev6283) in Win7.The result is that the option 'winavr/avr/lib' is still in the Linker tab of the Search directions and perhaps there still is the same problem in different operation system.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 12, 2011, 01:45:28 pm
It's sure CB adds 'avr/lib' folder automatically to the linker search path. The problem is if it should do this or not. Can someone test under linux and see if the problem also exits on linux or not pls? So the devs can remove it in the next nightly if adding 'avr/lib' to the search path generates this linker problem also under linux.
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: jens on August 12, 2011, 01:54:18 pm
It's sure CB adds 'avr/lib' folder automatically to the linker search path. The problem is if it should do this or not. Can someone test under linux and see if the problem also exits on linux or not pls? So the devs can remove it in the next nightly if adding 'avr/lib' to the search path generates this linker problem also under linux.


There is another thread about a similar problem on linux.
Here is one of my answers:
http://forums.codeblocks.org/index.php/topic,15118.msg101248.html#msg101248 (http://forums.codeblocks.org/index.php/topic,15118.msg101248.html#msg101248)
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: scarphin on August 12, 2011, 02:34:07 pm
Yes I'm aware of that thread. Correct me if I'm wrong but the situation there is the inclusion of the GCC libs not the avr-gcc specific libs. As from what I understand someone needs to make sure the problem still exists or not if avr-gcc specific linker libs 'avr/lib' not 'usr/lib' is included. Sry for the trouble if these 2 cases are the same or both libs are installed in the same folder under linux. It's been long since I used linux, just trying to help. ;)
Title: Re: a problem compiling eeprom of avr(atmega16,winavr) program
Post by: jens on August 12, 2011, 02:57:21 pm
The problem is similar, because it is there because C::B adds search-dirs, which seem to be wrong (in some versions?).
Normally (or in most cases?) a compiler should know where it's includes and libs are, as long as they are in the default-folders relative to the executable dir.