Compiling Common Lisp to an executable - build

I recently started learning Common Lisp using SBCL. How can I compile my Lisp programs into a Windows binary?

Making hello.exe:
* (defun main () (print "hello"))
MAIN
* (sb-ext:save-lisp-and-die "hello.exe" :toplevel #'main :executable t)
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into hello.exe:
writing 3160 bytes from the read-only space at 0x22000000
writing 2592 bytes from the static space at 0x22100000
writing 30134272 bytes from the dynamic space at 0x22300000
done]
> dir hello.exe
31,457,304 hello.exe
> hello.exe
"hello"
31 MB executable file!

Use SB-EXT:SAVE-LISP-AND-DIE. See http://www.sbcl.org/manual/#Saving-a-Core-Image for details.

There're several tools to do it. You can start with Buildapp.

(defun main ()
(format t "Hello, world!~%"))
(sb-ext:save-lisp-and-die "hello-world.exe"
:executable t
:toplevel 'main)

Related

Browsing through previous-compilation errors during a new compilation?

How to setup emacs so that I can browse through previous-compilation errors during new compilation?
Two things don't work for me:
M-g M-g (next-error) function is not working when second compilation is in-progress.
I have my emacs split into 5 uneven windows (split-windows-horizontally), the compilation "window" is double the size (dbl monitor setup). When I launch compilation it always appeared in the last double compilation window. Now it opens a new window for itself.
Here is a solution which seems to fulfill all your requirements:
the *compilation-old* buffer always stays in the same window
next-error does not break
all successive compilation outputs are appended at the end of *compilation-old* when the compilation process terminates
(defun my-compilation-finish-function (buffer msg)
;; Don't do anything if we are in a derived mode
(when (with-current-buffer buffer (eq major-mode 'compilation-mode))
;; Insert the last compilation output at the end of *compilation-old*
(if (get-buffer "*compilation-old*")
(with-current-buffer "*compilation-old*"
(save-excursion
(goto-char (point-max))
(insert-buffer buffer)))
(with-current-buffer buffer
(rename-buffer "*compilation-old*")))))
(add-hook 'compilation-finish-functions 'my-compilation-finish-function)
(defadvice compile (around my-compile-show-old activate)
"Show the *compilation-old* buffer after starting the compilation"
(let ((buffer (current-buffer)))
(when (get-buffer "*compilation-old*")
(pop-to-buffer "*compilation-old*")
(switch-to-buffer "*compilation*"))
ad-do-it
(when (get-buffer "*compilation-old*")
(switch-to-buffer "*compilation-old*")
(pop-to-buffer buffer))))
Putting the following in your init file will rename the compilation buffer to *compilation-old* when the compile command terminates.
Please note that this will not work if you run the new compilation process from the old compilation buffer (since compile will in this case reuse the buffer instead of creating a new one)
(defun my-rename-compilation-buffer (buffer message)
;; Don't do anything if we are in a derived mode
(when (with-current-buffer buffer (eq major-mode 'compilation-mode))
(let* ((old-compilation-buffer-name "*compilation-old*")
(old-compilation-buffer (get-buffer old-compilation-buffer-name)))
;; Kill old compilation buffer if necessary
(when old-compilation-buffer
(kill-buffer old-compilation-buffer))
;; Rename the current compilation buffer
(with-current-buffer buffer
(rename-buffer old-compilation-buffer-name)))))
(add-hook 'compilation-finish-functions 'my-rename-compilation-buffer)
It's a bit of a Kludge, but try this:
Before starting the new compilation, save (write, C-x C-w) the current compilation buffer to a file. If the buffer for the new file looses it "compilation-mode" setting, simply turn compilation-mode back on (M-x compilation-mode).

How to load cedet, semantic et. al only when .cxx,.h .cpp files are open

Because I use Emacs for many things these days I would like to only load cedet.el when I open a c/C++ source or header and not everytime emacs starts since it takes a significant toll on the startup time.
Right now the beginning of my init file looks like this:
(load-file "~/.emacs.d/plugins/cedet/common/cedet.el")
(semantic-load-enable-excessive-code-helpers)
;;(semantic-load-enable-semantic-debugging-helpers)
(setq senator-minor-mode-name "SN")
(setq semantic-imenu-auto-rebuild-directory-indexes nil)
(global-srecode-minor-mode 1)
(global-semantic-mru-bookmark-mode 1)
And it keeps going.
Is there a way to do this?
my emacs startup improved dramatically after i learned to use eval-after-load and autoload.
if you have a mode you only want loaded when you open a file of the type, add something like this to your .emacs (assuming foo-mode is defined in foo-mode.el on your load path):
(autoload 'foo-mode "foo-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.foo\\'" . foo-mode))
if you have some helper libraries which you only want loaded after you load a "main" library, add something like this to your .emacs (assuming bar-mode is a secondary mode which enhances foo-mode):
(eval-after-load "foo-mode"
'(progn
(require 'bar-mode)
;; ... do other bar-mode setup here ...
))
so, in your case, you probably want to setup cedet using eval-after-load c++-mode.
You could do it like this:
(add-hook 'c-mode-common-hook (lambda ()
(load-file "~/.emacs.d/plugins/cedet/common/cedet.el")
;; any code dependent on having this file loaded
))
If loading the file (or doing the other commands) several times is a problem, you should of course first check whether this file was already loaded (either test for something defined in cedet.el, or maintain an is-loaded flag yourself).
Edit: Such a flag might look like this:
(setq need-to-load-cedet-p t)
(add-hook 'c-mode-common-hook (lambda ()
(if need-to-load-cedet-p
(progn (load-file "~/.emacs.d/plugins/cedet/common/cedet.el")
(setq need-to-load-cedet-p nil))
;; code that should only be executed once after cedet is loaded goes here
)
;; code that should be executed every time a new buffer is opened goes here
))

Emacs matlab mode key binding for running tests

I am using Emacs + matlab-mode as my Matlab development environment. I also have MTEST installed together with Matlab to run my unit tests - what I want to do now is to have a key binding that runs the tests from the current file in the matlab-shell I constantly have opened around (M-x matlab-shell).
What I have until now is:
; Runs the unit tests available in the current buffer
(defun run-matlab-test ()
(interactive)
(matlab-shell-run-command (concat "runtests "
(car (split-string (buffer-name) "\\.")))))
; Bind "C-c l" to running unit tests in matlab-mode
(defun map-run-matlab-test-keys ()
(local-set-key (kbd "C-c l") 'run-matlab-test))
(add-hook 'matlab-mode-hook 'map-run-matlab-test-keys)
What I need to do is in the run-matlab-test function to have a way of calling the runtests command with the parameter provided by the (buffer-name) command and all this should happen in the matlab shell I mentioned above. Any hints ?
Edit: I managed to get it working by calling matlab-shell-run-command. The caveat here is that it only works if the starting sequence is: open your unit-test.m file, from that file run M-x matlab-shell (this way matlab starts with the current working directory in the tests directory) and then you can use the above binding.
To avoid your caveat above, you could probably issue a cd to matlab, before calling runtest, by doing something like the following (untested):
(defun run-matlab-test ()
(interactive)
(matlab-shell-run-command (concat "cd " (file-name-directory (buffer-file-name))))
(matlab-shell-run-command (concat "runtests "
(car (split-string (buffer-name) "\\.")))))

Clojure (read-line) doesn't wait for input

I am writing a text game in Clojure. I want the player to type lines at the console, and the game to respond on a line-by-line basis.
Research showed me that (read-line) is the way one is meant to get text lines from standard input in Clojure, but it is not working for me.
I am in a fresh Leiningen project, and I have added a :main clause to the project.clj pointing to the only source file:
(ns textgame.core)
(defn -main [& args]
(println "Entering -main")
; (flush) ;makes no difference if flush are commented out
(let [input (read-line)]
(println "ECHO:" input))
; (flush)
(println "Exiting -main"))
using lein run yields:
Entering -main
ECHO: nil
Exiting -main
In other words, there is no opportunity to enter text at the console for (read-line) to read.
How should I get Clojure to wait for characters and newline to be entered and return the corresponding string?
(I am using GNOME Terminal 2.32.1 on Linux Mint 11, Leiningen 1.6.1.1 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM, Clojure version 1.2.1.)
Update: If I run lein repl, I can (println (read-line)), but not when I have a -main function and run using lein run.
Try "lein trampoline run". See http://groups.google.com/group/leiningen/browse_thread/thread/a07a7f10edb77c9b for more details also from https://github.com/technomancy/leiningen:
Q: I don't have access to stdin inside my project.
A: There's a problem in the library that Leiningen uses to spawn new processes that blocks access to console input. This means that functions like read-line will not work as expected in most contexts, though the repl task necessarily includes a workaround. You can also use the trampoline task to launch your project's JVM after Leiningen's has exited rather than launching it as a subprocess.
I have had similar problems and resorted to building a jar file and then running that.
lein uberjar
java -jar project-standalone.jar
It's a bit slower, though it got me unstuck. An answer that works from the repl would
be better
Wrap your read-line calls with the macro with-read-line-support which is now in ns swank.core [since swank-clojure 1.4+ I believe]:
(use 'swank.core)
(with-read-line-support
(println "a line from Emacs:" (read-line)))
Thanks to Tavis Judd for the fix.
You can use read and use a string as input.
Not sure about the lein aspects of the problem, but definitely in emacs it is impossible to make stdin work. However, if you want to get text from the user, you can easily do it using a JOptionPane like this code from my little tic-tac-toe program:
(defn get-input []
(let [input (JOptionPane/showInputDialog "Enter your next move (row/column)")]
(map #(Integer/valueOf %) (.split input "/"))))

SML-NJ, how to compile standalone executable

I start to learn Standard ML, and now I try to use Standard ML of New Jersey compiler.
Now I can use interactive loop, but how I can compile source file to standalone executable?
In C, for example, one can just write
$ gcc hello_world.c -o helloworld
and then run helloworld binary.
I read documentation for SML NJ Compilation Manager, but it don`t have any clear examples.
Also, is there another SML compiler (which allow standalone binary creating) available?
Both MosML and MLton also have the posibility to create standalone binary files. MosML through mosmlc command and MLton through the mlton command.
Note that MLton doesn't have an interactive loop but is a whole-program optimising compiler. Which in basic means that it takes quite some time to compile but in turn it generates incredibly fast SML programs.
For SML/NJ you can use the CM.mk_standalone function, but this is not advised in the CM User Manual page 45. Instead they recommend that you use the ml-build command. This will generate a SML/NJ heap image. The heap image must be run with the #SMLload parameter, or you can use the heap2exec program, granted that you have a supported system. If you don't then I would suggest that you use MLton instead.
The following can be used to generate a valid SML/NJ heap image:
test.cm:
Group is
test.sml
$/basis.cm
test.sml:
structure Test =
struct
fun main (prog_name, args) =
let
val _ = print ("Program name: " ^ prog_name ^ "\n")
val _ = print "Arguments:\n"
val _ = map (fn s => print ("\t" ^ s ^ "\n")) args
in
1
end
end
And to generate the heap image you can use: ml-build test.cm Test.main test-image and then run it by sml #SMLload test-image.XXXXX arg1 arg2 "this is one argument" where XXXXX is your architecture.
If you decide to MLton at some point, then you don't need to have any main function. It evaluates everything at toplevel, so you can create a main function and have it called by something like this:
fun main () = print "this is the main function\n"
val foo = 4
val _ = print ((Int.toString 4) ^ "\n")
val _ = main ()
Then you can compile it by mlton foo.sml which will produce an executable named "foo". When you run it, it will produce this as result:
./foo
4
this is the main function
Note that this is only one file, when you have multiple files you will either need to use MLB (ML Basis files) which is MLtons project files or you can use cm files and then compile it by mlton projectr.mlb