Wiki

Changes between Initial Version and Version 1 of LibraryInCobra

Show
Ignore:
Timestamp:
12/03/13 08:58:42 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LibraryInCobra

    v1 v1  
     1= Libraries in Cobra = 
     2 
     3You can use (obviously) .Net libraries from Cobra and you can make .Net Libraries with Cobra. 
     4 
     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) 
     10 
     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 
     15 
     16== Creating a Library == 
     17 
     18Compile code as a library using the cobra compiler args  '''-target:lib''' [[BR]] 
     19 
     20Assuming a library source file myLib.cobra 
     21{{{ 
     22cobra -target:lib myLib.cobra 
     23}}} 
     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  
     26 
     27If multiple files and a common (or multiple)  library namespace, designate the (library) namespace in each file 
     28 
     29{{{ 
     30#!cobra 
     31# MyLib.cobra 
     32 
     33namespace MyLib 
     34 
     35class Foo 
     36 
     37    pass 
     38}}} 
     39 
     40and  
     41 
     42{{{ 
     43#!cobra 
     44# myLibGen.cobra 
     45 
     46namespace MyLib 
     47 
     48class Bar 
     49 
     50    pass 
     51}}} 
     52 
     53Compile the library 
     54{{{ 
     55cobra -target:lib MyLib.cobra myLibGen.cobra 
     56}}} 
     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>''' 
     59{{{ 
     60cobra -target:lib -out:MyLib myLibBase.cobra myLibGen.cobra 
     61}}} 
     62 
     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 
     65 
     66=== A note on library file naming: === 
     67 
     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. 
     69 
     70== Using a Library == 
     71 
     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. 
     74 
     75Heres an app using !MyLib 
     76{{{ 
     77#!cobra 
     78# testLib.cobra 
     79use MyLib 
     80 
     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() 
     86}}} 
     87 
     88To compile (assuming !MyLib.dll in cwd or system places): 
     89{{{ 
     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 
     94}}} 
     95 
     96A less error prone, more explicit alternative is to specify the ref to the library in the app source file. 
     97 
     98{{{  
     99#!cobra 
     100# testLib.cobra (app code) 
     101#assuming library code for library namespace MyLib in library file 'myLibraryFile.dll' 
     102 
     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 
     106 
     107use MyLib 
     108 
     109class Test 
     110    def main 
     111        f = Foo() 
     112        b = MyLib.Bar() 
     113}}} 
     114 
     115 
     116Back to LibraryTopics