Windows命令行编译Haskell简单程序(无需Cygwin)
Compiling Haskell Programs on Windows Without Cygwin
Hey there! I totally get the hassle of restricted university machines—let’s walk through how you can compile simple Haskell programs using native Windows tools that don’t require Cygwin or admin approval.
First, Check if GHC is Already Available
Many university lab machines come pre-installed with the Glasgow Haskell Compiler (GHC). To confirm:
- Open Command Prompt (CMD) or PowerShell
- Run
ghc --version
If you see a version number pop up, you’re all set! Skip straight to the compiling steps below.
If GHC Isn’t Installed: Use a Portable Build
If GHC isn’t present, you can use a portable (no-install) version that runs from a USB drive or your user folder—no admin rights needed:
- Grab a portable GHC archive (look for builds labeled "Windows x86_64" that don’t mention Cygwin)
- Extract it to a folder you have access to (like your user documents or a USB drive)
- Temporarily add the
binfolder from the extracted archive to your PATH:- In CMD:
set PATH=%PATH%;C:\path\to\your\ghc\bin - In PowerShell:
$env:PATH += ";C:\path\to\your\ghc\bin"
Now you can useghcandrunhaskellcommands just like a regular installation.
- In CMD:
Compiling & Running a Simple Haskell Program
Let’s use a basic "Hello World" example to test:
- Create a new text file named
hello.hswith this content:
main = putStrLn "Hello, Haskell!"
- Navigate to the folder containing
hello.hsin your command line - Option 1: Compile to an executable
Runghc hello.hs
This generates three files:hello.exe,hello.o, andhello.hi. Run the executable withhello.exe - Option 2: Run directly without compiling
For quick testing, userunhaskell hello.hs
This executes the code immediately without creating an executable file - Bonus: Run one-liners directly
You don’t even need a file for super simple code! Try:ghc -e "putStrLn \"Hello from the command line!\""
Key Notes
- Portable GHC builds don’t modify your system, so they’re safe to use on restricted machines
- If your university blocks downloading executable files, check if your department’s IT team can provide a pre-approved copy of GHC
- Stick strictly to native Windows builds of GHC—avoid any tools that mention "Cygwin" or "MSYS"
内容的提问来源于stack exchange,提问作者user65526
相关产品推荐
相关产品推荐

