Changes between Initial Version and Version 1 of LibraryInCobra

12/03/13 08:58:42 (4 years ago)



  • LibraryInCobra

    v1 v1  
     1= Libraries in Cobra = 
     3You can use (obviously) .Net libraries from Cobra and you can make .Net Libraries with Cobra. 
     5The process is analogous to doing so using C#. [[BR]] 
     6You 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). [[BR]] 
     7This file is then available for use as a library for any app that desires it.[[BR]] 
     8This library file can be kept along side the app binary, placed in a known location and referenced from there or installed into the[[BR]] 
     9global system Library location (GAC on .Net) 
     11In the using-your-library app code you  
     12  *  can specify to use the Library Namespace 
     13  *  If necessary provide a '''ref''' arg to describe where the library file resides  
     14  *  Make calls into the library code 
     16== Creating a Library == 
     18Compile code as a library using the cobra compiler args  '''-target:lib''' [[BR]] 
     20Assuming a library source file myLib.cobra 
     22cobra -target:lib myLib.cobra 
     24If successful, the compiler,rather than generating a .exe, will create a .dll and some debug symbol files[[BR]] 
     25    myLib.dll  and myLib.dll.mdb or myLib.pdb  
     27If multiple files and a common (or multiple)  library namespace, designate the (library) namespace in each file 
     31# MyLib.cobra 
     33namespace MyLib 
     35class Foo 
     37    pass 
     44# myLibGen.cobra 
     46namespace MyLib 
     48class Bar 
     50    pass 
     53Compile the library 
     55cobra -target:lib MyLib.cobra myLibGen.cobra 
     57Generates a library file called !MyLib.dll (same name as the first source file given). [[BR]] 
     58You can specify the output library filename if desired different from the first filename given using '''-out:<name>''' 
     60cobra -target:lib -out:MyLib myLibBase.cobra myLibGen.cobra 
     63This will generate a single library file (!MyLib.dll on .Net) composed of the code from the listed files. 
     64i.e a Library providing 2 classes in the namespace !MyLib, !MyLib.Foo and !MyLib.Bar 
     66=== A note on library file naming: === 
     68If 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. 
     70== Using a Library == 
     72This is just the same as using a system library except that (depending on how you named your library file) [[BR]] 
     73you may need to provide a reference arg to the compiler so it can find the library file at compile time. 
     75Heres an app using !MyLib 
     78# testLib.cobra 
     79use MyLib 
     81class Test 
     82    def main 
     83        f = Foo() 
     84        # could also forgo the 'use MyLib' and access Foo using a fully qualified name 
     85        # f = MyLib.Foo() 
     88To compile (assuming !MyLib.dll in cwd or system places): 
     90# fully specified 
     91cobra -ref:MyLib.dll testLib.cobra  
     92# in this case you should not have to explicitly provide the reference since the filename is inferable from the namespace 
     93# cobra testLib.cobra 
     96A less error prone, more explicit alternative is to specify the ref to the library in the app source file. 
     100# testLib.cobra (app code) 
     101#assuming library code for library namespace MyLib in library file 'myLibraryFile.dll' 
     103@ref myLibraryFile.dll 
     104# also if the library file is placed in a specific library directory (other than cwd) 
     105# @ref /Users/hops/src/theApp/libs/myLibraryFile.dll 
     107use MyLib 
     109class Test 
     110    def main 
     111        f = Foo() 
     112        b = MyLib.Bar() 
     116Back to LibraryTopics