Making header file "extern"?? [Solved]

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

I'm unravelling some fettucCini code and it would be nice if I could define a header file in a file and then get another file to see it but WITHOUT including the file where the file is defined.

Confused? So am I and I have to explain what I want to do. :?

I have split up the bare essential code for a GLCD driver chip from my GLCD code so that all I need to do is to just include in the project the .c file and the .h file for the chip in question.

The driver chip's header file has a definitition

#define GLCD_Header_file "R61505W.h (or whichever chip is being used)"

and one of the files that needs to know the chip used has

#include GLCD_Header_file	// This file is defined in the header file for the driver chip

This works ok if I have the file defined within another header file which is used by the file that need to know the chip type. But this means that I need a list of possible header files which can be used and then comment out all of the ones I don't need. Or waste keyboard strokes and write the file name each time I use a different chip.

So I was hoping to automate this, there may be a way to say "extern headerfile_whatever" and then get the file name from the definition .h file of the chip used.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Fri. Dec 2, 2011 - 08:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do it the way avr-libc does it and wrap each #define [header_file] in a #ifdef [chip_name].

Regards,
Steve A.

The Board helps those that help themselves.

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

So a list of possible header files depending on the chip used?

But this would defeat the purspose of what I'm trying to do I think as I have to include the header file for the chip I'm using which is what I had up to now.

I just commented out the files not used

//#include "SSD1289.h" 
//#include "ILI9325.h"
//#include "SSD1963.h"
#include "R61505W.h"

not a big deal I guess. I'm trying to replace the above with

#include GLCD_Header_file   // This file is defined in the header file for the driver chip 

So I was hoping that somewhere there would be a global list of definitions which could be accessed by all files in the project without doing anything specific ie pull it out of a "global list".

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

As Steve suggested, I would use

#define SSD1289

#ifdef SSD1289
#include "SSD1289.h"
#endif
#ifdef ILI9325
#include "ILI9325.h"
#endif
#ifdef SSD1963
#include "SSD1963.h"
#endif
#ifdef R61505W
#include "R61505W.h"
#endif

/Martin.

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

#define SSD1289 (or whatever chip) is ONLY defined in the driver chip's header file at the moment and only the driver chip's C file sees that file as it it is included.

I guess what I'm trying to do is to be able to see a definition in a file from a file of which is has no knowledge of except that is has been added to the project along with it's .c file.

The graphic.c file needs to get to 2 functions which are in the chip's driver .c file therefore I need to include it's header file.

What the purpose of the exercise is to simply change the driver chips's .h and .c file and recompile. Not a key stroke. :)

Ohhhh my head hurts when I'm trying to be tooooo clever for my boots.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:

I guess what I'm trying to do is to be able to see a definition in a file from a file of which is has no knowledge of except that is has been added to the project along with it's .c file.

Can't do. Most C compilers are compiling each source file separately. At compile time the compiler knows nothing but what is sees in the "compilation unit" (roughly the preprocessed source file it is compiling at the moment).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

If the 2 functions used in graphic.c have the same name for all chips, you could simply declare them as extern in graphic.c instead of including the complete header-file.

/Martin.

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

Post a minimal but compilable (I mean you don't need to post real functions, but real files and a guideline how to compile them) example of how do you do it "manually" (a zip of files).

JW

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

Quote:
Can't do.
But the impossible only takes a little longer... :)
Quote:
If the 2 functions used in graphic.c have the same name for all chips, you could simply declare them as extern in graphic.c instead of including the complete header-file.
hmmm that may work.

Right now NOTHING works as I stuffed up something else. :evil:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

You would normally use a 'general' interface. e.g. "graphic_lcd.h".
Your main.c only sees "graphic_lcd.h" and only uses general purpose functions like glcd_init() or glcd_gotoxy().

Every 'driver.c' file implements the general purpose functions. You link with whichever brand of driver that you want.

The downside is that you only work with the lowest common denominator of LCDs' capabilities.

Think about it. This is exactly how the C standard library works. 'strcpy()' or 'putchar()' have the same functionality on a PC, PIC, ARM, 8051, ...

But the underlying machine instructions will be vastly different.

David.

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

> #define SSD1289 (or whatever chip) is ONLY defined in the driver chip's header file

Then define it outside, in a generic file. All of them. Then, you could
use

#if GLCD == SSD1289
#  include "ssd1289.h"
#elif GLCD == FOOBAR
#  include "foobar.h"
#elif GLCD == JS
#  include "js.h"
#else
#  error "Unknown GLCD"
#endif

Then, compile it with -DGLCD=SSD1289.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Back in the land of the living, pulled out one too many wires just like I do with my hair. :lol:

Just declaring those 3 functions as extern in the required files fixed the issue. Now I don't need to include the .h file.

From my dabbling with GLCD in the past few months it seems that only 3 functions are different for all kinds of driver chips. So I have split up the code so that nothing needs to change in a project if I'm using the same size GLCD which may use a different chip.

Just change the required driver .c and .h files and it should all work.

In case anyone wonders these are the 3 functions which require different code for different chip.

// Functions accessible to other modules
void GLCD_Init (void);
void GLCD_SetCursor (unsigned int x, unsigned int y);
void GLCD_SetArea (unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2);

GLCD_Init is obviously called by main while the other 2 are used by the graphics driver.

Now on with the continuing task of reducing the number of wires used by the display by adding code for 8 bit and SPI mode.

Once it all works I may try and run the code on the LPCXpresso. That will be FUN!!

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly