trouble compiling c++ code

Discussion in 'Mac Programming' started by bbodzin, Nov 29, 2008.

  1. bbodzin macrumors newbie

    Joined:
    Nov 29, 2008
    #1
    I have an assignment for a class I am currently taking to write a computer program. I have not written a computer program in 25 years. However, I did find the C++ compiler on my mac, but I cannot get even a simple sample program to compile because the compiler cannot find the following files:
    stddef.h, stdio.h, gthr-default.h. Is there some specific #include I need in my sample program in addition to what the book says, or is there some option to the compiler that I need?. . . Or something? :confused:

    Please help.

    Thanks
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Let's start with the basics:
    Can you post at least the relevant portion(s) of your code?
    What is the extension of your source file(s)? .cpp? .cxx?
    What compiler and in what environment? g++ from the commandline? From Xcode? Something else?
    In a similar environment, can you compile a .c C program?

    Let us know and we will see what we can do to help.

    -Lee
     
  3. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #3
    Thanks for the help

    The only way I could figure out to invoke the compiler was to type "gcc3" in the terminal window. Is there another way? My code is VERY simple. I am having much more trouble with the environment than with actually coding. This is my entire sample test program. It is a cpp file.

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    using namespace std;

    int main ()
    {
    int celsius;
    cout <<"Enter the temperature in celsius:";
    cin >> celsius;

    int fahrenheit;
    fahrenheit = 9 * celsius/5 + 32;

    cout << "fahrenheit value is:";
    cout << fahrenheit << endl;
    }
     
  4. menthol moose macrumors regular

    Joined:
    Jul 20, 2006
    #4
    You don't need these lines:

    Code:
    #include <cstdio>
    #include <cstdlib>
    
    You only need those if you want to use the C I/O functions in C++, and you are not, so they are unneccessary.

    I tested your program without those two lines on my Macbook and in functions perfectly. Make sure you're using g++ instead of gcc. I had a bunch of errors and found out that was why.
     
  5. MacRumors Guy macrumors member

    Joined:
    Sep 17, 2008
    #5
    If you compile C++ with gcc you need to manually indicate to link against the C++ libraries. This is not need if you use g++.

    You only need the iostream import btw.
     
  6. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #6
    g++ no better

    Well, I typed "c++ FILENAME" and got what seem to be the same errors. There are three errors that say "No such file or directory".

    /usr/include/gcc/darwin/3.1/g++-v3/cstddef:48:20: stddef.h: No such file or directory
    /usr/include/gcc/darwin/3.1/g++-v3/cstdio:52:19: stdio.h: No such file or directory
    /usr/include/gcc/darwin/3.1/g++-v3/ppc-darwin/bits/gthr-default.h:37:21: pthread.h: No such file or directory

    It appears that the include files I have referenced are trying to invoke other include files and those are not being found. When I look under /usr/include for files named stddef.h or stdio.h, I find they are not there. In fact the command (from a "terminal" window)

    find / -name stdio.h -print

    produces only error messages, so there is no stdio.h anywhere on this system.

    Could it be that there is a space in the filename? The "developer tool" wrote the .cpp file and, given the window environment of the developer tool, let me put a space in the filename. I doubt this is the problem, but it is so strange that I am grabbing at straws here.
     
  7. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #7
    what does that mean?

    Well, I don't need to use any features of c++ that are not in "standard c", so the distinction between g++ and gpp is beyond me. In fact, if there wer a plain C compiler available, it would probably be easier for me to use it, rather than one that compiles a c++ program. the calls to cin and cout could esaily be replaced with standard C i/o functions.

    By the way, I commented out all the include statements but the one for iostream and the same errors came out. Still looking for those three .h files that were not found.
     
  8. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #8
    Actually, you are trying to compile a c++ program - your header imports, namespace declaration and input and output statements are c++. Anyway, gcc is a standard C compiler

    What I find interesting is the missing files are being looked for in the gcc 3.1 directories - what version of XCode do you have installed? What does typing "gcc -version" show?

    I have XCode 3 and I have only gcc 4 installed.
     
  9. autorelease macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #9
    Just use the 'g++' command to compile your code instead of 'gcc3.'
     
  10. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #10
    huh?

    Well, invoking "gcc -version" gives the error message:

    admin% gcc -version
    gcc: unrecognized option `-version'
    gcc: no input files

    If this is not the answer to your question about XCode, please tell me how to answer the question and I will try it.

    One more thing ... /usr/bin/gcc is a symbolic link to gcc3. To me that means that invoking gcc or gcc3 runs the same program, just with a different effective name.

    I wasn't very clear before, about being OK with standard C, instead of C++
    What I meant was that the program I need to write for this assignment does not need any Object Oriented features of c++, just simple I/O that is in any language. The sample program I showed above is a much simpler program that I am using just to see how to compile things on this machine. Once I get a clean compile of the test program, I will type in my more complicated program and see what work needs to be done. Until I can compile a simple program, there is no sense in typing in the longer one.

    By the way, this is a Mac tower running Mac OS 10.4.8. And I don't know what an XCode is.
     
  11. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #11
    standard C no better

    Ok ... I removed all the cin and cout statements and replaced them with "standard C" calls to printf and scanf. I have removed all the include statements except the one for iostream and then put one in for stdio.h. Here is the source program now:

    admin% cat try.cpp

    #include <iostream>
    #include "stdio.h"

    int main ()
    {
    int celsius;
    printf ("Enter the temperature in celsius:");
    scanf ( "%d", celsius);

    int fahrenheit;
    fahrenheit = 9 * celsius/5 + 32;

    printf ( "fahrenheit value is: %d\n, farenheit);
    }

    when i invoke "gcc try.cpp", I get a similar stream of error messages, included in which are three "No such file ..." errors, which appear to be the heart of the problem:


    stddef.h: No such file or directory
    /usr/include/gcc/darwin/3.1/g++-v3/cstdio:52:19: stdio.h: No such file or directory
    /usr/include/gcc/darwin/3.1/g++-v3/ppc-darwin/bits/gthr-default.h:37:21: pthread.h: No such file or directory

    Clearly, my new #include line for "stdio.h" did not work. And my error message that refers to stdio has changed, too.

    Here is another news flash. I removed the #include line for <iostream> and most of the error messages went away. Of course, it still could not find stdio.h, since that does not appear to exist ANYWHERE on this machine, and so printf and scanf are not defined, but those are clearly the result of not finding stdio.h so those errors are understandable.

    I would be satisfied if I could compile this program (and, therefore, my more complicated program) in the standard C environment or in the C++ environment. Either one would be OK, as long as it works. I would change the I/O lines to match the environment that works.
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    Try stdio.h in chevrons instead of quotes and see what you get.

    -Lee

    Edit: also, if you are compiling C, use the .c extension, not .cpp.
     
  13. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #13
    Where did you get gcc from? It doesn't come on Macs automatically. You have to install it with the XCode developer tools that come on your OS X install disk. So you must have installed XCode.

    XCode 2 is the version that works on Tiger, but as far as I remember, it came with gcc 4.

    Since no one else has any problems compiling this program, the problem's with your Mac. I suggest you make sure your development environment is as up to date as possible by (re)installing XCode from your OS X Tiger install disk.

    If you don't have XCode, or it's a version earlier than 2.2, try signing up for a free Apple Developer account at developer.apple.com and see if you can download XCode 2.

    Edit: actually, look in /Developer/Applications and run XCode from there, assuming you have it, which really you should if you have gcc. Look at the About XCode from XCode in the top menu.
     
  14. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #14
    When we bought this machine, several years ago, a lot of software was "preloaded" for us by the vendor, who went out of business shortly afterwards and so was unavailable to ask any more questions of. There is a lot of stuff on this machine that came from sources we cannot identify. We have loaded an OS upgrade since, to get to 10.4.8, and I imagine that the gcc and g++ stuff, being under /usr/bin (in the Unix sense), came with that upgrade. But there is no way to know for sure.

    Anyhow, I went to the developer site you suggested and downloaded a new version of XCode yesterday and now it seems to work fine. Thank you very much for the suggestion. We have made great progress on getting the code to work, now that we can edit, compile and run it. :):)
     
  15. bbodzin thread starter macrumors newbie

    Joined:
    Nov 29, 2008
    #15
    Thanks for the suggestion, but it doesn't help.

    The only difference between a #include statement that uses quotes around the name and one that uses "chevrons" around the name is whether the preproccessor looks first in the current dir for the named file. With quotes it does look in the current dir first, then the other dir(s). With chevrons it skips the current dir search and just looks in the other place(s).

    I believe the definition of "other place(s)" is up to the compiler and can be overridden with a command line option.

    Since we have no include files in our own directories, the difference is moot.
     
  16. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  17. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #17
    Remove the #include <iostream> too. And, if you really want to check your set-up, rename the program to try.c.

    The compiler may try to be "smart" and still decide to compile your program as C++ based on the extension. On the other hand, if it is compiling it as C, your including <iostream> will probably lead to a lot of errors since that header file is everything but C (and may try to include other C++ headers in turn, leading to further "file not found" errors).

    Try the simple "hello world" program first.

    Also, it's gcc --version (note the two dashes).

    Good luck!
     

Share This Page