Racket语言生成的.exe文件无法正常打开问题求助
The core issue here is that when running your code in the Racket/DrRacket IDE, the REPL keeps the process alive and automatically renders your pict3d scene. But when packaged as an executable, the program runs through your code once, creates the scene, then immediately exits—without waiting for you to close the window.
Here's how to fix this:
Step 1: Modify Your Code to Keep the Window Open
You need to explicitly tell your program to display the scene and wait for the window to close before exiting. The simplest way is to use the run-pict3d function, which handles rendering the scene and running the event loop until you close the window.
Here's the updated working code:
#lang racket (require pict3d) (run-pict3d (combine (with-color (rgba "red") (sphere (pos 1 0 0) 1/10)) (with-color (rgba "blue") (sphere (pos 0 1 1) 1/10)) (for/list ([t (in-range 1/10 1 1/10)]) (define v (pos-between (pos 1 0 0) (pos 0 1 1) t)) (sphere v 1/20))))
If you want more control over window management, you can split the logic into separate steps:
#lang racket (require pict3d) ; Define your 3D scene (define my-scene (combine (with-color (rgba "red") (sphere (pos 1 0 0) 1/10)) (with-color (rgba "blue") (sphere (pos 0 1 1) 1/10)) (for/list ([t (in-range 1/10 1 1/10)]) (define v (pos-between (pos 1 0 0) (pos 0 1 1) t)) (sphere v 1/20)))) ; Show the scene and get a handle to the window (define scene-window (show-pict3d my-scene)) ; Wait until the window is closed before exiting the program (wait-for-event scene-window 'close)
Step 2: Package Correctly with raco exe
Instead of relying on GRacket's export feature, use Racket's official command-line tool for reliable packaging:
- Save your updated code to a file (e.g.,
3d-scene.rkt). - Open a command prompt/terminal, navigate to the file's directory.
- Run this command to create an executable:
raco exe 3d-scene.rkt - For a standalone executable that doesn't require Racket to be installed on the target machine, use:
This creates a folderraco distribute 3d-scene-dist 3d-scene.rkt3d-scene-distwith your executable and all required dependencies bundled in.
Why This Works
run-pict3dstarts the pict3d event loop, which keeps the program running as long as the window is open.raco exeensures all necessary libraries (including pict3d) are properly bundled into the executable, avoiding missing dependency issues that could cause crashes or premature exits.
内容的提问来源于stack exchange,提问作者Alik

