Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

header files in C

Name: Anonymous 2007-06-23 21:40 ID:HRBo/NOe

Should you have to include a header file into its own library? I can't understand why I sometimes, but not always, get compile errors because it's not getting the header file prototypes before it compiles the libraries.

Name: Anonymous 2007-06-23 21:43 ID:PfWLFR57

Huh?

I don't get what you're asking.

Name: Anonymous 2007-06-23 21:57 ID:zLgI2oUp

There's a huge difference between linking and header files.
with #include , the compiler assumes your source file contains the contents of the header file.

Name: Anonymous 2007-06-23 22:04 ID:moow8DUG

Uh, C doesn't need prototypes. All functions are assumed to return machine-word sized ints, and take variable number of args. Additionally, all functions are 'global' unless declared as static, whereby they're only visible in the unit module they're defined in. Any symbols which can't be resolved at compile-time are done so at runtime.

Then again, I have no idea what OP is trying to ask.

Name: Anonymous 2007-06-23 22:04 ID:Heaven

>>4
link-time, not runtime, sorry.

Name: Anonymous 2007-06-24 0:19 ID:XvoRJg7w

Well, I have a library lotto.c, and a header for it, lotto.h. The free Borland C compiler gives me errors relating to not having function definitions, which would mean it isn't looking in the header file before processing the library. When I include the header, it compiles just fine.

This was not the case one project ago, I didn't need to include a header into its own library and it went and got the header like it was supposed to. I'm not sure what I'm doing differently.

>>4
Yeah I know C makes up prototypes, if those prototypes don't match your function it will break.

Name: Anonymous 2007-06-24 1:03 ID:fqvvSFKt

What do you mean you `have a library lotto.c'? I don't consider source files by themselves to be `libraries', per se, hence the confusion my fellow posters and I experienced earlier on.

But I digress, your problem is really vague and is most likely a problem on your end, not the compiler. Post a sample source file/project so the problem can be investigated further.

Name: Anonymous 2007-06-24 1:56 ID:EBWyJeTW

Just having a header won't help you, and it certainly won't help us help you. We need to see the code you have written.

Name: Anonymous 2007-06-24 2:36 ID:XvoRJg7w

//====== FILE = dates.c ========================================================
//= Provides a library of functions for dealing with dates.
//==============================================================================
//= Notes:
//= Includes functions for:
//= - Finding the day a date falls on
//= - Extracting information from YYYYMMDD format dates
//= - Finding out if a year is a leap year
//=
//= All functions were originally found in Dr. William Albrecht's COP 3514
//= Summer 2007 Chapter 6 PowerPoint slides.
//==============================================================================
//= History:
//= 06/23/2007 : Genesis
//==============================================================================

//====== FUNCTION calc_day_num( long int the_date ) ============================
//= Finds the day a date in YYYYMMDD format falls on.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: Day the date falls on, with 0 for Sunday, 1 for Monday, etc.
//==============================================================================
int
calc_day_num(long int the_date)
{
  int N,M,Y,C,L,d;
  N=get_N(the_date);
  M=get_M(the_date);
  if (M>2)  M=M-2; //adjust month num for formula
  else M=M+10;
   
  Y=get_Y(the_date);
  C=get_C(the_date);
  L=leap_year(C*100+Y);
   
  d=2.6*M-0.2;
  d=d+N+Y+Y/4+C/4-2*C - (1+L)*(M/11); //integer calcs!
  //The () around M/11 are required! Understand why!
  d=d%7;
  return d;
} //END FUNCTION calc_day_num

//====== FUNCTION get_N( long int the_date ) ===================================
//= Extracts the DD in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: DD in YYYYMMDD
//==============================================================================
int
get_N( long int the_date )
{
  int val;
  val=the_date - the_date/100*100;
  return val;
} //END FUNCTION get_N

//====== FUNCTION get_M( long int the_date ) ===================================
//= Extracts the MM in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: MM in YYYYMMDD
//==============================================================================/* gets the month from YYYYMMDD */
int get_M( long int the_date )
{    int val;
    val=the_date/100 - the_date/10000*100;
    return val;  }  // End get_M

