CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Modular Programming: #include .h file

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 4:49 pm     Reply with quote

Consider the following:

In the main file is, among others, included:

#include "test.h"

main()
{
while(TRUE)
{
// code here
// also calls a function declared in test.h
}
}


The test.h file contains a function declaration:
void sample(int, int);

And its implementation is contained in the test.c file:
#include "test.h"
void sample(int x, int y)
{
//code here
}


The compiler generates an error with the following message:
Function used but not defined: sample

Am I missing something or is this a compiler limitation?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517468
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 5:30 pm     Reply with quote

:=The test.h file contains a function declaration:
:= void sample(int, int);
:=
:=And its implementation is contained in the test.c file:
:= #include "test.h"
:= void sample(int x, int y)
:= {
:= //code here
:= }
:=
:=The compiler generates an error with the following message:
:=Function used but not defined: sample
:=
:=Am I missing something or is this a compiler limitation?
----------------------------------------------------------

With CCS, the function prototype must be exactly the same as
the declaration. (including the variable names).
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517469
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 5:32 pm     Reply with quote

You have to include the test.c file in your main file. You can include the test.h in the test.c file. You should use some #ifdef to prevent the header files from being included more than once.


:=Consider the following:
:=
:=In the main file is, among others, included:
:=
:=#include "test.h"
:=
:=main()
:={
:= while(TRUE)
:= {
:= // code here
:= // also calls a function declared in test.h
:= }
:=}
:=
:=
:=The test.h file contains a function declaration:
:= void sample(int, int);
:=
:=And its implementation is contained in the test.c file:
:= #include "test.h"
:= void sample(int x, int y)
:= {
:= //code here
:= }
:=
:=
:=The compiler generates an error with the following message:
:=Function used but not defined: sample
:=
:=Am I missing something or is this a compiler limitation?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517470
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 8:20 pm     Reply with quote

I have included the #ifndef but just didn't show it.

Not being able to #include the test.h only in the main file goes against true c modular programming. Jut including the test.h file should have sufficed. So, I'm surprised it doesn't work here.

:=You have to include the test.c file in your main file. You can include the test.h in the test.c file. You should use some #ifdef to prevent the header files from being included more than once.
:=
:=
:=:=Consider the following:
:=:=
:=:=In the main file is, among others, included:
:=:=
:=:=#include "test.h"
:=:=
:=:=main()
:=:={
:=:= while(TRUE)
:=:= {
:=:= // code here
:=:= // also calls a function declared in test.h
:=:= }
:=:=}
:=:=
:=:=
:=:=The test.h file contains a function declaration:
:=:= void sample(int, int);
:=:=
:=:=And its implementation is contained in the test.c file:
:=:= #include "test.h"
:=:= void sample(int x, int y)
:=:= {
:=:= //code here
:=:= }
:=:=
:=:=
:=:=The compiler generates an error with the following message:
:=:=Function used but not defined: sample
:=:=
:=:=Am I missing something or is this a compiler limitation?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517473
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 8:44 pm     Reply with quote

:=:=The test.h file contains a function declaration:
:=:= void sample(int, int);
:=:=
:=:=And its implementation is contained in the test.c file:
:=:= #include "test.h"
:=:= void sample(int x, int y)
:=:= {
:=:= //code here
:=:= }
:=:=
:=:=The compiler generates an error with the following message:
:=:=Function used but not defined: sample
:=:=
:=:=Am I missing something or is this a compiler limitation?
:=----------------------------------------------------------
:=
:=With CCS, the function prototype must be exactly the same as
:=the declaration. (including the variable names).

PCM Programmer, I'll try having the variable names in the prototype as well. This really surprises me that the declaration must also have variable names. As we all know in normal Cc just the type in the prototype should suffice.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517474
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Msg for PCM Programmer: Modular Programming: #include .h fil
PostPosted: Tue Sep 02, 2003 9:35 pm     Reply with quote

