├── .gitignore ├── LICENSE ├── README.md └── embed.go /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | golang-embed-julia 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License. 2 | 3 | Copyright 2017, the golang-embed-julia authors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | golang-embed-julia 2 | ===== 3 | 4 | Official documentation: 5 | 6 | ## Determine the required compiler flags 7 | 8 | You can get the required flags for your system using the `julia-config.jl` script. 9 | It is included with the julia installation, 10 | but the location depends on your Julia installation environment. 11 | First, check the output of 12 | 13 | ``` 14 | echo $JULIA_DEPOT_PATH 15 | ``` 16 | 17 | If that environment variable is set, 18 | the script might be in one of the indicated directories. 19 | If it is unset, or you can't find it there, try 20 | 21 | ``` 22 | julia -e "println(DEPOT_PATH)" 23 | ``` 24 | 25 | ## Use the cgo directive to load the Julia C libraries 26 | 27 | Documentation: 28 | 29 | Your go header will have the following structure: 30 | 31 | ```go 32 | package main 33 | 34 | // #cgo CFLAGS: 35 | // #cgo LDFLAGS: 36 | // #include 37 | import "C" 38 | ``` 39 | 40 | ## Test the provided go file: 41 | ~~~ 42 | $ go run embed.go 43 | 1.4142135623730951 44 | $ 45 | ~~~ 46 | -------------------------------------------------------------------------------- /embed.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // See README.md and on how to modify the paths. 4 | // NOTE: It is important that there is NO empty line before `import "C"`, see https://golang.org/cmd/cgo. 5 | // 6 | // #cgo CFLAGS: -fPIC -DJULIA_INIT_DIR="/Applications/Julia-0.6.app/Contents/Resources/julia/lib" -I/Applications/Julia-0.6.app/Contents/Resources/julia/include/julia -I. 7 | // #cgo LDFLAGS: -L/Applications/Julia-0.6.app/Contents/Resources/julia/lib/julia -L/Applications/Julia-0.6.app/Contents/Resources/julia/lib -Wl,-rpath,/Applications/Julia-0.6.app/Contents/Resources/julia/lib -ljulia 8 | // #include 9 | import "C" 10 | 11 | func main() { 12 | 13 | /* required: setup the Julia context */ 14 | C.jl_init() 15 | 16 | /* run Julia commands */ 17 | C.jl_eval_string(C.CString(`println(sqrt(2.0))`)) 18 | 19 | /* strongly recommended: notify Julia that the 20 | program is about to terminate. this allows 21 | Julia time to cleanup pending write requests 22 | and run all finalizers 23 | */ 24 | C.jl_atexit_hook(0) 25 | } 26 | --------------------------------------------------------------------------------