Compiling simple Hello World program on OS X via command line

后端 未结 8 955
梦如初夏
梦如初夏 2020-12-22 16:35

I\'ve got a simple hello world example that I\'m trying to compile on OS X, named hw.cpp:

#include 
#include 
usin         


        
相关标签:
8条回答
  • Use the following for multiple .cpp files

    g++ *.cpp
    ./a.out
    
    0 讨论(0)
  • 2020-12-22 17:11

    Try

    g++ hw.cpp
    ./a.out
    

    g++ is the C++ compiler frontend to GCC.
    gcc is the C compiler frontend to GCC.

    Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.

    Though I prefer a slightly more verbose approach:

    #include <iostream>
    
    int main()
    {
        std::cout << "Hello world!" << std::endl;
    }
    
    0 讨论(0)
  • 2020-12-22 17:15

    Compiling it with gcc requires you to pass a number of command line options. Compile it with g++ instead.

    0 讨论(0)
  • 2020-12-22 17:16
    g++ hw.cpp -o hw
    ./hw
    
    0 讨论(0)
  • 2020-12-22 17:22

    The new version of this should read like so:

    xcrun g++ hw.cpp
    ./a.out
    
    0 讨论(0)
  • 2020-12-22 17:23

    Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).

    0 讨论(0)
提交回复
热议问题