//====== FUNCTION get_Y( long int the_date ) ===================================
//= Gets the two least significant digits from YYYY in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: The two least significant digits from YYYY in YYYYMMDD
//==============================================================================
int
get_Y( long int the_date )
{
  int val;
  val= the_date/10000 -the_date/1000000L*100;
  return val;
} //END FUNCTION get_Y

//====== FUNCTION get_C( long int the_date ) ===================================
//= Gets the two most significant digits from YYYY in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: The two most significant digits from YYYY in YYYYMMDD
//==============================================================================
int
get_C( long int the_date )
{
  int val;
  val=the_date/1000000L;
  return val;
} //END FUNCTION get_C

//====== FUNCTION leap_year( int the_year ) ====================================
//= Takes a four-digit year, YYYY, and returns whether or not it's a leap year.
//=-----------------------------------------------------------------------------
//= Inputs: The year in YYYY format
//= Returns: 1 if leap year, 0 otherwise
//==============================================================================
int
leap_year( int the_year )
{
  int val=0; //Set to Not Leap Year
  if (the_year/100*100 == the_year) //divisible by 100
  {
    if(the_year/400*400==the_year) //divis. by 400
    val=1;
  } // end divs by 100 == true
  else if(the_year/4*4==the_year)
    val=1;
  return val;
} //END FUNCTION leap_year

//====== FILE = dates.h ========================================================
//= Provides function prototypes for the library defined in dates.c
//==============================================================================
//= Notes:
//= Function definitions are found in dates.c
//==============================================================================

//If this file has not been included already, continue:
#ifndef DATES_H
#define DATES_H

//------ Function Prototypes ---------------------------------------------------

//====== FUNCTION calc_day_num( long int the_date ) ============================
//= Finds the day a date in YYYYMMDD format falls on.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: Day the date falls on, with 0 for Sunday, 1 for Monday, etc.
//==============================================================================
int calc_day_num( long int the_date );

//====== FUNCTION get_N( long int the_date ) ===================================
//= Extracts the DD in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: DD in YYYYMMDD
//==============================================================================
int get_N( long int the_date );

//====== FUNCTION get_M( long int the_date ) ===================================
//= Extracts the MM in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: MM in YYYYMMDD
//==============================================================================
int get_M( long int the_date );

//====== FUNCTION get_Y( long int the_date ) ===================================
//= Gets the two least significant digits from YYYY in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: The two least significant digits from YYYY in YYYYMMDD
//==============================================================================
int get_Y( long int the_date );

//====== FUNCTION get_C( long int the_date ) ===================================
//= Gets the two most significant digits from YYYY in YYYYMMDD.
//=-----------------------------------------------------------------------------
//= Inputs: The date in YYYYMMDD format
//= Returns: The two most significant digits from YYYY in YYYYMMDD
//==============================================================================
int get_C( long int the_date );

//====== FUNCTION leap_year( int the_year ) ====================================
//= Takes a four-digit year, YYYY, and returns whether or not it's a leap year.
//=-----------------------------------------------------------------------------
//= Inputs: The year in YYYY format
//= Returns: 1 if leap year, 0 otherwise
//==============================================================================
int leap_year( int the_year );

#endif

When compiled with the rest of the project, I get a bunch of warnings that there were no prototype definitions for the functions in dates.c. If I include "dates.h" I don't get those errors. I'm looking for what I'm doing wrong so that I don't have to include the .h file. The last project I did, that wasn't necessary.

Name: Anonymous 2007-06-24 3:13 ID:loVMI5/3

It's perfectly fine to include the .h file in its corresponding .c source file; after all, you are using include guards. Your last project was just a fluke in that respect.

Name: Anonymous 2007-06-24 3:22 ID:EBWyJeTW

It isn't necessary either, warnings are not errors and differs from errors in that they're not fatal to the compilation process. You should however, like the warnings tell you, include the header file.
Why is it that you don't want to? Minimize time typing? Trust me you're just shooting yourself in the foot, and nuts.

Name: Anonymous 2007-06-24 3:52 ID:XvoRJg7w

