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 CCS Technical Support

Global variables with multiple compilation units
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
richard_m



Joined: 28 May 2009
Posts: 3

View user's profile Send private message

Global variables with multiple compilation units
PostPosted: Thu May 28, 2009 4:14 am     Reply with quote

I'm not sure if I'm doing this wrong or it's a bug, but I can't get global variables to work. They seem to be addresses as if packed in the wrong order in the file where they aren't defined, even though the declaration is in the same order. Surely the linker should fill in the memory addresses in both files the same way after relocating them if it moves them to align the 16 bit values for better packing?

Compiler is PCWHDD 4.069
Target is PIC 18f4680

Project Options:
Source files : main.o, sub.o
Multiple Compilation Units is ticked.
Link Separately is ticked.

header.h:
Code:

#include <18F4680.h>
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#use delay(clock=32000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

extern int8 a ;
extern int16 b ;
extern int8 c ;
extern int16 d ;

void subproc();


main.c:
Code:

#include "header.h"

int8 a = 0x01;
int16 b = 0x0203;
int8 c = 0x04;
int16 d = 0x0506;

void main() {
   delay_ms(500);     // let clock stabilise               
   
   // prints "a=01 b=0203 c=04 d=0506"
   printf("a=%x b=%x%x c=%x d=%x%x \r\n",
      a, make8(b, 1) , make8(b, 0),
      c, make8(d, 1) , make8(d, 0));
      
   subproc();      
   delay_ms(500);   // wait for buffer to empty
}



sub.c:
Code:

#include "header.h"

void subproc() {

   // prints "a=05 b=0503 c=57 d=0000"
   printf("a=%x b=%x%x c=%x d=%x%x \r\n",
      a, make8(b, 1) , make8(b, 0),
      c, make8(d, 1) , make8(d, 0));   
}
Ttelmah
Guest







PostPosted: Thu May 28, 2009 4:53 am     Reply with quote

The problem is including 'header', in the first unit.

Adding the extern declaration to the first unit, tells the compiler to consider these variables as declared in another unit. That it gets confused then, is not surprising....

Best Wishes
richard_m



Joined: 28 May 2009
Posts: 3

View user's profile Send private message

PostPosted: Thu May 28, 2009 5:22 am     Reply with quote

Ok, fair enough (although I've seen it done that way with other compilers), now I've separated out the externs into a separate header file that's only included by sub.c. But still the same :(

header.h
Code:
#include <18F4680.h>
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#use delay(clock=32000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

void subproc();


globals.h:
Code:
extern int8 a ;
extern int16 b ;
extern int8 c ;
extern int16 d ;


sub.c:
Code:
#include "header.h"
#include "globals.h"

void subproc() {

   // prints "a=05 b=0503 c=57 d=0000"
   printf("a=%x b=%x%x c=%x d=%x%x \r\n",
      a, make8(b, 1) , make8(b, 0),
      c, make8(d, 1) , make8(d, 0));   
}


main.c:
Code:
#include "header.h"

int8 a = 0x01;
int16 b = 0x0203;
int8 c = 0x04;
int16 d = 0x0506;

void main() {

   delay_ms(500);     // let clock stabilise               
   
   // prints "a=01 b=0203 c=04 d=0506"
   printf("a=%x b=%x%x c=%x d=%x%x \r\n",
      a, make8(b, 1) , make8(b, 0),
      c, make8(d, 1) , make8(d, 0));
      
   subproc();      
   delay_ms(500);   // wait for buffer to empty
}
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Fri May 29, 2009 7:25 am     Reply with quote

Are you using MPLAB IDE? You have to include only main.c in the Source Files section (Project window). In CCS compiler, you have to concentrate everything in the main source code file. I wouldn't use the external declarations, I would define all globals directly in main.c and after that include the file sub.c. I would suggest you also to include header.h only once in your project, it could be also a source of compiling errors.

Regards,
Adriano.
richard_m



Joined: 28 May 2009
Posts: 3

View user's profile Send private message

PostPosted: Thu Jun 04, 2009 2:11 am     Reply with quote

I'm using CCS PCWHD IDE version 4.069.

It's all very well sticking everything in the one file if you're dealing with a very simple, hobbyist type of project, but the code I'm working with is much more complicated than that. I appreciate the suggestions of ways to work around the bugs, but I really feel they ought to get fixed :)

The CCS compiler has the option of using multiple compilation units, so they *should* work. It does have a tendancy to do weird things like #include .c files, and because of that I have problems, but I can work them out if the compiler would at least generate valid code.

But it seems that the normal way of writing code, ie separately compiled .c files then linking the .o files together is not well used by the average user of this compiler as they are mostly beginners trying to do simple stuff. So CCS doesn't notice the bugs.. I will get onto them now I know I am not trying to do something non-standards compliant.

Thanks, it helps to know why I am having this problem :)
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 6:27 am     Reply with quote

