gen2brain / go-fitz

Golang wrapper for the MuPDF Fitz library
GNU Affero General Public License v3.0
369 stars 87 forks source link

Cannot built c-shared #64

Closed naoufalzerai closed 1 year ago

naoufalzerai commented 1 year ago

Hello, Thanks for the great work, here is my issue

Code :

package main

import "C"
import "github.com/gen2brain/go-fitz"

//export GetKey
func GetKey() *C.char {
    fitz.New("test.pdf")
    return C.CString("test")
}

func main() {
}

when building c-shared go build -buildmode=c-shared -o lib.a main.go

I got this error :


/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(buffer.o): in function `fz_new_buffer':
buffer.c:(.text.fz_new_buffer+0x47): undefined reference to `sigsetjmp'
/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(buffer.o): in function `fz_new_buffer_from_data':
buffer.c:(.text.fz_new_buffer_from_data+0x27): undefined reference to `sigsetjmp'
/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(buffer.o): in function `fz_new_buffer_from_base64':
buffer.c:(.text.fz_new_buffer_from_base64+0xf6): undefined reference to `sigsetjmp'
/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(colorspace.o): in function `fz_cached_color_convert':
colorspace.c:(.text.fz_cached_color_convert+0xa0): undefined reference to `sigsetjmp'
/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(colorspace.o): in function `fz_new_colorspace':
colorspace.c:(.text.fz_new_colorspace+0x6f): undefined reference to `sigsetjmp'
/usr/bin/ld: /home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/libs/libmupdf_linux_amd64_musl.a(colorspace.o):colorspace.c:(.text.fz_new_icc_colorspace+0x76): more undefined references to `sigsetjmp' follow
collect2: error: ld returned 1 exit status
gen2brain commented 1 year ago

For such case you need to use -tags extlib.

naoufalzerai commented 1 year ago

Thanks for the reply, i tried but got this message

➤ go build -tags extlib -buildmode=c-shared -o lib.a main.go                                                                       (base) 
# github.com/gen2brain/go-fitz
/home/near/go/pkg/mod/github.com/gen2brain/go-fitz@v1.20.2/fitz.go:6:10: fatal error: mupdf/fitz.h: No such file or directory
    6 | #include <mupdf/fitz.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
gen2brain commented 1 year ago

Sure, in order to use an external library, you need to install the one.

naoufalzerai commented 1 year ago

I already have mupdf-tools installed

gen2brain commented 1 year ago

And that package provides header files? It seems it doesn't, and those are tools, shouldn't you install library and dev/devel (header) files?

gen2brain commented 1 year ago

I just tested your case with -tags extlib and it builds and provides lib.a.

gen2brain commented 1 year ago

Btw. you want to build c-shared so you should name the library with .so, not with .a.

naoufalzerai commented 1 year ago

ok I will try it and let you know :smile:

naoufalzerai commented 1 year ago

I rebuild mupdf

➤ sudo make prefix=/usr/local install               (base) 
    CC build/release/source/fitz/load-jpx.o
    AR build/release/libmupdf.a
install -d /usr/local/include/mupdf
install -d /usr/local/include/mupdf/fitz
install -d /usr/local/include/mupdf/pdf
install -m 644 include/mupdf/*.h /usr/local/include/mupdf
install -m 644 include/mupdf/fitz/*.h /usr/local/include/mupdf/fitz
install -m 644 include/mupdf/pdf/*.h /usr/local/include/mupdf/pdf
install -d /usr/local/lib
install -m 644 build/release/libmupdf.a build/release/libmupdf-third.a /usr/local/lib
install -d /usr/local/bin
install -m 755  build/release/mutool build/release/muraster build/release/mupdf-gl build/release/mupdf-x11 /usr/local/bin
install -d /usr/local/share/man/man1
install -m 644 docs/man/*.1 /usr/local/share/man/man1
install -d /usr/local/share/doc/mupdf
install -d /usr/local/share/doc/mupdf/examples
install -m 644 README COPYING CHANGES /usr/local/share/doc/mupdf
install -m 644 docs/*.html docs/*.css docs/*.png /usr/local/share/doc/mupdf
install -m 644 docs/examples/* /usr/local/share/doc/mupdf/examples

I got this error go build -tags extlib -buildmode=c-shared -o lib.so main.go

pdf-js.c:(.text.pdf_disable_js+0x36): undefined reference to `js_freestate'
/usr/bin/ld: /usr/local/lib/libmupdf.a(xps-glyphs.o): in function `xps_select_font_encoding':
xps-glyphs.c:(.text.xps_select_font_encoding+0x1a): undefined reference to `FT_Set_Charmap'
/usr/bin/ld: /usr/local/lib/libmupdf.a(xps-glyphs.o): in function `xps_encode_font_char':
xps-glyphs.c:(.text.xps_encode_font_char+0x1b): undefined reference to `FT_Get_Char_Index'
/usr/bin/ld: xps-glyphs.c:(.text.xps_encode_font_char+0x50): undefined reference to `FT_Get_Char_Index'
/usr/bin/ld: /usr/local/lib/libmupdf.a(xps-glyphs.o): in function `xps_measure_font_glyph':
xps-glyphs.c:(.text.xps_measure_font_glyph+0x61): undefined reference to `FT_Get_Advance'
/usr/bin/ld: xps-glyphs.c:(.text.xps_measure_font_glyph+0x76): undefined reference to `FT_Get_Advance'
/usr/bin/ld: /usr/local/lib/libmupdf.a(xps-glyphs.o): in function `xps_lookup_font':
xps-glyphs.c:(.text.xps_lookup_font+0x441): undefined reference to `FT_Set_Charmap'
/usr/bin/ld: $WORK/b002/_cgo_.o: hidden symbol `opj_setup_decoder' isn't defined
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
gen2brain commented 1 year ago

Why do you install it manually? That is an error now for FreeType. This library supports tags for extlib, static extlib, it supports pkg-config, etc., choose. In order to use the C library you need to, well, have basic knowledge of how the C compiler and linker work. I tried your example, and it works, there is no issue with this library, it is only a user issue.

gen2brain commented 1 year ago

Your example:

thinkpad /temp/test # go mod init test
go: creating new go.mod: module test
go: to add module requirements and sums:
    go mod tidy

thinkpad /temp/test # go mod tidy
go: finding module for package github.com/gen2brain/go-fitz
go: found github.com/gen2brain/go-fitz in github.com/gen2brain/go-fitz v1.20.2

thinkpad /temp/test # ls -la /usr/lib64/libmupdf.so
lrwxrwxrwx 1 root root 18 Oct 12 07:23 /usr/lib64/libmupdf.so -> libmupdf.so.1.20.3
thinkpad /temp/test # ls -la /usr/include/mupdf/
total 92
drwxr-xr-x   4 root root  4096 Oct 12 07:23 .
drwxr-xr-x 429 root root 36864 Oct 26 00:49 ..
drwxr-xr-x   2 root root  4096 Oct 12 07:23 fitz
-rw-r--r--   1 root root  2639 Oct 12 07:23 fitz.h
-rw-r--r--   1 root root 13935 Oct 12 07:23 memento.h
drwxr-xr-x   2 root root  4096 Oct 12 07:23 pdf
-rw-r--r--   1 root root  1607 Oct 12 07:23 pdf.h
-rw-r--r--   1 root root 13904 Oct 12 07:23 ucdn.h

thinkpad /temp/test # go build -buildmode=c-shared -tags extlib -o lib.so
thinkpad /temp/test # ldd lib.so 
ldd: warning: you do not have execution permission for `./lib.so'
    linux-vdso.so.1 (0x00007ffc299a0000)
    libmupdf.so.1.20.3 => /usr/lib64/libmupdf.so.1.20.3 (0x00007f09811e1000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f0981104000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f0980f26000)
    libfreetype.so.6 => /usr/lib64/libfreetype.so.6 (0x00007f0980e5e000)
    libgumbo.so.1 => /usr/lib64/libgumbo.so.1 (0x00007f0980dff000)
    libharfbuzz.so.0 => /usr/lib64/libharfbuzz.so.0 (0x00007f0980cf5000)
    libjpeg.so.62 => /usr/lib64/libjpeg.so.62 (0x00007f0980c61000)
    libz.so.1 => /lib64/libz.so.1 (0x00007f0980c47000)
    libjbig2dec.so.0 => /usr/lib64/libjbig2dec.so.0 (0x00007f0980c27000)
    libopenjp2.so.7 => /usr/lib64/libopenjp2.so.7 (0x00007f0980bc0000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f09841c9000)
    libbz2.so.1 => /lib64/libbz2.so.1 (0x00007f0980bad000)
    libpng16.so.16 => /usr/lib64/libpng16.so.16 (0x00007f0980b74000)
    libgraphite2.so.3 => /usr/lib64/libgraphite2.so.3 (0x00007f0980b4e000)
    libglib-2.0.so.0 => /usr/lib64/libglib-2.0.so.0 (0x00007f0980a18000)
    libpcre2-8.so.0 => /usr/lib64/libpcre2-8.so.0 (0x00007f098097d000)
naoufalzerai commented 1 year ago

Great it works, I forget to compile /usr/lib/libmupdf.so

make prefix=/usr                        \
     shared=yes                         \
     docdir=/usr/share/doc/mupdf-1.20.3 \
     install