Libraries in Cobra
You can use (obviously) .Net libraries from Cobra and you can make .Net Libraries with Cobra.
The process is analogous to doing so using C#.
You generate some library code, compile it as a library ( there is a cobra compiler switch for doing this) and it generates a .dll ( or .jar file depending on the backend being used).
This file is then available for use as a library for any app that desires it.
This library file can be kept along side the app binary, placed in a known location and referenced from there or installed into the
global system Library location (GAC on .Net)
In the using-your-library app code you
- can specify to use the Library Namespace
- If necessary provide a ref arg to describe where the library file resides
- Make calls into the library code
Creating a Library
Compile code as a library using the cobra compiler args -target:lib
Assuming a library source file myLib.cobra
cobra -target:lib myLib.cobra
If successful, the compiler,rather than generating a .exe, will create a .dll and some debug symbol files
myLib.dll and myLib.dll.mdb or myLib.pdb
If multiple files and a common (or multiple) library namespace, designate the (library) namespace in each file
# MyLib.cobra namespace MyLib class Foo pass
# myLibGen.cobra namespace MyLib class Bar pass
Compile the library
cobra -target:lib MyLib.cobra myLibGen.cobra
Generates a library file called MyLib.dll (same name as the first source file given).
You can specify the output library filename if desired different from the first filename given using -out:<name>
cobra -target:lib -out:MyLib myLibBase.cobra myLibGen.cobra
This will generate a single library file (MyLib.dll on .Net) composed of the code from the listed files. i.e a Library providing 2 classes in the namespace MyLib, MyLib.Foo and MyLib.Bar
A note on library file naming:
If you name the library file the same as the namespace its made into, the cobra compiler can determine the library file from the namespace reference (the use clause in the calling app) and you do not need to specify a ref(erence) for the compiler to find the library file when you compile an app that uses/references the library.
Using a Library
This is just the same as using a system library except that (depending on how you named your library file)
you may need to provide a reference arg to the compiler so it can find the library file at compile time.
Heres an app using MyLib
# testLib.cobra use MyLib class Test def main f = Foo() # could also forgo the 'use MyLib' and access Foo using a fully qualified name # f = MyLib.Foo()
To compile (assuming MyLib.dll in cwd or system places):
# fully specified cobra -ref:MyLib.dll testLib.cobra # in this case you should not have to explicitly provide the reference since the filename is inferable from the namespace # cobra testLib.cobra
A less error prone, more explicit alternative is to specify the ref to the library in the app source file.
# testLib.cobra (app code) #assuming library code for library namespace MyLib in library file 'myLibraryFile.dll' @ref myLibraryFile.dll # also if the library file is placed in a specific library directory (other than cwd) # @ref /Users/hops/src/theApp/libs/myLibraryFile.dll use MyLib class Test def main f = Foo() b = MyLib.Bar()
Back to LibraryTopics