I also have multiple-unit projects and I have to organize my projects differently from the standard C because of the particularities of CCS compiler. I have indeed to #include .c files in the main project file. It works, although it's not standard. I don't believe that CCS is going to change its compiler to be standard compliant now that it is on version 4...

Good luck with your project,
Adriano.
Guest








Is there any chance to get a good example
PostPosted: Wed Jun 17, 2009 1:43 am     Reply with quote

I'm coming from the standard C world. I'm really frustrated because CCS seems not to support standard multiple file linking.

But I need to do this. I always get crazy things (without any warning).

Does anyone have a good example to see how to deal with more than four files? The sample CCS gives, is a little rare. It can't be seen how this really should be done.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jun 17, 2009 4:39 am     Reply with quote

I didn't yet test the "multiple compilation units" feature. If the term isn't completely meaningless respectively misleading, it would designate a technique, where the other source files are not included into main.c, which is the standard CCS single compilation unit method. Also, I would expect extern variables to work with.

Otherwise it can be simply added to the list of anounced and currently unsupported CCS C features (as e.g. fixed point types).
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Wed Jun 17, 2009 11:40 am     Reply with quote

Hi Guest,

I don't believe anybody is going to give you an example, because multiple-unit projects are usually complex and they involve company privacy. I would suggest that you try to do it yourself and ask us or CCS support if you get into trouble.

I checked at CCS site, they should support multiple unit compilation, although I have never tested it. Check this link:

http://www.ccsinfo.com/content.php?page=linker

Regards,
Adriano.
Guest








Is there any chance to get a good example
PostPosted: Thu Jun 18, 2009 2:45 pm     Reply with quote

Hi Adriano,

Thanks for your reply.

I did know what CCS wrote. Because of this I did buy the CCS IDE(4.xx).

The point is, CCS promises that you are able to link multiple files together. But if you did use extern vars strange things will happen. I tried to do it like it was at the CCS sample. But it did only work for two modules. The third was not able to reach the extern var. For the compiler it was a totally new var.

Now I gave up (totally frustrated). I think they shouldn't promise things which really do not work. If it worked they should give proper examples to see how a good pic style CCS code should being done.

I now do the same I did in the past with CCS --> single code style.

Thanks. Maybe some other users will find a solution. I'm out of my time to look for it.

Thanks

Otto
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jun 18, 2009 3:50 pm     Reply with quote

I tried to compile the example provided by richard_m, but it failed with
Function used but not defined for subproc() in main.c.

I understand the said description of multiple compilations units in the way, that the unit sub.c would be imported automatically.
apcaye



Joined: 22 May 2009
Posts: 29
Location: Brazil

View user's profile Send private message

PostPosted: Fri Jun 19, 2009 6:53 am     Reply with quote

You're welcome, Otto. In case no other user helps you, I would recommend you send an e-mail to CCS technical support. They usually answer quickly, in the same day when possible. You can attach your project files for them to analyse.

Best regards,
Adriano.
gilavar



Joined: 03 Mar 2009
Posts: 24

View user's profile Send private message

PostPosted: Fri Jun 19, 2009 10:59 am     Reply with quote

I am not very good at C at all, but I think you should declare variables in one unit and then use extern on every unit you are trying to access it from. It looks like in your code it is other way around.
[email protected]
Guest







Re: Is there any chance to get a good example
PostPosted: Sun Aug 16, 2009 3:37 am     Reply with quote

Facing similar problems. I am dealing with C for the past 12 yrs. never found a compiler except CCS, where extern variable doesn't work in the manner it's meant for. Getting frustrated.

Anonymous wrote:
Hi Adriano,

Thanks for your reply.

I did know what CCS wrote. Because of this I did buy the CCS IDE(4.xx).

The point is, CCS promises that you are able to link multiple files together. But if you did use extern vars strange things will happen. I tried to do it like it was at the CCS sample. But it did only work for two modules. The third was not able to reach the extern var. For the compiler it was a totally new var.

Now I gave up (totally frustrated). I think they shouldn't promise things which really do not work. If it worked they should give proper examples to see how a good pic style CCS code should being done.

I now do the same I did in the past with CCS --> single code style.

Thanks. Maybe some other users will find a solution. I'm out of my time to look for it.

Thanks

Otto
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Aug 16, 2009 9:17 pm     Reply with quote

I was able to successfully compile and link the program doing the
following:

I located the file mcu.zip in the PICC Examples folder and
extracted the file MCU Documentaiton.PDF.

I renamed your header.h file to header.c (a quirk in the compiler made it
want to look for a .c file when I built main.c)

I added the following lines in sub.c
extern int8 a;
extern int16 b;
extern int8 c;
extern int16 d;

I removed the variable and subproc() definitions from the header file and
left the primary definitions in main.c.

I modified the subproc() definition in main.c to external:
extern void subproc();

Using page 3 of the PDF I individually compiled each of the modules
then used Build from the Compile menu to link the files to a .hex file.
(make to check Multiple Compilation Units on the project Options screen)

The program linked successfully and created the .hex file.
_________________
Google and Forum Search are some of your best tools!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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