:=:=The test.h file contains a function declaration:
:=:= void sample(int, int);
:=:=
:=:=And its implementation is contained in the test.c file:
:=:= #include "test.h"
:=:= void sample(int x, int y)
:=:= {
:=:= //code here
:=:= }
:=:=
:=:=The compiler generates an error with the following message:
:=:=Function used but not defined: sample
:=:=
:=:=Am I missing something or is this a compiler limitation?
:=----------------------------------------------------------
:=
:=With CCS, the function prototype must be exactly the same as
:=the declaration. (including the variable names).


I tried having the function prototype with variable names in the .h file, and the definition in the .c file

In the .c file I #include .h and the function parameters are exactly the same.

In the main program I only #include .h and the compiler still generates the same error message.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517477
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 10:03 pm     Reply with quote

There is only one source file for the CCS compiler. It does not compile modules and then link them together. You have to use a slightly different approach.

:=I have included the #ifndef but just didn't show it.
:=
:=Not being able to #include the test.h only in the main file goes against true c modular programming. Jut including the test.h file should have sufficed. So, I'm surprised it doesn't work here.
:=
:=:=You have to include the test.c file in your main file. You can include the test.h in the test.c file. You should use some #ifdef to prevent the header files from being included more than once.
:=:=
:=:=
:=:=:=Consider the following:
:=:=:=
:=:=:=In the main file is, among others, included:
:=:=:=
:=:=:=#include "test.h"
:=:=:=
:=:=:=main()
:=:=:={
:=:=:= while(TRUE)
:=:=:= {
:=:=:= // code here
:=:=:= // also calls a function declared in test.h
:=:=:= }
:=:=:=}
:=:=:=
:=:=:=
:=:=:=The test.h file contains a function declaration:
:=:=:= void sample(int, int);
:=:=:=
:=:=:=And its implementation is contained in the test.c file:
:=:=:= #include "test.h"
:=:=:= void sample(int x, int y)
:=:=:= {
:=:=:= //code here
:=:=:= }
:=:=:=
:=:=:=
:=:=:=The compiler generates an error with the following message:
:=:=:=Function used but not defined: sample
:=:=:=
:=:=:=Am I missing something or is this a compiler limitation?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517478
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 10:30 pm     Reply with quote

Try the following test program. It compiled without errors
with PCM vs. 3.173, and MPLAB vs. 5.70.20. If it doesn't
work for you, then post your version of the compiler.


The file TEST.H contains only this line:
<PRE>
void sample(int x, int y);
</PRE>


The file TEST.C contains this code:
<PRE>
#include <16F877.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use Delay(clock=8000000)
<BR>
#include "test.h"
<BR>
//========================================================
void main()
{
int a;
int b;
<BR>
a = 0x55;
b = 0xAA;
<BR>
sample(a, b);
<BR>
while(1);
}
<BR>
//------------------------------
<BR>
void sample(int x, int y)
{
int c;
<BR>
c = x + y;
<BR>
}
</PRE>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517481
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Msg Again for PCM Programmer: Modular Programming: #include
PostPosted: Tue Sep 02, 2003 11:31 pm     Reply with quote

Am I correct in my assumption here that the program contains only 2 files, namely, TEST.H and TEST.C. The latter file has the main() function.

If this is correct then I can see why it works. The function definition is written below (and outside) the main().
The function prototype (contained in the TEST.H) is above so the compiler sees it before the main().

The scenario I'm trying to figure out is having 3 files contained in the same directory. A file, let's call it main1.c contains the main(), that #includeS TEST.H. This TEST.H is #includeD in the TEST.C.

I want the TEST.H file only to contain prototypes and all its definitions in a separate file (not in the main file). This makes the main file more "readable". We know that all C textbooks will tell us this is possible. You can even make the variables public by using the word extern.

But somehow the CCS compiler doesn't like it written this way. I'm using CCS C ver. 3.173




/*****************MAIN.C**************************/
#include <16F877.h>

//and so on...

#include "TEST.H" //contained in the same directory
//This is also #includeD in the TEST.C

main()
{
while(1)
{
//code here that also calls the sample() function
}
}
/*****************END OF MAIN.C*********************/




/***************TEST.H******************************/
#ifndef TEST_H
#define TEST_H

void sample( int x, int y );

#endif

/***************END OF TEST.H***********************/



