LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2004, 06:33 PM   #1
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Rep: Reputation: 30
Need help with a UNIX project


I'm taking an operating systems class, and I've been our of school for a couple years now. I'm having to do a project in C on a UNIX system, and I really don't remember jack about programming in C. To compound the issue, I've never programmed C on a UNIX machine. I'm having some really, really basic issues here and I'm sure that I'm just making a pathetically stupid mistake...

Here is my test program...

#include</usr/include/c++/3.2.2/backward/iostream.h>

void main(void)
{cout<<"Hello World!";}


Here are the errors I'm getting...

$ gcc test.c
In file included from test.c:1:
/usr/include/c++/3.2.2/backward/iostream.h:32:20: iostream: No such file or directory
/usr/include/c++/3.2.2/backward/iostream.h:34: parse error before "std"
test.c: In function `main':
test.c:5: `cout' undeclared (first use in this function)
test.c:5: (Each undeclared identifier is reported only once
test.c:5: for each function it appears in.)
test.c:4: warning: return type of `main' is not `int'


What's also confusing me is that there are two copies of iostream.h on my system and they give me different errors when included. Here are the directories they are located in...

/usr/include/c++/3.2.2/backward
/usr/include/g++-3


And finally, I have to turn in my project as a "makefile." I've never messed with make except to compile things I'm installing. Can anyone point me to some info on make?
 
Old 01-29-2004, 06:36 PM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Have you tried just:
#include <iostream.h>

That should use the correct one. You should make the main() an int function because unix expects it to return an int. Example:

int main(void) or the more common int main(int argc, char *argv[])

Edit: Removed poor assumption. Corrected response below.

Last edited by frob23; 01-29-2004 at 06:42 PM.
 
Old 01-29-2004, 06:39 PM   #3
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Also, change the file name to test.cpp so gcc knows it is a c++ file.

As a note: you don't want to give a specific path for includes unless it is necessary because it ruins portability.
 
Old 01-29-2004, 06:44 PM   #4
DrOzz
Senior Member
 
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 59
this should do the trick :
Code:
#include <iostream.h>

int main ()
{
  cout << "Hello World!";
  return 0;
}
and then :
g++ -o test test.cpp


p.s. just for a future recommendation you should post questions like these in the programming fourm, it'll benefit you in the end

Last edited by DrOzz; 01-29-2004 at 06:50 PM.
 
Old 01-29-2004, 06:45 PM   #5
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
u say that your your trying to program in c but that is a c++ program in c it would be more like
#include <stdio.h>

main() {
printf("hello world");
}
 
Old 01-29-2004, 06:47 PM   #6
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
Okay, here's what I've done per your suggestions so far... I changed the filename to test.cpp. I changed the include to just iostream.h. This didn't work before, but since I changed the filename of test.c to test.cpp, it seemed to find the correct file (/us/include/c++/3.2.2/backward/iostream.h). I also changed the return of main to an int and added a return(1); line at the end of the main function. Now I get the following errors...

$ gcc test.cpp
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
from test.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccFNt7WS.o(.text+0x19): In function `main':
: undefined reference to `std::cout'
/tmp/ccFNt7WS.o(.text+0x1e): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccFNt7WS.o(.text+0x4a): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccFNt7WS.o(.text+0x79): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccFNt7WS.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status


Further recommendations?
 
Old 01-29-2004, 06:52 PM   #7
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
I tried compiling with this command and all I got was a warning, so it worked...

g++ -o test test.cpp

Why didn't this one work?

gcc test.cpp

Is there a difference between gcc and g++? Is it required that you indicate what the output file is?

Thanks so much for the help.
 
Old 01-29-2004, 06:59 PM   #8
DrOzz
Senior Member
 
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 59
no it is not required to specify an output file, so if you simply type:
g++ test.cpp
then the output file will be named :
a.out
 
Old 01-29-2004, 07:01 PM   #9
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
gcc assumes preprocessed (.i) files are C and assumes C style link-
ing.

g++ assumes preprocessed (.i) files are C++ and assumes C++ style
linking.

They are mostly the same program it just depends on how they make assumption. Usually when I have just one source file I type `make test` or whatever and make will use the right one. Sometimes I get away with using gcc for c++ programs... but yeah g++ is the correct program to call.
 
Old 01-29-2004, 07:03 PM   #10
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
Note: Typing `make test` will automatically make sure the output is test... instead of a.out. I don't know how big this project, you are going to be doing, will be but you might want to look into how to use make. Also, if you really want to take the easy road... use kdevelop -- It is the only program I use from the KDE project but it is very nice.

Edit: try to make it readable.

Last edited by frob23; 01-29-2004 at 07:06 PM.
 
Old 01-29-2004, 07:09 PM   #11
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
Where can I find some info on make? And what is a makefile, exactly? Just a .cpp file?
 
Old 01-29-2004, 07:13 PM   #12
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
try `info make.info` It will tell you all about it.

A Makefile is just a set definition telling make how to build your program. It is really nice, especially since it can be setup to only recompile the stuff that has changed and you will only have to type one thing to rebuild you program. Imagine you have 10 cpp files.

g++ -o project file1.cpp file2.cpp file3.cpp file4.cpp file5.cpp file6.cpp file7.cpp file8.cpp file9.cpp file10.cpp

Everytime you wanted to test it. Instead of make. It also does lots of other stuff. But that is just one simple example.
 
Old 01-29-2004, 10:04 PM   #13
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
Okay, more questions...

My project is supposed to be using system calls (i.e. fork(), exec(), etc.). How do I use these in C++? Is there a header file I need to be including? I get errors when I try to use them in a program.
 
Old 01-29-2004, 10:09 PM   #14
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: OpenBSD, Ubuntu, FreeBSD
Posts: 1,449

Rep: Reputation: 48
#include <sys/types.h>
#include <unistd.h>

That should do it. I believe those two include files should have everything you need. I know they define fork and exec -- at the very least. I believe they have the rest of the system calls.
 
Old 01-30-2004, 01:22 PM   #15
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
Okay, here are a few more questions for you programming wizards... I have to write a program that will create a child process with the fork() command. I've successfully done that, but here's what I don't know how to do:

1) How do I identify the parent's process id (PPID) in the child process?
2) How do I register which function will be my signal handler?
3) How do I create pipes between the two processes? Can I create them before the fork and then have them be inherited?
4) How can I run a shell command from the C++ program, and how can I get the output of that command to be handled by the C++ program instead of being output to the screen?


Thanks again, guys. I really appreciate the assistance.

Last edited by gauge73; 01-30-2004 at 01:23 PM.
 
  


Reply


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
UNIX (Linux, BSD, etc) Programming :: UNIX kuphryn Programming 8 04-05-2004 12:50 AM
Beginning a big project - Need an Good Project Manager gamehack Programming 3 01-15-2004 12:49 PM
Why did you experienced users of Unix change to unix over Windows? Laptop2250 Linux - General 11 10-28-2003 12:51 PM
Cannot see Open GL project in KDevelop project wizard SparceMatrix Programming 2 08-08-2002 12:14 AM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 07:45 AM


All times are GMT -5. The time now is 04:49 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration