You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. Grab a portable GHC archive (look for builds labeled "Windows x86_64" that don’t mention Cygwin)
  2. Extract it to a folder you have access to (like your user documents or a USB drive)
  3. Temporarily add the bin folder 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 use ghc and runhaskell commands just like a regular installation.

Compiling & Running a Simple Haskell Program

Let’s use a basic "Hello World" example to test:

  1. Create a new text file named hello.hs with this content:
main = putStrLn "Hello, Haskell!"
  1. Navigate to the folder containing hello.hs in your command line
  2. Option 1: Compile to an executable
    Run ghc hello.hs
    This generates three files: hello.exe, hello.o, and hello.hi. Run the executable with hello.exe
  3. Option 2: Run directly without compiling
    For quick testing, use runhaskell hello.hs
    This executes the code immediately without creating an executable file
  4. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:53:21