Problems with compilation/linking

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

Hi Freaks!

I've decided I've got better to do than play with
AVR peripheral registers directly, so I just downloaded a copy of AVRLIB so that I can use the peripheral handling functions.

I installed AVRLIB into this directory tree:

--AVRLIB
|
Atmel----WinAVR
|
--KamAVR
|
--Projects----MyProject

I have tried to start with the I2C libraries. I copied global.h and i2cconf.h to the MyProject directory where I have my C source code.

If I try to compile with the following includes

//LIBC stuff
#include 
#include 
#include 
#include 
//AVRLIB stuff [Project Specific stuff]
#include "global.h"
#include "i2cconf.h"
#include "../../AVRlib/i2c.h"

I get the messages "global.h: no such file or directory" and "i2cconf.h: no such file or directory" as errors occurring in i2c.h.

On the other hand, if I copy i2c.h into my project directory and change to includes to

//LIBC stuff
#include 
#include 
#include 
#include 
//AVRLIB stuff [Project Specific stuff]
#include "global.h"
#include "i2cconf.h"
#include "i2c.h"

I get the messages "undefined reference to XXXX" where XXXX is the name of any library function I use in my code.

How can I solve this? Did I do womething wrong while installing AVRLIB? Am I doing the includes in the wrong way?

Thanks for any help!

nxp

[suffering from includophobia]

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

Maybe consider using -I on the CFLAGS to instruct the compiler to look in other directories for the .h files?

Cliff

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

nxp wrote:
I've decided I've got better to do than play with AVR peripheral registers directly,

So, instead, you're playing around to get this Library working!

Perhaps the registers weren't so bad, after all... :wink:

Quote:
I get the messages "undefined reference to XXXX" where XXXX is the name of any library function I use in my code.

Sounds like you have included just the haeder files, but not linked the actual Libraries themselves?

Remember, a header file contains no code - it just contains promises to the compiler that certain functions will be provided "elsewhere".
The compiler takes your word for this, and generates code that needs these functions to be provided.

You fulfil the promises by providing both the compiler output and the Library files to the Linker.

If the Library files are missing, the Linker will complain saying, "undefined reference to XXXX"

In other words, it is saying, "You've used XXXX - you said you'd provide a definition for it, but you haven't!"

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, it seems I was doing a newbie's mistake. I had been under the impression that including the h-file was enough to tell the compiler which functions I needed to use. I beleived that the installation for AVRLIB would have provided some form of library table to GCC and then the header just tell it which files to choose. Seems I was wrong.

Does such a system like the one I assumed exist anywhere? Does GCC provide any way to code something like that?

Including both the .h and the .c files in my project allows it to compile.

Just one last thing - I2C.c needs the rprintf functions, and as far as I can see they are only message and error outputs describing what's going on at a low level. If I dont think I need them, can I comment them out and not include the printf and uart functions?

nxp
[to include or not to include?]

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

nxp wrote:
I had been under the impression that including the h-file was enough to tell the compiler which functions I needed to use.

Yes, including the h files does tell the compiler - but that's all it does. There also needs to be something that actually provides the "bodies" of those functions.
See previous reply.

Quote:
I beleived that the installation for AVRLIB would have provided some form of library table to GCC and then the header just tell it which files to choose.

Usually a "Library" does mean a compiled binary object that you just have to link to your project.

However, it seems that this particular library is a source library - ie, just a collection of source files that you have to compile for yourself.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...