C header file

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 3863 Views - Last Post: 07 April 2008 - 11:15 PM Rate Topic: -----

#1 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

C header file

Post icon  Posted 05 April 2008 - 10:33 PM

i have added a C header file in the C include directory, but when i compile the error msg said [Linker error] undefined reference. please advise.
Is This A Good Question/Topic? 0
  • +

Replies To: C header file

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: C header file

Posted 05 April 2008 - 10:35 PM

Vauge answer for a vauge question.

You'll need to define it.

If you post the source where the variable is define, as well as it's usage in your program, someone can be a bit more helpful.
Was This Post Helpful? 0
  • +
  • -

#3 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 10:57 PM

View Postno2pencil, on 5 Apr, 2008 - 10:35 PM, said:

Vauge answer for a vauge question.

You'll need to define it.

If you post the source where the variable is define, as well as it's usage in your program, someone can be a bit more helpful.


below is the usage in my program as the p_prompt caused the error msg:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <p_io.h>

int main(void) {
	double a_3, a_2, a_1, a_0;
	double x1, x2;
	p_prompt("Enter the coefficients a_3, a_2, a_1 and a_0: ", "%lg%lg%lg%lg", &a_3, &a_2, &a_1, &a_0);
	p_prompt("Enter the end points: ", "%lg%lg", &x1, &x2);
	return EXIT_SUCCESS;
}




this is the C header file called p_io.h:

#ifndef _PIO_H_
#define _PIO_H_

#include <stdbool.h>

int p_scanf(const char *, ...);
int p_fscanf(FILE * restrict, const char *, ...);
int p_prompt(const char * restrict, const char *, ...);
int p_guarded_prompt(bool (*)(void * *), const char * restrict, const char *, void * *);

#endif

/* p_scanf and p_fscanf are similar to scanf and fscanf, respectively,
 * as described in Section 15.8 of "C A Reference Manual", fifth edition,
 * by Samuel P. Harbison III and Guy L. Steele J.,
 * except for the processing of multibyte characters:
 * the l size specifier to the c, s and [ conversion letters is not supported.
 * p_scanf and p_fscanf differ from scanf and fscanf in that:
 * - they expect the input to be terminated by \n EOF;
 * - they expect the whole contents of the control string to be used up;
 * - they expect no input to remain after all conversions have been performed;
 * - they expect the format string to be syntactically correct;
 * - they perform a conversion only if the input matches the range of possible values
 *   of the type determined by the conversion letter and size specifier;
 * - they allow the format string to contain any number of substrings of the form
 *   <=n, >=n or !=n, where n is an integer or floating point number, to be placed right
 *   after the * and maximum size width modifiers if present, to force the input to be
 *   accepted only if it is less than or equal to, greater than or nonequal to,
 *   or nonequal to n, respectively;
 *   these new modifiers will be ignored if the associated conversion letter is one of
 *   c, s, p, n %, or [.
 * If one of the previous conditions fails to be satisfied, then the functions return
 * EXIT_FAILURE and print out to standard error a cause for failure, reporting that
 * 1. Control D does not follow carriage return, or
 * 2. there is not enough input to match the whole format string, or
 * 3. some input is left after the whole format string has been processed, or
 * 4. a part of the format string is not syntactically correct, or
 * 5. a part of the format string is not matched by the input,
 *   with a mention of whether
 *   - no input is left, or
 *   - the input does not match the type determined by the part of the format string
 *	 being processed (in particular, the z size specifier expects the input to be positive,
 *	 even if associated with the d or i conversion letters, and
 *	 the u, o and x conversion letters, associated with any size specifier,
 *	 expect the input to be positive), or
 *   - the norm of the input is too large, or too close to 0, or
 *   - for the new modifers of the form <=n, >=n and !=n, that the input fails a constraint
 *	 of the form <=, >= or !=, respectively.
 * When 1., 2. or 3. are causes for failure, the first cause, in that order, is reported.
 * When there is a cause for failure which is neither 1., 2. or 3.,
 * then the cause associated with the leftmost conversion specifier or character
 * in the format string is reported, and the final segment of the segment string starting with
 * that leftmost conversion specifier or character is output to standard error.
 * The functions also return EXIT_FAILURE if there is a syntactic error in the format string,
 * printing out to standard error a message to this effect together with what remains of the
 * format string that cannot be processed, unless a shorter initial segment of the format string
 * fails to match the input for one of the reasons 1.to 5. above.
 *
 * p_prompt takes a string s, a format string s' and a variable list l of pointers as argumemts.
 * The string s is printed out to standard output (not followed by a new line character), and
 * the program waits for the user to input some values that will be processed by p_scanf with
 * s' and l as aguments. The function p_prompt returns EXIT_SUCCESS if p_scanf succeeds;
 * otherwise the error messages of p_scanf are sent to standard error and the procedure repeats.
 *
 * p_guarded_prompt takes a pointer to a function, say guard, a string s, a format string s',
 * and a pointer p to a pointer to void as arguments; p is meant to be an array args of
 * pointers of size equal to the number of values to store, and
 * guard is meant to take p as argument and return true or false.
 * Let l be the variable list of pointers determined by args in the obvious way.
 * The string s is printed out to standard output (not followed by a new line character), and
 * the program waits for the user to input some values that will be processed similarly as
 * if they were processed by p_scanf with s' and l as aguments.
 * The function p_guarded_prompt returns EXIT_SUCCESS if p_scanf would succeed and guard returns
 * true. If p_scanf would fail then the error messages of p_scanf are sent to standard error
 * and the procedure repeats. If p_scanf would succed and guard returns false then guard might be
 * implemented in such as way that it sends error messages to a stream (preferably standard error)
 * and the procedure repeats.
 * As an example of use, suppose that a negative int and an int greater than 15 have to be
 * provided by the user, and stored in int variables m and n, respectively.
 * Then the code might define args as follows:
 * void * args[2] = {&m, &n};
 * The function guard might be defined as follows:
 * bool guard(void * args[2]) {
 *	 if (*(int *)args[0] >= 0) {
 *		 fprintf(stderr, "First input is not negative.\n");
 *		 return false;
 *	 }	
 *	 if (*(int *)args[1] <= 15) {
 *		 fprintf(stderr, "Second input is not greater than 15.\n");
 *		 return false;
 *	 }	
 * return true;
 * }
 * The function p_guarded_prompt would be called as follows:
 * p_guarded_prompt(guard, "Input a negative int and an int greater than 15: ", "%d %d", args).
 * But note that the function p_guarded_prompt is meant to be used to impose more complex
 * conditions on the input: with this example, a statement of the form
 * p_prompt("Input a negative int and an int greater than 15: ", "%<=-1d %>=16d", args)
 * would do the job. */


Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C header file

Posted 05 April 2008 - 11:01 PM

Try:

#include "p_io.h"


Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: C header file

Posted 05 April 2008 - 11:02 PM

int p_prompt(const char * restrict, const char *, ...);


Ok, so you've defined a function with 2 arguments, & a return type. So what is the compiler supposed to DO with it? I don't see any code or functionality for this function. & what is with the three dots in the argument list?

View PostKYA, on 6 Apr, 2008 - 02:01 AM, said:

Try:

#include "p_io.h"


That would be if it was in the local directory. In the initial question the poster specified that he moved the header file into the includes directory. Also, the error would be that the header file is not found, rather than the function is undefined.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: C header file

Posted 05 April 2008 - 11:06 PM

I misread his post then :( That's twice now
Was This Post Helpful? 0
  • +
  • -

#7 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:14 PM

View Postno2pencil, on 5 Apr, 2008 - 11:02 PM, said:

int p_prompt(const char * restrict, const char *, ...);


Ok, so you've defined a function with 2 arguments, & a return type. So what is the compiler supposed to DO with it? I don't see any code or functionality for this function. & what is with the three dots in the argument list?

View PostKYA, on 6 Apr, 2008 - 02:01 AM, said:

Try:

#include "p_io.h"


That would be if it was in the local directory. In the initial question the poster specified that he moved the header file into the includes directory. Also, the error would be that the header file is not found, rather than the function is undefined.



i have tried #include "p_io.h" but same error msg.
Was This Post Helpful? 0
  • +
  • -

#8 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:16 PM

You have the header p_io.h, but not the actual code for all the functions.

You still have to tell the compiler to compile the actual source code as well.

IE, assumeing the source file is name p_io.cpp

command line compilers:

GNU
gcc p_io.c <your other source files>

MS
cl p_io.c <your other source files>

Borland
bcc p_io.c <other files>
note: depending on the version, it might be bcc32

Digital Mars
dmc p_io.c <other files>


For a GUI compiler (Visual Studio, Turbo C++, ect...)
you need to add the source file to your project.
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: C header file

Posted 05 April 2008 - 11:16 PM

View Postkckc314, on 6 Apr, 2008 - 02:14 AM, said:

i have tried #include "p_io.h" but same error msg.

That doesn't answer the question of "Where is the code to the function?". You've defined it, but what does it do?
Was This Post Helpful? 0
  • +
  • -

#10 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:19 PM

View Postno2pencil, on 5 Apr, 2008 - 11:02 PM, said:

int p_prompt(const char * restrict, const char *, ...);


Ok, so you've defined a function with 2 arguments, & a return type. So what is the compiler supposed to DO with it? I don't see any code or functionality for this function. & what is with the three dots in the argument list?


That is called a ellipsis. It means that the function takes a variable amount of arguments.

http://www.cprogramm...l/lesson17.html
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: C header file

Posted 05 April 2008 - 11:21 PM

View PostCerolobo, on 6 Apr, 2008 - 02:19 AM, said:

That is called a ellipsis. It means that the function takes a variable amount of arguments.

http://www.cprogramm...l/lesson17.html

I knew it was referred to as ellipsis when in a GUI environment. As in, "click on the ellipsis to bring up the options". However, I had no idea that it could be used in C++! Neat, thanks for the link.
Was This Post Helpful? 0
  • +
  • -

#12 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:26 PM

It's actually used in a lot of C functions

printf(char *fmt, ...);
scanf(char *fmt, ...);

as well as a few file I/O functions.

This post has been edited by Cerolobo: 05 April 2008 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

#13 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:33 PM

View Postno2pencil, on 5 Apr, 2008 - 11:16 PM, said:

View Postkckc314, on 6 Apr, 2008 - 02:14 AM, said:

i have tried #include "p_io.h" but same error msg.

That doesn't answer the question of "Where is the code to the function?". You've defined it, but what does it do?



ok guys, i don't quite follow here. anyway, to make it simple, as you have seen my header file and how i include it into my program. FYI, right now i have 2 files p_io.h and p_io.c in the C include directory. I am using a GUI compiler. each time i just click on the compile and run button to test the program. you may tell me where do i put the files, how i put them, how to link etc. is it a once and for all job, after that i just use my IDE as usual just click compile and run. please advise a simple step.
Was This Post Helpful? 0
  • +
  • -

#14 Cerolobo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:37 PM

What GUI are you useing?

For Visual Studio, you should have something a toolbar named "Solution Explorer". You have to just drag and drop the file into the project tree.

for Turbo C++, you should still have some type of toolbar that has a list of the files in your project. Again, drag and drop.
Was This Post Helpful? 0
  • +
  • -

#15 kckc314  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 135
  • Joined: 05-April 08

Re: C header file

Posted 05 April 2008 - 11:39 PM

View PostCerolobo, on 5 Apr, 2008 - 11:26 PM, said:

It's actually used in a lot of C functions

printf(char *fmt, ...);
scanf(char *fmt, ...);

as well as a few file I/O functions.



ok guys, i don't quite follow here. anyway, to make it simple, as you have seen my header file and how i include it into my program. FYI, right now i have 2 files p_io.h and p_io.c in the C include directory. i have seen the functions are in p_io.c file. I am using a GUI compiler. each time i just click on the compile and run button to test the program. you may tell me where do i put the files, how i put them, how to link etc. is it a once and for all job, after that i just use my IDE as usual just click compile and run. please advise a simple step.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3