简体   繁体   中英

C++ #include <iostream>

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

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.

Execute

g++ test.cpp -o out

Instead of

gcc text.cpp
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM