How to compile and run C/C++ in a Unix console/Mac terminal?

前端 未结 16 1541
抹茶落季
抹茶落季 2020-11-30 16:32

How can I compile/run C or C++ in Unix console or a Mac terminal?

(I know it, forget it, and relearn it again. Time to write it down.)

相关标签:
16条回答
  • 2020-11-30 16:49

    To compile C or C++ programs, there is a common command:

    1. make filename

    2. ./filename

    make will build your source file into an executable file with the same name. But if you want to use the standard way, You could use the gcc compiler to build C programs & g++ for c++

    For C:

    gcc filename.c
    
    ./a.out
    

    For C++:

    g++ filename.cpp
    
    ./a.out
    
    0 讨论(0)
  • 2020-11-30 16:53

    Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)

    - Wall -pedantic -Weffc++ -Werror
    
    0 讨论(0)
  • 2020-11-30 16:55

    This is the command that works on all Unix machines... I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.

    $ g++ -o lab21 iterative.cpp
    

    -o is the letter O not zero

    lab21 will be your executable file

    iterative.cpp is your c++ file

    After you run that command type the following in terminal to run your program:

    $ ./lab21
    
    0 讨论(0)
  • 2020-11-30 16:55

    Two steps for me:

    first:

    make foo
    

    then:

    ./foo
    
    0 讨论(0)
  • 2020-11-30 16:55

    In order to compile and run a cpp source code from Mac terminal one needs to do the following:

    1. If the path of cpp file is somePath/fileName.cpp, first go the directory with path somePath
    2. To compile fileName.cpp, type c++ fileName.cpp -o fileName
    3. To run the program, type ./fileName
    0 讨论(0)
  • 2020-11-30 16:56
    gcc main.cpp -o main.out  
    ./main.out
    
    0 讨论(0)
提交回复
热议问题