Skip to content

Getting Your C Compiler Installed

Brian "Beej Jorgensen" Hall edited this page Feb 9, 2018 · 39 revisions

Instructions and pitfalls in getting your C compiler installed.

General Build Instructions

To build your C program from the command line (Bash shell or Terminal):

gcc -Wall -Wextra -o hello hello.c

General Pitfalls

  • If you accidentally tell gcc to overwrite your .c file with the -o (output) flag, you'll get errors. And your source code will be overwritten!

    # WRONG!!
    gcc -Wall -Wextra -o foo.c foo
    
    # RIGHT!!
    gcc -Wall -Wextra -o foo foo.c
    
  • If you're on Windows and get errors about WinMain, make sure you have a main() function in your code! There are also other reasons you might see this error. Read on, below.

macos

Option A: Command Line Tools

  • Open a Terminal.
  • Run xcode-select --install.
  • When prompted to install the developer command line tools, click Install.

Option B: Full XCode install

If you're going to install XCode anyway for some other reason, you can use these instructions:

  • Install XCode from the AppStore.
  • Open a Terminal.
  • Run gcc.
  • When prompted to install the developer command line tools, click Install.

Windows

The two options for Windows that we know of for building the Unix-like code we play with at Lambda School are WSL and Cygwin. MinGW is too minimalist for our use.

WSL

The Windows Subsystem for Linux is an officially-supported binary compatibility layer for Linux apps running on Windows.

This basically gives you a mini Linux install. Ubuntu is recommended, but not required.

Requires Windows 10.

  • Install WSL
    • Check your build number. If it is pre-16215, you'll need to turn on developer mode, as per the above instructions.
  • Run bash from the command prompt or start menu.
  • Run sudo apt install build-essential to install GCC.

Drive Access

  • To get to your Windows drive from Bash, type:
    cd /mnt/c/Users/YourUserName
    
  • There is no supported way to access your WSL drive from Windows command prompt. Don't do it.

VSCode and WSL

  • Keep your program files on your Windows drive.
  • Run VSCode as normal.
  • You can bring up a bash shell then switch to your Windows drive, as outlined above. Then run gcc or whatever you need to.
  • Unrelated to C programming, VSCode has some integrated WSL support for running the node debugger. See launching WSL node from the VSCode debugger.

Cygwin

Cygwin is a library compatibility layer for building and running Linux apps.

  • Install Cygwin.
  • Launch a Windows command prompt.
  • Install the necessary packages by running the Cygwin Setup utility.
    setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel
    
  • Launch Cygwin-Terminal from its icon.

Drive Access

  • To get to your Windows drive from Bash, type:

    cd /cygdrive/c/Users/YourUserName
    

    or

    cd c:/Users/YourUserName
    
  • To get to your Cygwin drive from Windows command prompt, type:

    cd c:\cygwin\home\youruser
    

VSCode and Cygwin

  • Keep your files either on your Windows drive or Cygwin drive. Access them as per above.
  • Run VSCode as normal.
  • Run gcc from the Cygwin Terminal bash shell.

Cygwin Pitfalls

  • If you have MinGW also installed, make sure that the Cygwin binaries are first in your PATH. If not, it will run the wrong GCC on the command line and you'll receive an error about WinMain not being found. Uninstall MinGW, per below.

  • Another way to get an error about WinMain not being found is if you accidentally specify your source .c program as the output file with -o. See General Pitfalls, above.

MinGW

MinGW does not support fork(), so will not work for LS classes.

Having a MinGW install can interfere with the workings of Cygwin. You might get errors related to an undefined WinMain. Here are instructions for uninstalling MinGW.

Linux/Unix-likes

Google for specific instructions using your distribution name, e.g. ubuntu gcc install.