LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 08-26-2002, 05:50 PM   #1
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 219

Rep: Reputation: 30
Why doesn't this simple program compile?


This is a demonstration of multiple source code compilation in the book "Linux Programming Unleashed 2nd Edition" from SAMS publishing page 48:

There are three seperate files in the same directory,

helper.c
Code:
/*
 * helper.c - Helper code for howdy.c
 */

#include <stdio.h>
void msg(void)
{
 printf("This message sent from Jupiter.\n");
}

helper.h
Code:
/*
 * helper.h - Header for helper.c
 */

void msg(void)
howdy.c
Code:
/*
 * * howdy.c - Canonical "Hello, world!" program
 * */
#include <stdio.h>
#include "helper.h"

int main(void)
{
 printf("Hello, Linux programming world!\n");
 return 0;
}
This is compiled, according to the book,

Code:
gcc howdy.c helper.c -o howdy
But, I get the following errors:

Code:
howdy.c: In function `msg':
howdy.c:8: parse error before `{'
howdy.c:8: declaration for parameter `main' but no such parameter
howdy.c:10: number of arguments doesn't match prototype
cc1: prototype declaration
I have been through numerous C++ texts and this is the first time I have seen anything like an elementary example and explanation of how to use the preprocessor directives. And even here there is very little encouragement. For example, what is the difference between a library enclosed in '<>' and one enclosed in '""'? What is the purpose of the code in the helper.h?

Last edited by SparceMatrix; 08-26-2002 at 05:53 PM.
 
Old 08-26-2002, 07:25 PM   #2
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
the <> is compiler supplied, the "" are things you've written. in helper.h you need a semicolon, it's a function call.

it's not a function call, it's a prototype, either way it needed a semicolon. sorry if i caused any confusion.

Last edited by rshaw; 08-27-2002 at 07:15 PM.
 
Old 08-26-2002, 07:56 PM   #3
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
I can't help but believe there is a typo somewhere.

One cause of the errors (howdy.c:8) is the missing semicolon in helper.h:
Code:
/*
 * helper.h - Header for helper.c
 */
void msg(void); /* semicolon added */
After the precompiler reads in all the files to compile howdy.c you get:
Code:
(cruft from stdio.h)
void msg(void)
int main(void)
{
printf("Hello, Linux programming world!\n");
return 0;
}
Without the semicolon, the next thing the compiler expects is the open brace for the code in the msg function which accounts for part of the errors (howdy.c:8). The second part is because "main" is prototyped as "main(int, char**)" and the declaration of main() doesn't match the prototype (howdy.c:10). Older non-ansi C compilers did not require prototypes and main() worked fine. Add the semicolon and fix the "main" declaration and it should compile fine.

I don't have a clue why helper.h was included in howdy.c since the msg function was not called from howdy.c. Like I said, must be a typo somewhere. Look for an errata sheet for the book.

The use of quotes and <> symbols for include files determines the directory search order for that include file. "man cpp" and look for the -I and -I- directives for an explanation.
 
Old 08-27-2002, 12:20 AM   #4
SparceMatrix
Member
 
Registered: Aug 2002
Distribution: SME Server, CentOS
Posts: 219

Original Poster
Rep: Reputation: 30
Thanks so much all. The semicolon seems to be all that was needed. It is not there in the text and no errata either. There is something I missed, but it was functional:

Code:
/*
 * * hello2.c - Canonical "Hello, world!" program
 * */
#include <stdio.h>
#include "helper.h"

int main(void)
{
 printf("Hello, Linux programming world!\n");
 msg(); 
 return 0;
}
And so the whole point of the example is fulfilled.

Thanks again for your assistance. If I had not seen the same kind of errors in post grad mathematics text books, I would be more irate.

Last edited by SparceMatrix; 08-27-2002 at 12:21 AM.
 
Old 08-27-2002, 03:37 AM   #5
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
i think that's why the jedi philosophy works in learning programming. and books are not enuff.
one master, one apprentice.
 
  


Reply



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
Simple program wont compile BACTRATE Linux - Software 14 07-24-2008 12:57 PM
Simple Kernel Compile Question. murphaph Linux - Software 7 03-16-2004 02:49 PM
How to compile a tiny simple library by g++? Xiangbuilder Programming 13 10-20-2003 08:33 PM
simple kernel compile question Zerodark Slackware 2 09-08-2003 10:23 PM
Help with the following simple C program ..... purpleburple Programming 5 09-13-2002 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:33 AM.

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