I'm trying to wrap the Discount Markdown implementation with Cgo so I
can have a high-performance Markdown-to-HTML function.
My source code tree is as follows:
--src/
------cmd/
----------discount.go
----------gohelper.c
----------Makefile
------pkg/
----------discount/ (Discount tree + compiled code)
discount.go
---
package discount
// #cgo LDFLAGS: -lmarkdown
// #include "gohelper.c"
import "C"
func Markdownify (md string) string {
var length C.int = (C.int)(len (md))
doc := C.mdtos (C.mkd_string ((C.CString(md)), length, 0))
var html string = string (C.GoString (doc))
return html
}
---
gohelper.c
---
#include "../pkg/discount/mkdio.h"
// Converts Discount type to a format usable within Go
static char* mdtos(MMIOT *md) {
char** mdc = (char**) md;
return *mdc;
}
---
Makefile
---
include $(GOROOT)/src/Make.inc
TARG=discount
CGOFILES=discount.go
include $(GOROOT)/src/Make.pkg
---
When I run make, this compiles correctly, giving the following output:
---
buu700@Canada:~/workspace/discount/src/cmd$ make
CGOPKGPATH= cgo -- discount.go
touch _obj/_cgo_run
6g -o _go_.6 _obj/discount.cgo1.go _obj/_cgo_gotypes.go
6c -FVw -I/usr/lib/go/pkg/linux_amd64 -I . -o "_cgo_defun.6" _obj/
_cgo_defun.c
gcc -m64 -I . -g -fPIC -O2 -o _cgo_main.o -c _obj/_cgo_main.c
gcc -m64 -I . -g -fPIC -O2 -o discount.cgo2.o -c _obj/
discount.cgo2.c
gcc -m64 -I . -g -fPIC -O2 -o _cgo_export.o -c _obj/_cgo_export.c
gcc -m64 -g -fPIC -O2 -o _cgo1_.o _cgo_main.o discount.cgo2.o
_cgo_export.o -lmarkdown
cgo -dynimport _cgo1_.o >_obj/_cgo_import.c_ && mv -f _obj/
_cgo_import.c_ _obj/_cgo_import.c
6c -FVw -I . -o "_cgo_import.6" _obj/_cgo_import.c
rm -f _obj/discount.a
gopack grc _obj/discount.a _go_.6 _cgo_defun.6 _cgo_import.6
discount.cgo2.o _cgo_export.o
---
I then copied _obj/discount.a to $GOROOT/pkg/linux_amd64/
To test the library, I wrote the following code:
---
package main
import "discount"
import "fmt"
func main() {
fmt.Printf (discount.Markdownify ("**hi**"))
}
---
However, when I try to use this, I get the following result:
---
buu700@Canada:~/workspace/discount$ 6g test.go
buu700@Canada:~/workspace/discount$ 6l test.6
/usr/lib/go/pkg/linux_amd64/discount.a(discount.cgo2.o)(.text):
mkd_string: not defined
mkd_string(0): not defined
---
Anyone have any idea what's up with this and/or what I should change?
I do have Discount installed, if that is at all relevant. Here is an
archive of my code:
http://buu700.com/discount.tar.gz