Problem with header files

A catchall for PSoC5 discussions not captured by the other forums.

Moderator: ericb

Re: Problem with header files

Postby bobmarlowe on Thu Apr 05, 2012 12:23 am

What is rather unusual as I see and what will lead to this unusual-behaveour is:
In the header-files you declare your variables.
Header-files are to define macros and function prototypes(thus the name HEADER), but NOT to do anything that will allocate a single byte, be it for a variable or a function.
The error(s) you face are related to linking and have nothing to do at first with IDE-Problems (You might as well use different IDEs, they are supported (µVision, Eclypse).
You may blame the linker for PSoC5 as well, because it does not throw the errrors the PSoC3 does, but you will never get your code working regardless of what CPU or IDE you use, the basic problem lies within your coding.

Sorry for that.
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Sun Apr 08, 2012 9:47 am

Hi Bob,

thanks for your last e-mail. You are correct that the problem lies in my coding.
I am looking into this...

I believe that probably the IDE is not the component to blame. However I still blame
the IDE because it compiled the code correctly as PSOC5 and only gave the errors
when compiled as PSOC3... Lucky for me you gave the work-around so I can focus on
the real problems in my code now!

Thanks!

Heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Sun Apr 08, 2012 10:24 am

Great! I was afraid, you would give up.
I would suggest:
Copy the variable-declarations out of the .h-files and put them into the C-modules where they are needed and where they belong to.
Change the declaration in the .h-files to "extern". This will allow to #include the .h-files in other modules without allocating storage.
Usually every C-file (exept main.c) has its own .h-file which can make the number of files a bit confusingly. And (as I saw from your code) do not include, what is not really needed.

Ah, yes, another point: in C an expression to be false means it is 0 (zero), so for heavens sake write

#define FALSE 0
#define TRUE !FALSE

and you may define (I really like it)

#define forever 1

and use it in a loop

while (forever) ....


Happy coding
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 7:14 am

Hi Bob,

In which header file does one normally include the extern variable declarations?
For instance I have a function for an LFO called LFO.c so my intuitive guess would
be to include all the relevant LFO variables in its header file. But the thing is that
the variables are not intialized in LFO.c but in another function called Intializer.c

Heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 7:57 am

The extern declaration of a variable can be repeated in multiple different .h files. I think it is even possible (without error or even warning) to have a header-file with an extern definition referenced by the C-file where the variable is declared (try it out).

In my opinion it is better readable (and understandable) when every module has its own modulename_Start() function which initializes the required vars and may initialize some associated hardware as well. A structure like that makes it easier to use a module in a completely different context (project) - it has its own .h-file and does all the nessecary initialization. This sort of isolating is namede "Encapsulation" and is inherent to C++. Here every "Object" is a container for its data AND code (and initialization of course). Since C++ is a bit big for an embedded system like a PSoC, we (the designer) should try to keep as close to this principle as possibe.
To keep a long speech short: Put the vars into the Modules where they belong to, initialize all those vars in an initializer-function as part of the module, put all that MUST be visible to other modules into the .h-file.

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 8:48 am

I am working my way through all the errors but some of the comments I get are too cryptic:
Missing ";" before extern. What does this mean??

This is my code and all seems okay. Also there is no ";" missing...

/* DEFINES*/
#define TRUE !FALSE
#define FALSE 0
#define ON !OFF
#define OFF 0
#define YES !NO
#define NO 0
#define NOSTATUS 0

/* INCLUDES */
#include <project.h>

#ifndef __Initializer_h
#define __Initializer_h

/* EXTERNAL VARIABLE DECLARATIONS */
extern uint8 ActiveSenseState;
extern uint8 SineTable[];

#endif

Heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 9:08 am

Probably uint8 is not yet defined?
It would be rather helpful, when you tell at least in which line the error occurs (second extern?) :mrgreen:
A complete file or even the project would be appreciated as well.

§ 1: The compiler is always right
§ 2: Should it be that §1 happens to be wrong, automatically §1 drops in. :roll:


I've always hated these C-compilers for their rather cryptic, sometimes mis-interpretable error-messages.

Bob

PS: By the way: Why not putting the sine-table into program-memory. It is rather doubtfull that the values will ever change during the lifetime of the project and it would save some (precious) RAM.

Plse excuse my typos, but that cat will not take its paws off the kzboard.
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 9:16 am

Hi Bob,

The error occurs for both lines containing the extern modifier. If I deactivate (//) the first line,
the error repeats itself for the second line...

I thought that uint8 was defined in project.h which is indirectly called by device.h. Do you know
how I can fix this?

heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 9:48 am

Defined in CyTypes.h
Find->all Project files #typedef

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 10:31 am

Hi Bob,

I found the mistake. A ; was missing in one of my headers. So you were right about the first law of compilers ;)

Another thing:
If I include a header file with extern declarations of variables I still need to declare the variables inside the function
itself. Is this normal and am I making a huge newbie mistake?

Heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 10:53 am

I just remembered the first time I had written a C program consisting of several modules. After my experiences with header files (not quite difficult to yours) I tread C into the basket and switched back to PASCAL again.

You'll have to learn the vocables: inside a function means (always) between the function header (void MyFunc(void) ) and the closing "}"
Inside a MODULE is out of any function within the C-File. I suppose you mean the latter, because a declared local var (inside a function) cannot be declared as external.

So, putting it all together:

in the header-file(s) (yes, you may declare it more than once!) you have
extern uint8 MyVar;

Within your module (This may appear only ONCE!!!)

uint8 Myvar;

That's it

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 10:54 am

Yes, I see, it's "MyVar"
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 11:19 am

Hi Bob,

thanks for the answer!

I have removed all references to my own header files and gotten the code error free. I will see if I get the code working
now and later move some variables into header files again. I have to say that the whole story would have been a lot easier
if somehow I was able to find the translation units (the code after preprocessing)...

Heiko
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 11:41 am

What is the reason that you are willing against all odds to put again vars into the header files?
Kind of resistence against suggestions?? DON'T !!!

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: Problem with header files

Postby hjvanderlinden on Tue Apr 10, 2012 11:54 am

Hi Bob,

I don't want to use header files anymore but I can see how they somehow fit into a good programming practice.
For the moment I am not going to focus on headers files...

I took the modules from the PSOC3 project to the PSOC5 project and am getting loads of new errors which
make no sense at all. I copy-pasted the files from the PSOC3 project to the PSOC5 project and found out that
I needed to replace all the copied files because of some weird bug in the IDE. But here's the kicker after replacing
all files and copying the correct code in them the IDE starts complaining that the first file in the list contains all
kinds of non-existent errors again.

I think I am going to move away from Cypress products after all...

For the people at Cypress: I really hope you have to use your own IDE so you experience first hand how much you make your
customers suffer! I don't care anymore if the compiler is correct or if the IDE is correct. It's taking too much time to develop a
product on this crappy IDE and I find it unforgivable that I need to recompile the project as a PSOC 3 project (while
it is clearly a PSOC5 project) to discover all kinds or errors the IDE should have found in the first place. And then ending
up discovering that the corrected files wont work anymore in the PSOC5 environment... I cannot imagine that this is correct
behavior of an IDE and I really don't like these kinds of work-arounds when I am programming code that will end up in a product.

Thanks for answer so many of my questions Bob!
hjvanderlinden
Cheese Wheel
Cheese Wheel
 
Posts: 64
Joined: Sat Jan 14, 2006 6:49 am

Re: Problem with header files

Postby bobmarlowe on Tue Apr 10, 2012 12:26 pm

The easiest way to switch from PSoC3 to PSoC5 is to change the device. I think, copying must fail. Just left-click in the Workspace-Explorer on the Project and then click on Device-selector. Well and select your device.

And: Regardless of which IDE you use (µVision, Eclypse) a C-Program will never ever work without header-files.

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1185
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Previous

Return to PSoC5 General

Who is online

Users browsing this forum: No registered users and 1 guest