how to include a library

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

Hi guys

The other day, I downloaded a lcd1602 library which has lcd.h and lcd.c two files.

And when I try to include and use it, the AVR studio produces error when I include the lcd.h file.

but it works ok when I iinclude the lcd.c file, I thought all I need is to include the lcd.h file if I am using a library.

I have tried these these codes:

#include "lcd.h"

and it doesn't work

but this work:

#include "lcd.c"

and these happen to the other library I tried use as well.

What's wrong?

your help is appreciated.

Zhuhua

Zhuhua Wu - Electronic Engineering Student

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

Hello,

Are you sure that lcd.c is compiled ?
If you use WinAVR, you must explicitly add lcd.c to the list of your sources files in the Makefile.
Can you show us the error message you get and the content of your Makefile ? And can you tell us which compiler do you use on which platform ?

Judicael,

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

A pair of .c and .h files is not what i call a library, it's just a pair of two source files that need to get included (well, as you found out: only the .c file, since it will include the .h file itself) in your project's list of source files. A library on the other hand is a precompiled .a file that simply gets linked with the rest of your code.

Einstein was right: "Two things are unlimited: the universe and the human stupidity. But i'm not quite sure about the former..."

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

bug13avr wrote:

#include "lcd.h"

Correct.
Quote:

#include "lcd.c"

Wrong.

You have to compile lcd.c (into lcd.o) and link that with your application. This is often accomplished by adding lcd.c to the project or in a Makefile.

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

Quote:

and it doesn't work

You didn't actually say what "doesn't work" but I suspect you got link errors such as "undefined reference to LCD_init" and similar? This is because as well as the #include of the .h file you must add the .c file to the list of files to be compiled and linked.

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

Judicael wrote:
Hello,

Are you sure that lcd.c is compiled ?
If you use WinAVR, you must explicitly add lcd.c to the list of your sources files in the Makefile.
Can you show us the error message you get and the content of your Makefile ? And can you tell us which compiler do you use on which platform ?

I don't know I need to compiled it, I thought I just need to simply include the files.

So it looks like I am doing it the wrong way.

I am using AVR Studio 5.1, nothing else I think, but I have AVR Studio 4.81 in my computer as well.

the error message is attached

Attachment(s): 

Zhuhua Wu - Electronic Engineering Student

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

clawson wrote:
Quote:

and it doesn't work

You didn't actually say what "doesn't work" but I suspect you got link errors such as "undefined reference to LCD_init" and similar? This is because as well as the #include of the .h file you must add the .c file to the list of files to be compiled and linked.

Yes your are right, I got those message

Zhuhua Wu - Electronic Engineering Student

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

Thanks for all the responds, so I think I did it the wrong way, so how should it be done properly??

what should I google for? or better yet, a link to tutorial or some sort?

Thanks a lot

Zhuhua

Zhuhua Wu - Electronic Engineering Student

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

You have to add the *.c file as a project source file, in the pjt. tree. You can't just include the *.h file, you have to tell the IDE where to LOOK for it. So either put a copy of the *.h file in your pjt. directory or point the include path to where that file is on your PC. See my tutorial on including files if you don't know what I'm talking about.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

indianajones11 wrote:
See my tutorial on including files if you don't know what I'm talking about.

I still don't follow, can you provide a link to your tutorial on including files please.

My goal is to able to use name.h name.c properly in my code, is it all right to just include "name.c"? but I see all the other code they use include "name.h", I think they must do that for a reason, I just don't know why.

Thanks

Zhuhua

Zhuhua Wu - Electronic Engineering Student

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

Thanks for all your help, I got it sort it :)

Zhuhua Wu - Electronic Engineering Student

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

Quote:

My goal is to able to use name.h name.c properly in my code, is it all right to just include "name.c"? but I see all the other code they use include "name.h", I think they must do that for a reason, I just don't know why.

Well you can start by visiting the tutorial forum here and reading the article by abcminiuser about Managing Large Projects which shows you how things would be split into separate .c and .h files and then how the project is then built.

The bottom line is that you put the ADC/UART/LCD/whatever code into
.h and
.c such as lcd.h and lcd.c then you #include "lcd.h" in any files that make calls to the functions that are in lcd.c and you also add lcd.c to the list of all the files to be built in the project. When a main.c calls an LCD_init() kind of function then when you compile main.c to make main.o the compiler just inserts a "CALL LCD_Init-but-I-dont-know-the-address-yet" entry int he main.o file. Later, because lcd.c is also listed as one of the files to be built it will be compiled to create lcd.o which contains the code of LCD_init(). Finally a process called linking is performed in which all the .o file contents are joined together to make a single
.elf file. In doing this the linker sees LCD_init in lcd.o and als the as yet unresolved call to it from main.o and as it copies the code from main.o into project.elf it finally fills in the address it now knows LCD-init wil be at when it comes to that CALL LCD_init within main.o

Cliff

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

Thanks Cliff

I got it sorted by try and error after read all the other responds, but you explanation gave me a brief introduction on how and why, that's very helpful information to me.

I am sue this will help other newbies too.

Thanks again.

Zhuhua

clawson wrote:
Quote:

My goal is to able to use name.h name.c properly in my code, is it all right to just include "name.c"? but I see all the other code they use include "name.h", I think they must do that for a reason, I just don't know why.

Well you can start by visiting the tutorial forum here and reading the article by abcminiuser about "Managing Large Projects" which shows you how things would be split into separate .c and .h files and then how the project is then built.

The bottom line is that you put the ADC/UART/LCD/whatever code into
.h and
.c such as lcd.h and lcd.c then you #include "lcd.h" in any files that make calls to the functions that are in lcd.c and you also add lcd.c to the list of all the files to be built in the project. When a main.c calls an LCD_init() kind of function then when you compile main.c to make main.o the compiler just inserts a "CALL LCD_Init-but-I-dont-know-the-address-yet" entry int he main.o file. Later, because lcd.c is also listed as one of the files to be built it will be compiled to create lcd.o which contains the code of LCD_init(). Finally a process called linking is performed in which all the .o file contents are joined together to make a single
.elf file. In doing this the linker sees LCD_init in lcd.o and als the as yet unresolved call to it from main.o and as it copies the code from main.o into project.elf it finally fills in the address it now knows LCD-init wil be at when it comes to that CALL LCD_init within main.o

Cliff

Zhuhua Wu - Electronic Engineering Student