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:
Anonymous2007-06-23 21:43 ID:PfWLFR57
Huh?
I don't get what you're asking.
Name:
Anonymous2007-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:
Anonymous2007-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.
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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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;
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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-06-24 17:51 ID:mxoxU4hu
Compilation is an obsolete requirement of certain implementations of programming languages.
Name:
Anonymous2007-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.
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!
>>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.
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.
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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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:
Anonymous2007-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.
>>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:
Anonymous2007-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
>>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:
Anonymous2007-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.