file include question

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

I know this is a nob question but i had an issue where i was trying to do a fancy include but it didnt seem to work i would get implicit function declaration errors. does anyone know why? i changed the way i was including files but im just confused why this way didnt work.

--main_include.h

#ifndef __M_H_
#define __M_H_
#ifdef __O_
#include "file2.h"
#endif

typedef enum _status { GOOD, ERROR} status;
#endif

--file1.c

#define __O_
#include "main_include.h"
void main()
{
 function();  <--ERROR HAPPENS HERE
}
status function()
{
  return GOOD;
}

--file2.h

#ifndef __F2_H_
#define __F2_H_
#include "main_include.h"
status function(void);
#endif
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I realize that I will get a lot of disagreement, but having lots of little include files is really obsolete 1970's code thinking. Having lots of little files is a convention left over from the time when editors couldn't handle more than a few thousand characters in a single file.

Try putting all of your application's code including constants and declarations into one main file. The standard include files like stdio.h and likewise should remain as includes at the beginning of the code, but anything specific to your application should be in the main file.

If it can not be determined why a multiple include file fails, so what? The point of programming is to make code that works, not to determine why code fails. Granted this mentality is a perfect recipe for major bugs in the future as an application grows to be non-trivial, but proper design of the algorythm before coding will catch many of the bugs.

[ By the way, try to avoid texting shortcuts like substituting a lower-case 'i' for first-person singular "I" and skipping apostrophes "didnt" in public international specialized forums like this one. ]

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

Run your compiler with the option for showing the preprocessed source as the output. That should give you a hint as to what goes wrong.

Quote:

im just confused why this way didnt work.

The rest of us are confused by your include scheme as such. What on earth requires this complex and recursive include arrangement?

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

Quote:

What on earth requires this complex and recursive include arrangement?

Agreed. It may well be that something as the enum typedef is project wide and you want to put it into a .h that spans many files. While the function declaration for fuunction() is more localised but, because function() is shared between two .c it still needs to be in a .h to form a bridge. But why wouldn't you just

//main_include.h
typedef enum { GOOD, ERROR} status;
//file1.h
status function(void);
//file1.c
#include "main_include.h"
#include "file1.h"
void main() 
{ 
 function();  
} 
status function() 
{ 
  return GOOD; 
} 

As for not splitting things across files I'm guessing Simonetta has never worked with 100+ other engineers on a 50,000 .c file project such a the Linux kernel or a digital TV recorder or something equally complex. Sure you ca put everything (including the .h contents) into one very big .c file if you are a single engineer working on a 1K..16K AVR project but even if you trade up to a 64K..128K AVR with 2-3 engineers you are going to have fun trying to work like that!

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

I was trying to have 1 include file that i used in all my .c files i still have just 1 file i just added the #define __file_included in the main_include file it works.

/*
 * INCLUDE.h
 *
 *  Created on: Mar 14, 2011
 *      Author: Cheta Anyiah
 */

#ifndef INCLUDE_H_
#define INCLUDE_H_

/************************************************************************************
 * MANDITORY INCLUDE FILES
 ************************************************************************************/
#include 
#include 


/************************************************************************************
 * OPTIONAL FILE INCLUDE SECTION
 * add the #define for the module that you want to add
 * example for LCD support add #define LCD_7SEG
 ************************************************************************************/

#define TIMER_ENABLE //adds timer module to project
#define LCD_7SEG //adds seven segment LED module to project
#define COMMS	//adds usart communctions and remote monitoring module
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I was trying to have 1 include file that i used in all my .c files

Why? What does it gain? Is this about not having to edit something in two places (like function prototypes or externs etc)?

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

Include guards like this do nothing when including a .h file in multiple .c files. Its purpose is to prevent problems when the .h file is included multiple time from one .c file.

Regards,
Steve A.

The Board helps those that help themselves.

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

clawson wrote:
Quote:

I was trying to have 1 include file that i used in all my .c files

Why? What does it gain? [...]?

The only gain I can envision is for something that you use in all projects. Some macros that you are especially fond of, or some such.

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

this is the first time I'm attempting to use only one file I read it in a book but it does seem easier(once i got it working). for every .c file I only need to include 1 file any edits i know were to go.

/*
 * INCLUDE.h
 *
 *  Created on: Mar 14, 2011
 *      Author: Cheta Anyiah
 */

#ifndef INCLUDE_H_
#define INCLUDE_H_

