| 1 | = Libraries in Cobra = |
| 2 | |
| 3 | You can use (obviously) .Net libraries from Cobra and you can make .Net Libraries with Cobra. |
| 4 | |
| 5 | The process is analogous to doing so using C#. [[BR]] |
| 6 | 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). [[BR]] |
| 7 | This file is then available for use as a library for any app that desires it.[[BR]] |
| 8 | This library file can be kept along side the app binary, placed in a known location and referenced from there or installed into the[[BR]] |
| 9 | global system Library location (GAC on .Net) |
| 10 | |
| 11 | In 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 | |
| 18 | Compile code as a library using the cobra compiler args '''-target:lib''' [[BR]] |
| 19 | |
| 20 | Assuming a library source file myLib.cobra |
| 21 | {{{ |
| 22 | cobra -target:lib myLib.cobra |
| 23 | }}} |
| 24 | If 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 | |
| 27 | If multiple files and a common (or multiple) library namespace, designate the (library) namespace in each file |
| 28 | |
| 29 | {{{ |
| 30 | #!cobra |
| 31 | # MyLib.cobra |
| 32 | |
| 33 | namespace MyLib |
| 34 | |
| 35 | class Foo |
| 36 | |
| 37 | pass |
| 38 | }}} |
| 39 | |
| 40 | and |
| 41 | |
| 42 | {{{ |
| 43 | #!cobra |
| 44 | # myLibGen.cobra |
| 45 | |
| 46 | namespace MyLib |
| 47 | |
| 48 | class Bar |
| 49 | |
| 50 | pass |
| 51 | }}} |
| 52 | |
| 53 | Compile the library |
| 54 | {{{ |
| 55 | cobra -target:lib MyLib.cobra myLibGen.cobra |
| 56 | }}} |
| 57 | Generates a library file called !MyLib.dll (same name as the first source file given). [[BR]] |
| 58 | You can specify the output library filename if desired different from the first filename given using '''-out:<name>''' |
| 59 | {{{ |
| 60 | cobra -target:lib -out:MyLib myLibBase.cobra myLibGen.cobra |
| 61 | }}} |
| 62 | |
| 63 | This will generate a single library file (!MyLib.dll on .Net) composed of the code from the listed files. |
| 64 | i.e a Library providing 2 classes in the namespace !MyLib, !MyLib.Foo and !MyLib.Bar |
| 65 | |
| 66 | === A note on library file naming: === |
| 67 | |
| 68 | 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. |
| 69 | |
| 70 | == Using a Library == |
| 71 | |
| 72 | This is just the same as using a system library except that (depending on how you named your library file) [[BR]] |
| 73 | you may need to provide a reference arg to the compiler so it can find the library file at compile time. |
| 74 | |
| 75 | Heres an app using !MyLib |
| 76 | {{{ |
| 77 | #!cobra |
| 78 | # testLib.cobra |
| 79 | use MyLib |
| 80 | |
| 81 | class 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 | |
| 88 | To compile (assuming !MyLib.dll in cwd or system places): |
| 89 | {{{ |
| 90 | # fully specified |
| 91 | cobra -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 | |
| 96 | A 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 | |
| 107 | use MyLib |
| 108 | |
| 109 | class Test |
| 110 | def main |
| 111 | f = Foo() |
| 112 | b = MyLib.Bar() |
| 113 | }}} |
| 114 | |
| 115 | |
| 116 | Back to LibraryTopics |