I'm currently working on extending racklog, which is a library I installed by running raco pkg install in the repo directory.
I'm trying to provide a new function, namely a struct constructor. Currently, I'm defining the struct as follows in racklog.rkt. I then provide it from that file.
; racklog.rkt
(struct my-struct (value))
(provide my-struct)
The main file of the library just provides everything from this file:
; main.rkt
(require "racklog.rkt")
(provide (all-from-out "racklog.rkt"))
However, when I try to use the provided constructor (which should be named my-struct) in a file requiring this module, it says that the id isn't found. In particular, I'm trying:
; test.rkt
(require racklog)
my-struct
This also happens even with non-struct things such as defined variables, functions, etc. All the other provided forms seem to be working fine. What's the way I can fix this so I can use the provided constructor? Thanks!
This is an unfortunate stale compiled file issue. Try raco setup --pkgs racklog to compile racklog and run your program again. It should now work. Alternatively, you can manually delete the compiled directories.
Related
I'm new to Common Lisp, I'm using Emacs/SLIME on Windows 10, and I'm trying to wrap my head around how CL and ASDF/packaging works.
I have a custom package 'my-pack' in a directory 'D:\Dropbox\my-packages'.
I have created a .conf file in:
%LOCALAPPDATA%\config\common-lisp\source-registry.conf.d\
And added this line:
(:tree "D:\\Dropbox\\my-packages\\")
I opened Emacs, started SLIME and made the project via the REPL:
(cl-project:make-project #p"D:/Dropbox/my-packages/my-pack)
I verified that the project is in the directory and then loaded the system with asdf (version 3.3.1):
(asdf:load-system :my-pack)
And it worked fine.
But when I quit and restart Emacs, I get the following error when trying to the load the system:
Component :MY-PACK not found
[Condition of type ASDF/FIND-COMPONENT:MISSING-COMPONENT]
As far as I can tell I've followed the steps in the manual. Any help appreciated.
cl-project's make-project ends with this line:
(push dir asdf:*central-registry*)
it adds your new project's directory to this list that tells ASDF where to look for projects. What is its value when you restart CL?
2.
\config\common-lisp\
Shouldn't it be .config?
However, I don't encourage to use this conf file with :tree. The doc says:
tell ASDF to recursively scan all the subdirectories
So, imagine that just once, you try yourself at web development and you install, for example, JavaScript dependencies with npm or equivalent, you'll end up with a gigantic node_modules/ and your Lisp will now take a few seconds to start up.
I suggest to put your projects under ~/common-lisp/ or ~/quicklisp/local-projects, or to create symlinks, or to add yourself your projects in asdf:*central-registry* from your Lisp startup file:
;; .sbclrc
(pushnew "/home/me/projects/ciel/" asdf:*central-registry* :test #'equal)
I developed a very simple FFI for the Wiring PI library, to be able to control a Raspberry Pi using Racket.
This worked well, but only covers the base functionality. I wanted to extend this to also use the lcd.h from the libwiringPiDev.so, so that I could create an FFI to control an LCD.
Normally, when you compile a C file that uses the LCD, you tag both the standard library and the development library when compiling:
-lwiringPi -lwiringPiDev
In my LCD FFI I did the following:
(define-ffi-definer define-lcd
(ffi-lib "usr/lib/libwiringPiDev.so"))
However, when I try to require that racket file, I run into:
; ffi-lib: couldn't open "/usr/lib/libwiringPiDev.so"
; (/usr/lib/libwiringPiDev.so: undefined symbol: digitalRead) [,bt for
; context]
The digitalRead function exists in the base library but not in the dev library...how do I do a "FFI with dependencies" so that Racket can properly manage both libraries to be able to find the necessary symbols?
Try loading libwiringPi.so first in global mode, then load libwiringPiDev.so:
(define-ffi-definer define-lcd
(ffi-lib "/usr/lib/libwiringPi.so" #:global? #t))
(define-ffi-definer define-lcd-dev
(ffi-lib "/usr/lib/libwiringPiDev.so"))
The #:global? #t argument corresponds to setting the RTLD_GLOBAL flag in the call to dlopen, and the man page for dlopen (on my Linux system) says
RTLD_GLOBAL
The symbols defined by this shared object will be made available
for symbol resolution of subsequently loaded shared objects.
I've decided I want to use Clojure's Java interoperability to play with the jMusic library. Because I know Java better than Clojure (by a lot), I'm testing things in Java and then rewriting them as (not-so-idiomatic) Clojure. I'm using Eclipse Luna with the Counterclockwise plugin, and am not using Maven, just Leiningen. As a quick aside, I don't want to use Emacs because I want to focus on learning one tool at a time.
To incorporate jMusic, I've downloaded the file and am right-clicking on the Project folder, selecting Build Path, and selecting Add External Archive. I've done that with both the Clojure project and the Java project. I have the following Java code:
import jm.music.data.Note;
import jm.util.Play;
public final class TestMusic {
public static void main(String[] args) {
Play.midi(new Note());
}
}
This program runs and makes noise without issue. I cannot reproduce this in Clojure. I wrote the following code (which could be wrong, but that's a separate issue):
(ns my-clojure-test.core
(:use jm.music.data Note)
(:use jm.util Play))
(. Play midi (. Note))
And I get the following error:
;; Clojure 1.6.0
CompilerException java.io.FileNotFoundException: Could not locate jm/music/data__init.class
or jm/music/data.clj on classpath: , compiling:(my_clojure_test/core.clj:1:1)
#<Namespace my-clojure-test.core>
I've tried doing the same right-click on the project and adding an external archive. I've moved the jMusic.jar to src/java, and also extracted its files alongside the .jar.
My project.clj file looks like this:
(defproject my-clojure-test "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/core.logic "0.8.10"]]
:source-paths ["src" "src/my-clojure-test"]
:java-source-paths ["src/java"])
How do I get Clojure to recognize the jMusic so I can make the aural "hello world" program play?
EDIT:
The selected answer does work, but do check the comments for more details about what I had to do to get it working, in case anyone else runs into the same problem.
To break down the error message:
CompilerException java.io.FileNotFoundException: Could not locate jm/music/data__init.class
or jm/music/data.clj on classpath: , compiling:(my_clojure_test/core.clj:1:1)
#<Namespace my-clojure-test.core>
This is saying that it was looking for either jm/music/data__init.class or jm/music/data.clj. Either one would suffice in order to load the namespace jm.music.data which you have asked for in your ns declaration. Of course there is no such Clojure namespace. This happened because you tried to use use to access classes, and it is designed for accessing namespaces. import is for accessing classes and packages.
(ns my-clojure-test.core
(:import (jm.music.data Note)
(jm.util Play)))
The . notation takes a method first, then the class, except when accessing a static method, in which case one should use /. The proper way to invoke a constructor is with the . following the classname, without any separator.
(Play/midi (Note.))
In emacs cider repl, now I know how to use clojure.tools.namespace in a leiningen project. However, when I use it on a single clj file which doesn't belong to any project, it seems clojure.tools.namespace doesn't work on the file:
=> #<FileNotFoundException java.io.FileNotFoundException: Could not locate com/foo__init.class or com/foo.clj on classpath: >
I have declared clojure.tools.namespace in .lein/profile.clj and require it in the clj file. How should I make clojure.tools.namespace work on a single clj file?
My profile.clj
{:user
{:repl-options {:timeout 128000}
:plugins [;; REPL
[cider/cider-nrepl "0.9.0-SNAPSHOT"]
[refactor-nrepl "0.2.2"]
;; Application server
[lein-immutant "2.0.0-SNAPSHOT"]
;; Automated testing
[lein-cloverage "1.0.2"]
[lein-test-out "0.3.1"]
;; Package management
[lein-ancient "0.6.2"]
[lein-clojars "0.9.1"]
;; Documentation
[codox "0.6.8"]
[lein-clojuredocs "1.0.2"]
;; Static analysis
[lein-typed "0.3.4"]
;; [jonase/eastwood "0.1.2"]
[lein-bikeshed "0.1.6"]
[lein-kibit "0.0.8"]]
:jvm-opts ["-Dapple.awt.UIElement=true"]
:dependencies [[org.clojars.gjahad/debug-repl "0.3.3"]
[difform "1.1.2"]
[spyscope "0.1.4"]
[org.clojure/tools.trace "0.7.8"]
[org.clojure/tools.namespace "0.2.9"]
[im.chit/vinyasa "0.2.0"]
[slamhound "1.5.5"]
[criterium "0.4.3"]]
:injections [(require 'spyscope.core)
(require 'alex-and-georges.debug-repl)
(require 'com.georgejahad.difform)
(require '[vinyasa.inject :as inj])
(inj/inject 'clojure.core '>
'[[clojure.repl apropos dir doc find-doc pst source]
[clojure.tools.trace trace trace-forms trace-ns trace-vars
untrace-ns untrace-vars]
[clojure.test run-tests run-all-tests]
[clojure.pprint pprint pp]
[com.georgejahad.difform difform]
[alex-and-georges.debug-repl debug-repl]
[vinyasa.pull pull]])]}}
While this doesn't answer your specific question, it might provide some ideas on
alternative workflows which may help.
lein-exec This is a lein plugin that
lets you write clojure scripts or evaluate clojure expressions on the command line
in a similar way to what can be done with shells, python, ruby, perl etc. Of
course, startup time is a bit high, but this plugin will help you deal with
dependencies etc inside your single clj script file. (there are other possible
solutions to improve the startup speed). See this blog post for some examples
http://charsequence.blogspot.in/2012/04/scripting-clojure-with-leiningen-2.html
A scratch project. I have a fairly generic project called scratch. It just allows
me to create a new file within the src directory of the project which I can use for
experiments or demonstrations. For example, I have a file called stackoverflow.clj
within this scratch project which I use when working out the answer to a clojure
question on stack overflow. In fact, I have a lot of individual clj files in this
directory, each just representing a simple idea, test, experiment etc. It isn't
really a project i.e. I couldn't do a lein run at the root of the project and
expect anything meaningful. However, I can go to the source directory in emacs,
open a file and then run cider.
I was trying to make executable file using lisp code. But I can't compile lisp file at all because there is no hellowolrd package before loading helloworld system
;; test.lisp
(asdf:load-system :helloworld)
(defun main()
(helloworld:start))
Of course, I made the helloworld system and put It in ~/quicklisp/local-projects/. helloworld system is successfully loaded without errors.
;; ~/quicklisp/local-projects/helloworld/helloworld.asd
(asdf:defsystem helloworld
:version "1.0"
:components ((:file "package")))
;; ~/quicklisp/local-projects/helloworld/package.lisp
(defpackage :helloworld
(:use :common-lisp :asdf)
(:export :start))
(in-package :helloworld)
(defun start()
(format t "Welcome, ASDF"))
I want to compile test.lisp without explicit loading. I also tried use-package and defpackage but failed.
;; test.lisp
(asdf:load-system :helloworld)
(use-package :helloworld)
(defun main()
(helloworld:start))
;; test.lisp
(asdf:load-system :helloworld)
(defpackage :test
(:use :cl :asdf)
(:export :main))
(in-package :test)
(defun main()
(helloworld:start))
How can I use helloworld package defined in helloworld system without loading it?
Should I have to make a new system using helloworld system?
In this code, there's something interesting going on:
;; test.lisp
(asdf:load-system :helloworld)
(defun main()
(helloworld:start))
You can't compile it as a whole because, as you've noted trying to read the symbol hellowworld:start is a problem, because there's no helloworld package yet. To read the symbol, you at least need to have the package defined. But then, why don't we get the same problem with (asdf:load-system :helloworld)? Simply, the ASDF package has already been defined (either the implementation includes it, or you loaded it already, or something else. One thing you could do, then, is to make sure at compilation time that you've already loaded your helloworld system:
;; test.lisp
(eval-when (:compile-toplevel)
(asdf:load-system :helloworld))
(defun main()
(helloworld:start))
That should let you compile the file; since you'll evaluate the loading form when compiling, and then the package will be defined by the time you define main.
Of course, now you'll have a compiled file, but what will happen if you load it into a fresh instance of Lisp where the helloworld system hasn't been loaded? You'll have a problem. So you really want to load that system when you load the file too, and probably if you're just executing forms from the file too (e.g., if you were reading forms one at a time and evaluating them). So you probably want to evaluate that load-system in two more contexts:
;; test.lisp
(eval-when (:compile-toplevel :load-toplevel :execute)
(asdf:load-system :helloworld))
(defun main()
(helloworld:start))
All that said, be sure to consider whether this is the most appropriate way to load your system in this case. It may make sense if, for some reason, you're trying to keep all the code in one file, or making a delivery script. If, on the other hand, you're making another ASDF loadable system, then you'd probably just include helloworld as a dependency and let ASDF handle loading the dependencies.
1- To have a file that can be both compiled and loaded, use eval-when. See my article http://fare.livejournal.com/146698.html
2- I recommend using cl-launch:
cl-launch -sp helloworld -r start -o helloworld -d !
3- If you don't want to waste 60MB in space, but instead can stand half a second to a few seconds of startup latency, use a #!/usr/bin/cl script.
If you want to compile a binary, you can use a simple Lisp script for that, e. g. (assuming SBCL)
;;;; build.lisp
(require "asdf")
(asdf:operate 'asdf:load-op "helloworld")
(sb-ext:save-lisp-and-die "helloworld"
:toplevel helloworld:start
:executable t)
Makefile:
all:
sbcl --load build.lisp
When loading a file, Lisp consequently executes its forms, so by the time it arrives at save-lisp-and-die it already knows about the helloworld package.
There are also dedicated tools for making executables, buildapp and cl-launch. The latter can produce executable scripts and work with saved cores.
UPD
Yes, the executable built in this way will contain the whole Lisp, so 1) it will be huge; 2) having two or more such executables means that you've got (probably superfluous) copies of the whole Lisp.
Another implementation can produce smaller binaries, e. g. CLISP; they say commercial ones are even better.
For Lisp-less target computers this is the only possible solution. On the contrary, if a target computer has a Lisp compiler, there are are two other options: scripts and saved cores.
You can make a shell script invoking sbcl --load <file>or use cl-launch to obtain a sophisticated shell wrapper, which can be as portable as you want it to. SBCL and CLISP support shebang, too.
However, if startup time matters, loading libraries will be a bottleneck, even though they are compiled only once. In this case you can work around by using a custom core, or image. A core can be thought of as a saved state of the Lisp world, and saving a core is the primary task of sb-ext:save-lisp-and-die and its counterparts. Loading a core is fast, and you immediately have all the libraries compiled in it. But then again the size becomes considerable, though not as huge as in the case of a standalone executable. Naturally, one core can be used for several applications. Cl-launch can work with cores as well; in particular, it allows to conveniently define the entry function.