I have just installed BER MetaOCaml with the following :
$ opam update
$ opam switch 4.02.1+BER
$ eval `opam config env`
and I am not able to run MetaOCaml code in tuareg toplevel or simply ocaml toplevel. Following code :
let a = .<1+2>.;;
results in
Error: Reference to undefined global `Trx'
I have tried #load "trx.cma" but it cannot find the file.
Answer edit :
metaocaml is the right executable.
After adding this :
`(defun tuareg-run-metaocaml ()
"Run an OCaml toplevel process. I/O via buffer `*ocaml-toplevel*'."
(interactive)
(tuareg-run-process-if-needed
"/usr/bin/opam config exec -- metaocaml")
(display-buffer tuareg-interactive-buffer-name))
(add-hook 'tuareg-mode-hook
' (lambda ()
(define-key tuareg-mode-map (kbd "C-c M-s")
'tuareg-run-metaocaml)))`
To my .emacs, I am now able to run MetaOCaml with C-c M-s.
Related
I am currently using ocaml 4.06.0 and I am trying to use the Z3 sat solver. I am using opam's oasis to compile the files (which is building everything successfully). However, when I run the native code produced I am getting the following error: error while loading shared libraries: libz3.so. I tried reinstalling the z3 package but the error still persists. Can anyone help me solve this please because I have no idea what else to try?
Here is what I did just now to install z3 under Ubuntu 18.04.1:
$ opam depext conf-gmp.1
$ opam depext conf-m4.1
These installed gmp and m4 outside of opam. Pretty impressive.
$ opam install z3
Now the z3 library is installed so you can use it from OCaml code. But there are no executables installed (that I can find).
$ export LD_LIBRARY_PATH=~/.opam/4.06.0/lib/z3
$ ocaml -I ~/.opam/4.06.0/lib/z3
OCaml version 4.06.0
# #load "nums.cma";;
# #load "z3ml.cma";;
# let ctx = Z3.mk_context [];;
val ctx : Z3.context = <abstr>
The setting of LD_LIBRARY_PATH is what makes it possible to find libz3.so.
This is as far as I got for now. Maybe this will be helpful.
Update
Here is how I compiled and linked a test program.
$ export LD_LIBRARY_PATH=~/.opam/4.06.0/lib/z3
$ cat tz3.ml
let context = Z3.mk_context []
let solver = Z3.Solver.mk_solver context None
let xsy = Z3.Symbol.mk_string context "x"
let x = Z3.Boolean.mk_const context xsy
let () = Z3.Solver.add solver [x]
let main () =
match Z3.Solver.check solver [] with
| UNSATISFIABLE -> Printf.printf "unsat\n"
| UNKNOWN -> Printf.printf "unknown"
| SATISFIABLE ->
match Z3.Solver.get_model solver with
| None -> ()
| Some model ->
Printf.printf "%s\n"
(Z3.Model.to_string model)
let () = main ()
$ ocamlopt -I ~/.opam/4.06.0/lib/z3 -o tz3 \
nums.cmxa z3ml.cmxa tz3.ml
$ ./tz3
(define-fun x () Bool
true)
$ unset LD_LIBRARY_PATH
$ ./tz3
./tz3: error while loading shared libraries: libz3.so:
cannot open shared object file: No such file or directory
It works--i.e., it says that the trivial formula x can be satisfied by making x be true.
Note: initially I thought the setting of LD_LIBRARY_PATH wasn't necessary here. But in later testing I've found that it is necessary. So that is probably the key to your problem.
It's a little cumbersome and error prone to set LD_LIBRARY_PATH for running your programs. It's good enough for personal testing, but probably not for any kind of wider deployment. There are ways to set the search path for shared libraries at link time.
I hope this helps.
On Fedora 21, I compiled the Frama-C Aluminum distribution from source after installing all its prerequisites. My version of OCaml is 4.02.3. Frama-C and the Frama-C GUI work fine. I am trying to follow section 2.3, "The ViewCfg plug-in" of the Frama-C Plug-In Development Guide. However, in section 2.3.4, "Extending the Frama-C GUI", after I add the GUI extension code and run it using the "-load-script" option, I get the following message:
File "cfg_print.ml", line 87, characters 19-43:
Error: Unbound module GMenu
[kernel] user error: compilation of 'cfg_print.ml' failed
Lines 86-87 read:
let cfg_selector
(popup_factory:GMenu.menu GMenu.factory) main_ui ~button:_ localizable =
I googled "unbound module gmenu" but didn't find anything useful. I also never ran into this error while using the Neon and Sodium versions of Frama-C. Interestingly, if I skip that section and follow section 2.3.5, "Splitting files and writing a Makefile", I no longer get the "Unbound module GMenu" message, and the example works fine.
If I had to guess, when I use the "-load-script" option, Frama-C (or my version of OCaml, whatever the case may be) apparently cannot find the Gtk libraries for some reason. But if I use make, OCaml can find the Gtk libraries. Is there something possibly wrong with the way I installed Frama-C and/or the Gtk libraries? How can I check this, or more importantly, how can I fix this?
Your Frama-C installation is probably ok. What you observe is a bug that was introduced when we transitioned to OCamlfind. We will fix it for Frama-C Silicium.
In case you really want to use a script, here is the patch that you need to apply to the sources of Frama-C:
--- a/src/kernel_services/plugin_entry_points/dynamic.ml
+++ b/src/kernel_services/plugin_entry_points/dynamic.ml
## -236,7 +236,7 ## let load_script base =
else
Format.fprintf fmt "%s -c" Config.ocamlc ;
Format.fprintf fmt " -w Ly -warn-error A -I %s" Config.libdir ;
- if !Config.is_gui then Format.pp_print_string fmt " -I +lablgtk" ;
+ if !Config.is_gui then Format.pp_print_string fmt " -package lablgtk2" ;
List.iter (fun p -> Format.fprintf fmt " -I %s" p) !load_path ;
Format.fprintf fmt " %s.ml" base ;
Format.pp_print_flush fmt () ;
I find that when I run ocaml in terminal, ie, in interactive mode, it will load .ocamlinit. However, when I run ocaml test.ml, ie, in script mode, it doesn't load .ocamlinit file. This actually causes some trouble for me, since I have the following setup in my .ocamlinit:
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
#require "core.top";;
#require "core.syntax";;
So when I run in interactive mode, #use "topfind" will be executed and I can open other libraries in my code. But as .ocamlinit is not loaded in script mode, when I run ocaml test.ml in terminal, with test.ml like this:
open Core.Std;;
let () = print_endline "hello world"
It will fail with error: "Error: Unbound module Core".
My question is:
How to fix this so that when running ocaml in script mode, the .ocamlinit file will also be loaded?
Why the ocaml toplevel system doesn't load .ocamlinit file in script mode?
ps. The OCaml version is 4.01.0
I think that's a "bug" (or at least unexpected behaviour) in the toplevel. This could be fixed in future versions of OCaml. I don't have a satisfying workaround to propose, besides ocaml -noprompt < test.ml (which is not equivalent as you will get some noisy output from the toplevel instead of just your file executed), or of course compiling the program and running it (which may be as simple as ocamlbuild test.byte && ./test.byte).
I am basically executing:
ocamlmktop -o mytoplevel 1.cmo 2.cmo
and I get by an executable toplevel. The question is how do I make aquamacs run that toplevel?
It depends of the Emacs mode that you use to develop in OCaml under Aquamacs. As tuareg-mode is by far the most popular choice, I consider this case.
You usually start an OCaml toplevel with the command M-x tuareg-run-ocaml which prompts for a toplevel to start. You can here enter the path to your custom toplevel.
The toplevel proposed by tuareg-run-ocaml is actually the value of the variable tuareg-interactive-program, you can take advantage of this to define a custom tuareg-run-mytoplevel function with:
(defun tuareg-run-mytoplevel ()
(interactive)
(let ((tuareg-interactive-program "/path/to/mytoplevel"))
(tuareg-run-ocaml)))
If you put this definition in your ~/.emacs, you can then use the command M-x tuareg-run-mytoplevel to start your own toplevel.
I have been trying to figure out a way to efficiently save a program, compile it and then run it in emacs. I have succeeded only partially in it.
I use smart-compile.el to make the job easier ( http://emacswiki.org/emacs/smart-compile.el ).
In that I have edited the C++ relevant portion to the below so that a program compiles and runs when I type M-x smart-compile RET followed by a RET.
(defcustom smart-compile-alist '(
;; g++-3 is used instead of g++ as the latter does not
;; work in Windows. Also '&& %n' is added to run the
;; compiled object if the compilation is successful
("\\.[Cc]+[Pp]*\\'" . "g++-3 -O2 -Wall -pedantic -Werror
-Wreturn-type %f -lm -o %n && %n")
..
To give an example, for a program sqrt.cpp, smart-compile auto generates following compile command:
g++-3 -O2 -Wall -pedantic -Werror -Wreturn-type sqrt.cpp -lm -o sqrt && sqrt
This works as long as my .cpp does not have any cin statement. For codes with cin statements, the console window shows the point where the user is supposed to input data. But I am unable to input anything and the Compilations status stays stuck at running.
In order to make the codes with user input work, I have to remove the && FILENAME part and then manually run ./FILENAME in emacs' eshell.
I am running emacs 24.3 in Windows. I have Cygwin installed and its bin added to the Windows environment variable Path (that's why g++-3 compilation works).
I would appreciate if someone could guide me how to save-compile-run user input required .cpp programs in emacs using a single command. Or at least how do I need to modify the above g++-3 command to make compile+run work for user input programs.
Thanks!
Emacs is programmable, so if something requires two steps, you can write a command that combines them. The simples code would look like this:
(defun save-compile-execute ()
(interactive)
(smart-compile 1) ; step 1: compile
(let ((exe (smart-compile-string "%n"))) ; step 2: run in *eshell*
(with-current-buffer "*eshell*"
(goto-char (point-max))
(insert exe)
(eshell-send-input))
(switch-to-buffer-other-window "*eshell*")))
The above code is simple, but it has one flaw: it doesn't wait for the compilation to finish. Since smart-compile doesn't support a flag for synchronous compilation, this must be achieved by temporarily hooking into the compilation-finish-functions, which makes the code more complex:
(require 'cl) ; for lexical-let
(defun do-execute (exe)
(with-current-buffer "*eshell*"
(goto-char (point-max))
(insert exe)
(eshell-send-input))
(switch-to-buffer-other-window "*eshell*"))
(defun save-compile-execute ()
(interactive)
(lexical-let ((exe (smart-compile-string "./%n"))
finish-callback)
;; when compilation is done, execute the program
;; and remove the callback from
;; compilation-finish-functions
(setq finish-callback
(lambda (buf msg)
(do-execute exe)
(setq compilation-finish-functions
(delq finish-callback compilation-finish-functions))))
(push finish-callback compilation-finish-functions))
(smart-compile 1))
The command can be run with M-x save-compile-execute RET or by binding it to a key.