LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with a UNIX project (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-a-unix-project-140126/)

gauge73 01-29-2004 05:33 PM

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?

frob23 01-29-2004 05:36 PM

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.

frob23 01-29-2004 05:39 PM

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.

DrOzz 01-29-2004 05:44 PM

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 :p

SciYro 01-29-2004 05:45 PM

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");
}

gauge73 01-29-2004 05:47 PM

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::operator<< <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?

gauge73 01-29-2004 05:52 PM

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. :)

DrOzz 01-29-2004 05:59 PM

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

frob23 01-29-2004 06:01 PM

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.

frob23 01-29-2004 06:03 PM

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.

gauge73 01-29-2004 06:09 PM

Where can I find some info on make? And what is a makefile, exactly? Just a .cpp file?

frob23 01-29-2004 06:13 PM

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.

gauge73 01-29-2004 09:04 PM

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.

frob23 01-29-2004 09:09 PM

#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.

gauge73 01-30-2004 12:22 PM

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.


All times are GMT -5. The time now is 09:18 PM.