Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-04-2012, 07:40 PM   #1
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 120

Rep: Reputation: Disabled
Beginning C programming- questions about basic use of make and compiling

I am trying to learn how to put together a simple make file. I am using as a template.

I have already placed a generic set of CFLAGS into the environment via my .bashrc file.

Here is my Makefile:

keith@t520:~/cprograms/rpcalc$ cat Makefile 
DEPS = rpcalc.h

%.o: %.c $(DEPS)
	$(CC) -c -o $@ $< $(CFLAGS)

newcalc:	rpcalc-main.o getch.o getop.c stack.o
	gcc -o newcalc rpcalc-main.o getch.o getop.o stack.o
Here is the output:
keith@t520:~/cprograms/rpcalc$ make -B
cc -c -o rpcalc-main.o rpcalc-main.c -g -O0 -W -Wall -fno-common -Wcast-align -Wredundant-decls -Wwrite-strings -Wbad-function-cast -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wparentheses -pedantic -Wparentheses -ansi
cc -c -o getch.o getch.c -g -O0 -W -Wall -fno-common -Wcast-align -Wredundant-decls -Wwrite-strings -Wbad-function-cast -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wparentheses -pedantic -Wparentheses -ansi
getch.c:8:5: warning: no previous prototype for ‘getch’ [-Wmissing-prototypes]
getch.c:13:6: warning: no previous prototype for ‘ungetch’ [-Wmissing-prototypes]
cc -c -o stack.o stack.c -g -O0 -W -Wall -fno-common -Wcast-align -Wredundant-decls -Wwrite-strings -Wbad-function-cast -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wparentheses -pedantic -Wparentheses -ansi
gcc -o newcalc rpcalc-main.o getch.o getop.o stack.o
So why am I getting those compiler warnings about getch and ungetch having no previous prototype? Those two functions are both contained in the getch.c file. Here is the rpcalc.h header file:

keith@t520:~/cprograms/rpcalc$ cat rpcalc.h
#define 	NUMBER		'0'

void push(double);
double pop(void);
int getop(char []);
int getch(void);
void ungetch(int);
The compiled code works properly, but I feel I must be either writing the Makefile wrong or misunderstanding the scope of the prototypes in the header file???

I 'm wondering if the header file is being read at all- I'm not using the -I flag in the makefile because I couldn't figure out how to invoke it without it over-riding the $CFLAGS environmental variable. But it appears to be using the header since the other source code files are not having any warnings.

Here is the getch.c file:
#include <stdio.h>

#define BUFSIZE	100

char buf[BUFSIZE];		/* buffer for ungetch		*/
int bufp = 0;			/* next free position in buf	*/

int getch(void)			/* get a (possibly pushed back) character */
	return (bufp > 0) ? buf[--bufp] : getchar();

void ungetch(int c)		/* push character back on input	*/
	if (bufp >= BUFSIZE)
		printf("Ungetch: too many characters\n");
		buf[bufp++] = c;
Getch and ungetch are called by the getop.c file. I have tried changing their position in the Makefile statements but still get the warnings.
Old 03-05-2012, 12:49 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
The message tells you which "warning" subset was triggered: -Wmissing-prototypes

Here's the snippet from the gcc man page:
-Wmissing-prototypes (C and Objective-C only)
    Warn if a global function is defined without a previous
    prototype declaration.  This warning is issued even if the
    definition itself provides a prototype.  The aim is to detect
    global functions that fail to be declared in header files.
So there you have it

To avoid the warning, you need to declare function prototypes in a header. Then include the header in whatever C code files intend to use those functions.

To be clear, adding rpcalc.h as a dependency in the Makefile does not have the same effect as #include'ing it in the C source file. Further, you would need to #include rpcalc.h (containing the prototypes for the defined functions) in the file that actually provides the implementation for those functions as well (i.e. getch.c). That last point (about #include'ing rpcalc.h in getch.c) is specifically what is triggering the warning.

BTW, you also have a typo in your Makefile. You list newcalc as having a dependency on getop.c as opposed to getop's object file (as you do for all the other dependencies).

Last edited by Dark_Helmet; 03-05-2012 at 01:05 AM.
1 members found this post helpful.
Old 03-05-2012, 08:13 AM   #3
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 120

Original Poster
Rep: Reputation: Disabled
Thank you once again Dark_Helmet for taking the time to go over this code. I do understand the requirement to have the (rpcalc.h) header listed in each source file that needs it, I just forgot it in this file and didn't see the mistake until you pointed it out.

Amazing, how a programming language "just works" if you do it correctly!

Old 03-05-2012, 11:48 AM   #4
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Well, I could make the argument that it's dumb to require a header with the prototype of functionA() in a file that provides the full implementation of functionA().

Many of the warnings are there to make sure the developer explicitly identifies exactly what the compiler should do. Though, there are times when I feel like some of the warnings are there to "encourage programming style" rather than identify technical ambiguity.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
A Few Basic Linux Questions (Mounting ISO, Make Install, Etc.) Melon Bread Linux - Newbie 10 05-09-2012 04:58 PM
[SOLVED] Beginning C programming- learning basic use of malloc/realloc keithostertag Programming 5 02-28-2012 11:08 AM
[SOLVED] Beginning C programing- basic use of pointers, reallocating memory, etc. keithostertag Programming 5 02-24-2012 11:52 PM
Beginning programming mikeymorgan Programming 38 06-28-2005 07:59 AM
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration