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

前端 未结 16 1542
抹茶落季
抹茶落季 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:56

    I found this link with directions:

    http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/

    Basically you do:

    gcc hello.c
    ./a.out (or with the output file of the first command)
    
    0 讨论(0)
  • 2020-11-30 16:57

    Use a makefile. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.

    Read the make manual, it seems quite long at first glance but most sections you can just skim over. All in all it took me a few hours and made me much more productive.

    0 讨论(0)
  • 2020-11-30 17:00

    just enter in the directory in which your c/cpp file is.

    for compiling and running c code.

    $gcc filename.c
    $./a.out filename.c
    

    for compiling and running c++ code.

    $g++ filename.cpp
    $./a.out filename.cpp
    

    "$" is default mac terminal symbol

    0 讨论(0)
  • 2020-11-30 17:01

    A compact way to go about doing that could be:

    make foo && ./$_
    

    Nice to have a one-liner so you can just re-run your executable again easily.

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