>>11
It's not that I don't want to include them, it's just that I was curious as to how it's supposed to work. I thought that since the headers are included in the main program file that including them again is redundant.

Name: Anonymous 2007-06-24 4:02 ID:fqvvSFKt

>>12
Compilation of source files are done independantly of each other, so you need to include headers in each file whereever you need them. It's um, pretty obvious.

Name: Anonymous 2007-06-24 17:51 ID:mxoxU4hu

Compilation is an obsolete requirement of certain implementations of programming languages.

Name: Anonymous 2007-06-24 18:50 ID:SFo3ibdy

>>14
Just because premature optimization is evil, doesn't mean all optimization is evil. There's a reason CPUs are designed to process... ah I can't even be bothered finishing what I was saying. Screw you guys I'm going to bed.

Name: Anonymous 2007-06-24 19:12 ID:Heaven

>>14
too bad interpreted languages is slow as fuck.

Name: Anonymous 2007-06-24 23:11 ID:Heaven

>>16
Too bad your CPU is slow as fuck.

Time spent actually executing the program is nothing to the amount of time spent building it, if you're paying the developers that is. Interpreted languages cut development costs, increase revenue, and provide ENTERPRISE CEOs with better parking spaces. DOWN WITH C!

Name: Anonymous 2007-06-24 23:12 ID:/mNYqvMq

>>14

If compilation is obsolete, how do you plan to compile the interpreter?

Name: Anonymous 2007-06-24 23:50 ID:rK4eewFu

>>18
you don't.
you interpret the interpreter

Name: Anonymous 2007-06-25 0:08 ID:Heaven

>>17
i'm coding OSes and emul8rs for hobby and i have noone above my head.

Name: Anonymous 2007-06-25 0:09 ID:yoV63RLw

>>19
It's interpreters all the way down, young man!

Name: Anonymous 2007-06-25 0:31 ID:P9UlcDgt

>>17
too bad fast CPU is expensive as fuck.

Name: Anonymous 2007-06-25 8:59 ID:DH/5UnSV

>>22
cheaper than coding in asm

Name: Anonymous 2007-06-25 19:21 ID:YM7ijnvx

>>17
Just use a language which can be interpreted and compiled. You develop in an interpreted environment and then release builds as compiled optimised modules. FFS.

Name: Anonymous 2007-06-25 19:22 ID:Heaven

ITT amateur hour.

Name: Anonymous 2007-06-25 19:25 ID:Heaven

>>25
It is once you arrive

Name: Anonymous 2007-06-25 19:35 ID:YM7ijnvx

>>26

ZING!!!

Name: Anonymous 2007-06-25 19:58 ID:s3JL39w4

>>24

Does the java native compiler work yet, or is it still just a broken toy?

Name: Anonymous 2007-06-25 20:02 ID:Heaven

i like the way perl does it. it's almost as fast as compiled code, yet it requires no more work from the programmer than using an interpreted language.

Name: Anonymous 2007-06-25 20:20 ID:Heaven

>>29
too bad perl is slow as fuck

Name: Anonymous 2007-06-25 20:32 ID:Heaven

>>30
sub factor{my@f,$n=pop;map{$n/=$_,push@f,$_ until$n%$_}2..$n/2;@f?@f:($n)}
print "prime factors of $_: ",join',',factor$_ for@ARGV

Name: Anonymous 2007-06-25 20:35 ID:Heaven

>>31
that's small, not fast.

Name: Anonymous 2007-06-25 20:52 ID:G5DW3Y2f

>>31
better:
print"prime factors of $_: @{[factor$_]}"for@ARGV

Name: Anonymous 2007-06-25 20:52 ID:Heaven

>>32
try doing it faster in an interpreted language.

Name: Anonymous 2007-06-25 21:39 ID:Heaven

>>34
it can be done faster; in Haskell for example
i'd prefer to do it in C or ASM though.

Name: Anonymous 2007-06-25 21:54 ID:Heaven

>>35
CODE OR GTFO

Name: Anonymous 2007-06-26 10:50 ID:fR11vpWO

>>17
THIS MAN SPEAKS THE TRUTH

Name: Anonymous 2007-06-26 20:25 ID:E3kJQrjQ

Watch as I bring up more confusion:

Say if you have

somefuncs.h - Containing _declarations_ such as "extern void somefunc();"

somefuncs.c - Containing _definitions_ such as "void somefunc()".

If you were to include somefuncs.h into somefuncs.c, wouldn't their be two symbols for somefunc() as (as I have been told) extern tells the compiler that this function will be found in another unit/source file, so, including the prototype extern void somefunc(), and then the function definition void somefunc(), would create a duplicate set of functions, one being defined and one being undefined.

OR

Is extern inferred on functions without a static prefix - because if it is, that's saved me a lot of head ache.

Name: Anonymous 2007-06-26 22:21 ID:ymXUGzBP

You would use the extern in the "somefuncs.c".
It tells the compiler that the function exists, but not necessarily in the header file.  For example, you could use it to access functions from other libraries you do not have the source to but can link against.

Name: Anonymous 2007-06-26 23:12 ID:Pc14mBHm

No, I think you shouldn't include a header with extern and the definition in the same ".c" file. you probably need to split that header in two.

Name: Anonymous 2007-06-27 5:42 ID:QZVu6fPc

Looks like some sorta blind spot in C; I read a 4 page doc on this and still left scratching my head :(

Name: Anonymous 2007-06-27 6:28 ID:IRsSvb/I

This compiles with no warnings (-Wall -W etc.)

#include <stdio.h>

void x();
extern void x();

int main()
{
        x();
        return 0;
}

void x() {
        puts("Tada!");
}


You want "void somefunc();" in your *.h files, and "extern void somefunc();" in your *.c files, and in the latter case, you usually won't even need it.

Name: Anonymous 2007-06-27 8:30 ID:40Vzgd2e

>>42

puts() is gay, don't use it. you are not EXPERT HARRVARRD PROGRAMMER

Name: Anonymous 2007-06-27 9:03 ID:B5ZtvHvz

>>42
Does the linker have fits?

Name: Anonymous 2007-06-27 14:28 ID:2UE/EANS

>>43
printf("%s\n", "Tada!"); ?

Name: Anonymous 2007-06-27 22:07 ID:40Vzgd2e

>>45
printf("Tada!\n");

you fucking moron

Name: Anonymous 2007-06-27 22:19 ID:Heaven

>>46
char tada[]="Tada!\n";
int t=write(stdout,tada,strlen(tada));
if(t==-1) write(stderr,"could not write to stdout!",26);

you fucking moron

Name: Anonymous 2007-06-27 22:25 ID:EI0HlTro

>>47

char tada[]="Tada!\n";
if(write(stdout,tada,strlen(tada))==-1 && write(stderr,"could not write to stderr!\n",27)==-1) exit(255);

you fucking moron

Name: Anonymous 2007-06-27 22:42 ID:imL7JRaL

>>48
puts("Tada!");
you fucking moron

Name: Anonymous 2007-06-27 22:59 ID:jcLi1qb5

>>48
you don't need to write the terminating null character.
and why are you writing to stderr that you can't write to stderr if you can't write to stdout, but don't yet know whether or not you can write to stderr?
also, not being able to write to stdout and/or stderr is not usually a fatal error.

tl;dr: you fucking moron

Name: Anonymous 2007-06-27 23:31 ID:EI0HlTro

>>50
you don't need to write the terminating null character.
\n doesn't mean null, it means newline. lern2c.
and why are you writing to stderr that you can't write to stderr if you can't write to stdout, but don't yet know whether or not you can write to stderr?
This is the cvs version, get the latest stable build if you want sane stdout/stderr logging.
also, not being able to write to stdout and/or stderr is not usually a fatal error.
Generally that is a true statement, unless the entire point of the program is to write to stdout, in which case not writing to stdout means you have failed completely.

Also I'm not seeing you coming up with anything better, so shut the hell up.

tl;dr: you fucking moron

Name: Anonymous 2007-06-27 23:48 ID:Heaven

>>1-52
tl;dr: you fucking morons.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List