-1

I'm a complete newbie to C++. I'm trying to write a simple c++ program but I got an error message. I suspect this is due to me accidentally deleting some .h files on my mac which might have ruined my Clang compiler. How can I fix this? Do I need to reinstall Xcode or change a compiler?

Error message from terminal:

    192:desktop ivanlee$ gcc test.cpp
    In file included from test.cpp:1:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:38:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
    In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iosfwd:90:
    In file included from /usr/include/wchar.h:70:
    In file included from /usr/include/_types.h:27:
    /usr/include/sys/_types.h:32:10: fatal error: 'sys/cdefs.h' file not found
        #include <sys/cdefs.h>
             ^
    1 error generated.

Code:

#include <iostream>

int main(){
    return 0;
    std::cout << "Hey";
}
TZHX
  • 5,291
  • 15
  • 47
  • 56
Lee Ivan
  • 11
  • 1
  • [Read a good book about C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – VP. Jun 08 '15 at 10:00
  • 2
    If you really did delete system header files, you're going to want to fix that first, *before* you proceed any further. – Greg Hewgill Jun 08 '15 at 10:08

3 Answers3

2

At first I ought to recommend you the definitive stackoverflow c++ books list. Read these books and get your skills grow. This also will prevent questions like this.

Your question needs very-very basic knowledges and answers may be too long and your problem can be solved by many different methods.

I can tell you one but you should not ask questions like this.

Answer

Your code contains a mistakes:

  1. return 0 before your other instructions (it should be the last). Now your program will just do nothing.

  2. You should always compile C++ code with C++ compiler. gcc is not a c++ compiler but c compiler - use g++ instead.

  3. Even if you correct 2 errors above, your std::cout call may fail because it does not flush the stream. You should also add << std::endl to this call.

Community
  • 1
  • 1
VP.
  • 15,509
  • 17
  • 91
  • 161
0

Execute

g++ test.cpp -o out

Instead of

gcc text.cpp
Hassaan Salik
  • 413
  • 4
  • 22
0
run Terminal
execute gcc -v

Read the info and copy the include path. Copy it to your IDE that allows you to add the include path.

Mine, for an example, is:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1

Draken
  • 3,134
  • 13
  • 34
  • 54
Eman Jayme
  • 230
  • 2
  • 8