/***************TEST.C*****************************/
#include "TEST.H"

void sample(int x, int y)
{
//handle x and y
}

/***************END OF TEST.C*********************/

-------------------
:=Try the following test program. It compiled without errors
:=with PCM vs. 3.173, and MPLAB vs. 5.70.20. If it doesn't
:=work for you, then post your version of the compiler.
:=
:=
:=The file TEST.H contains only this line:
:=<PRE>
:=void sample(int x, int y);
:=</PRE>
:=
:=
:=The file TEST.C contains this code:
:=<PRE>
:=#include <16F877.h>
:=#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
:=#use Delay(clock=8000000)
:=<BR>
:=#include "test.h"
:=<BR>
:=//========================================================
:=void main()
:={
:=int a;
:=int b;
:=<BR>
:=a = 0x55;
:=b = 0xAA;
:=<BR>
:=sample(a, b);
:=<BR>
:=while(1);
:=}
:=<BR>
:=//------------------------------
:=<BR>
:=void sample(int x, int y)
:={
:=int c;
:=<BR>
:=c = x + y;
:=<BR>
:=}
:=</PRE>
:=
:=
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517486
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Tue Sep 02, 2003 11:59 pm     Reply with quote

<font face="Courier New" size=-1>I'm assuming that you want to call the function within main().
Somehow, main() needs to know that the function exists
before you call it. You can do this by putting the function
above main(), which is what CCS does in all their examples.
Or, you can put the function below main(), and put a function
prototype above main().

So, if your function prototype is in test.h, and your function
is in test.c, then your main file should look like this:

<PRE>
#include "test.h" // This file contains the function prototype
<BR>
main()
{
<BR>
sample(1, 2); // Call the function.
<BR>
}
<BR>
#include "test.c" // This file contains the function.
</PRE>

I do all my large programs like this, and it works very well.


Note: I have put the filenames in quotes because the board
wasn't displaying them if I used angle brackets.</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517490
arga



Joined: 09 Sep 2003
Posts: 22

View user's profile Send private message

Re: Modular Programming: #include .h file
PostPosted: Wed Sep 03, 2003 4:24 am     Reply with quote

:=<font face="Courier New" size=-1>I'm assuming that you want to call the function within main().
:=Somehow, main() needs to know that the function exists
:=before you call it. You can do this by putting the function
:=above main(), which is what CCS does in all their examples.
:=Or, you can put the function below main(), and put a function
:=prototype above main().
:=
:=So, if your function prototype is in test.h, and your function
:=is in test.c, then your main file should look like this:
:=
:=<PRE>
:=#include "test.h" // This file contains the function prototype
:=<BR>
:=main()
:={
:=<BR>
:=sample(1, 2); // Call the function.
:=<BR>
:=}
:=<BR>
:=#include "test.c" // This file contains the function.
:=</PRE>
:=
:=I do all my large programs like this, and it works very well.
:=
:=
:=Note: I have put the filenames in quotes because the board
:=wasn't displaying them if I used angle brackets.</font>

Thanks. I do the same, too. And I also looked at CCS' examples and noticed the same, too. I pointed this out to my Visual C++ programmer colleagues and we thought that it is a compiler limitation. But, I wanted to make my programs more transparent. Obviously, this is a limitation.

As I do visual programming as well (in C++ and this is the main focus of the company), the way we do modular programming is in line with normal programming techniques. However, I'm new to embedded C and have only previously done it in Assembly. Hence, I post quite often.

I don't see any advantage of having prototypes in a .h file only to define it in the same file where the main() is defined.

Thanks, PCM Programmer, for all your input.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517495
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

CCS Compiler header file
PostPosted: Mon Sep 08, 2003 10:24 am     Reply with quote

Hello,

Is it possible to just declare the header file in the main file just as we do in any other compiler, or is it neccessary to add the ifndef statements.

thanks
arun
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Mon Sep 08, 2003 10:41 am     Reply with quote

In my opinion you should always have the

#ifndef SOMEFILE_H
#define SOMEFILE_H
.
.
.
#endif

for every header file. It is just good coding practice.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group