/************************************************************************************
 * MANDITORY INCLUDE FILES
 ************************************************************************************/
#include 
#include 


/************************************************************************************
 * OPTIONAL FILE INCLUDE SECTION
 * add the #define for the module that you want to add
 * example for LCD support add #define LCD_7SEG
 ************************************************************************************/

#define TIMER_ENABLE //adds timer module to project
#define LCD_7SEG //adds seven segment LED module to project
#define COMMS	//adds usart communictions and remote monitoring module
#define UTIL	//includes function helpers..utilities

/************************************************************************************
 * PORT CONVERSTIONS code portability section
 ************************************************************************************/
//#define PORTA PORTA
//#define PORTB PORTB
//#define PORTC PORTC
//#define DDRA DDRA
//#define DDRB DDRB



/************************************************************************************
 * MACROS
 ************************************************************************************/
#define bit_get(word_or_byte_to_mask , bit_mask) ((word_or_byte_to_mask) & (bit_mask))		//ie) bit_get(variable_to_get,0x01 or bit_get(variable_to_get, BIT(0))
#define bit_set(word_or_byte_to_mask , bit_mask) ((word_or_byte_to_mask) |= (mbit_mask))		//ie) bit_set(variable_to_set,0x01) or bit_set(variable_to_set,BIT(0))
#define bit_clear(word_or_byte_to_mask , bit_mask) ((word_or_byte_to_mask) &= ~(bit_mask))	//ie) bit_clear(variable_to_clear, 0x01) or  bit_set(variable_to_clear,BIT(0)) 
#define bit_toggle(word_or_byte_to_mask , bit_mask) ((word_or_byte_to_mask) ^= (bit_mask))		//ie) bit_toggle(variable_to_toggle,0x01) or bit_toggle(variable_to_toggle,BIT(0))
#define bit_write(clear_or_set ,word_or_byte_to_mask , bit_mask) (clear_or_set ? bit_set(word_or_byte_to_mask , bit_mask) : bit_clear(word_or_byte_to_mask , bit_mask))		//clears bit specific if c > 0 otherwise sets
#define BIT(bit_to_set) (0x01 << (bit_to_set))				// translates a bit number to a bit position BIT(0) creates a mask of 00000001
#define LONGBIT(bit_to_set) ((unsigned long)0x00000001 << (bit_to_set))	//translate to a 32 bit mask

/************************************************************************************
 * Mandatory Prototypes
 ************************************************************************************/
void mainInit(void);


/************************************************************************************
 * #defines
 ************************************************************************************/
#define EOS '\0'		//end of string used in the sizeof comparison 

/************************************************************************************
 * Data Types
 ************************************************************************************/

typedef char		   byte;					  /* 8 bit quantity										*/
typedef unsigned char  ubyte;                    /* Unsigned  8 bit quantity                           */
typedef signed   char  Sbyte;                     /* Signed    8 bit quantity                           */
typedef unsigned int   uint16;                    /* Unsigned 16 bit quantity                           */
typedef signed   int   Sint16;                     /* Signed   16 bit quantity                           */
typedef unsigned long  uint32;                    /* Unsigned 32 bit quantity                           */
typedef signed   long  Sint32;                     /* Signed   32 bit quantity                           */
typedef float          real;                       /* Single precision floating point                    */
typedef long long 	   uint64;					  /* 64 bit unsigned */
typedef enum  _boolean {FALSE, TRUE} boolean;
//will be used for the return values of functions this helps with debugging
typedef enum _status {SUCCESS, ERROR } status;

//values used when setting specific bits
typedef enum _bitnumber {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN} bitnumber;





//---------------------------------------------------------------------------------------------------------------
/* To use LCD or the 7 segment display define LCD_7SEG */
#ifdef LCD_7SEG
#include "LCD7SEG.h"
#endif
//---------------------------------------------------------------------------------------------------------------

#ifdef COMMS
#include "comms.h"
#endif

//---------------------------------------------------------------------------------------------------------------
/* To use timer define TIMER_ENABLE */
#ifdef TIMER_ENABLE
#include "timer.h"
#endif
//---------------------------------------------------------------------------------------------------------------

#ifdef UTIL
#include "Utilities.h"
#endif


#endif /* INCLUDE_H_ */
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I read it in a book but it does seem easier

I'd suspect whether the author can identify his ass from his elbow.

It is NOT a good strategy. Suggest you read the tutorial about managing large projects to further understand.

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

lol i check it out