FAQ
Hi

I'm trying to integrate libtiff with my Go app to avoid the cost of
creating new processes etc (since the Go app will need to invoke tiff2pdf
repeatedly and as fast as possible).

I'm having a few issues getting it to compile, even with almost no code.

These are the files I'm using:

Makefile:
all:
  go build ./...


deps:
  git clone git@github.com:vadz/libtiff.git


.PHONY: deps

demo.go:
package main

/*
#cgo CFLAGS: -D_THREAD_SAFE -pthread -Ilibtiff/libtiff
#include "libtiff/tools/tiff2pdf.c"
*/
import "C"

func main() {

}


When I run make, I get the following output:
go build ./...

# git.local/services/tiff2pdf

Undefined symbols for architecture x86_64:

   "_TIFFClientOpen", referenced from:

       _main in demo.cgo2.o

   "_TIFFClientdata", referenced from:

       _main in demo.cgo2.o

       _t2p_write_pdf in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

       _t2p_write_advance_directory in demo.cgo2.o

       _t2p_write_pdf_header in demo.cgo2.o

       _t2p_write_pdf_obj_start in demo.cgo2.o

       ...

   "_TIFFClose", referenced from:

       _main in demo.cgo2.o

   "_TIFFError", referenced from:

       _main in demo.cgo2.o

       _t2p_init in demo.cgo2.o

       _t2p_validate in demo.cgo2.o

       _t2p_write_pdf in demo.cgo2.o

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_read_tiff_size in demo.cgo2.o

       ...

   "_TIFFFileName", referenced from:

       _t2p_write_pdf in demo.cgo2.o

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

       _t2p_write_advance_directory in demo.cgo2.o

   "_TIFFGetField", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_read_tiff_size in demo.cgo2.o

       _t2p_read_tiff_size_tile in demo.cgo2.o

       _t2p_write_pdf_info in demo.cgo2.o

       _t2p_pdf_tifftime in demo.cgo2.o

   "_TIFFGetFieldDefaulted", referenced from:

       _t2p_read_tiff_data in demo.cgo2.o

   "_TIFFGetSeekProc", referenced from:

       _main in demo.cgo2.o

   "_TIFFGetVersion", referenced from:

       _main in demo.cgo2.o

       _tiff2pdf_usage in demo.cgo2.o

   "_TIFFGetWriteProc", referenced from:

       _t2p_write_pdf in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

       _t2p_write_pdf_header in demo.cgo2.o

       _t2p_write_pdf_obj_start in demo.cgo2.o

       _t2p_write_pdf_obj_end in demo.cgo2.o

       _t2p_write_pdf_name in demo.cgo2.o

       ...

   "_TIFFIsCODECConfigured", referenced from:

       _t2p_read_tiff_data in demo.cgo2.o

   "_TIFFIsTiled", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

   "_TIFFNumberOfDirectories", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

   "_TIFFNumberOfStrips", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

   "_TIFFNumberOfTiles", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFOpen", referenced from:

       _main in demo.cgo2.o

   "_TIFFReadEncodedStrip", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

   "_TIFFReadEncodedTile", referenced from:

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFReadRGBAImageOriented", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

   "_TIFFReadRawStrip", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

   "_TIFFReadRawTile", referenced from:

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFReverseBits", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFScanlineSize", referenced from:

       _t2p_read_tiff_size in demo.cgo2.o

   "_TIFFSetDirectory", referenced from:

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

   "_TIFFSetField", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFStripSize", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFTileRowSize", referenced from:

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFTileSize", referenced from:

       _t2p_read_tiff_size_tile in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "_TIFFWarning", referenced from:

       _main in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

   "_TIFFWriteDirectory", referenced from:

       _t2p_write_pdf in demo.cgo2.o

       _t2p_write_advance_directory in demo.cgo2.o

   "_TIFFWriteEncodedStrip", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "__TIFFfree", referenced from:

       _t2p_free in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

   "__TIFFmalloc", referenced from:

       _main in demo.cgo2.o

       _t2p_init in demo.cgo2.o

       _t2p_write_pdf in demo.cgo2.o

       _t2p_read_tiff_init in demo.cgo2.o

       _t2p_read_tiff_data in demo.cgo2.o

       _t2p_readwrite_pdf_image in demo.cgo2.o

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

       ...

   "__TIFFmemcpy", referenced from:

       _t2p_readwrite_pdf_image_tile in demo.cgo2.o

       _t2p_tile_collapse_left in demo.cgo2.o

   "__TIFFmemset", referenced from:

       _main in demo.cgo2.o

       _t2p_init in demo.cgo2.o

       _t2p_read_tiff_init in demo.cgo2.o

   "__TIFFrealloc", referenced from:

       _t2p_readwrite_pdf_image in demo.cgo2.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see
invocation)

make: *** [all] Error 2


It seems to be suggesting it can't find the libtiff header files referenced
by tiff2pdf.c, but I've gone in circles a few times on this trying various
LDFLAGS and CFLAGS options - I either go back a step (e.g. getting
"tiff2pdf.c not found" errors), or nothing changes.
I can compile libtiff directly using its configure script and generated
Makefile, but haven't had the same luck with go build.

I'm no C expert by the way, so probably missing something really obvious :/

Any help will be appreciated!

Thanks
Ian



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Ian Taylor at Nov 18, 2014 at 5:07 pm

    On Tue, Nov 18, 2014 at 3:40 AM, wrote:
    /*
    #cgo CFLAGS: -D_THREAD_SAFE -pthread -Ilibtiff/libtiff
    #include "libtiff/tools/tiff2pdf.c"
    */
    import "C"
    Undefined symbols for architecture x86_64:

    "_TIFFClientOpen", referenced from:

    _main in demo.cgo2.o
    You need something like

    #cgo LDFLAGS -ltiff

    by the #cgo CFLAGS line. I don't know exactly what -l command you
    need on your system, but it is whatever is required to make a pure C
    program work.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Emily Maier at Nov 18, 2014 at 5:10 pm

    On Tue, Nov 18, 2014 at 03:40:24AM -0800, iankent85@gmail.com wrote:
    Hi

    I'm trying to integrate libtiff with my Go app to avoid the cost of
    creating new processes etc (since the Go app will need to invoke tiff2pdf
    repeatedly and as fast as possible).

    I'm having a few issues getting it to compile, even with almost no code.

    These are the files I'm using:

    Makefile:
    all:
    go build ./...


    deps:
    git clone git@github.com:vadz/libtiff.git


    .PHONY: deps

    demo.go:
    package main

    /*
    #cgo CFLAGS: -D_THREAD_SAFE -pthread -Ilibtiff/libtiff
    #include "libtiff/tools/tiff2pdf.c"
    */
    import "C"

    func main() {

    }


    When I run make, I get the following output:
    go build ./...

    # git.local/services/tiff2pdf

    Undefined symbols for architecture x86_64:

    "_TIFFClientOpen", referenced from:

    _main in demo.cgo2.o

    "_TIFFClientdata", referenced from:

    _main in demo.cgo2.o

    _t2p_write_pdf in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    _t2p_write_advance_directory in demo.cgo2.o

    _t2p_write_pdf_header in demo.cgo2.o

    _t2p_write_pdf_obj_start in demo.cgo2.o

    ...

    "_TIFFClose", referenced from:

    _main in demo.cgo2.o

    "_TIFFError", referenced from:

    _main in demo.cgo2.o

    _t2p_init in demo.cgo2.o

    _t2p_validate in demo.cgo2.o

    _t2p_write_pdf in demo.cgo2.o

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_read_tiff_size in demo.cgo2.o

    ...

    "_TIFFFileName", referenced from:

    _t2p_write_pdf in demo.cgo2.o

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    _t2p_write_advance_directory in demo.cgo2.o

    "_TIFFGetField", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_read_tiff_size in demo.cgo2.o

    _t2p_read_tiff_size_tile in demo.cgo2.o

    _t2p_write_pdf_info in demo.cgo2.o

    _t2p_pdf_tifftime in demo.cgo2.o

    "_TIFFGetFieldDefaulted", referenced from:

    _t2p_read_tiff_data in demo.cgo2.o

    "_TIFFGetSeekProc", referenced from:

    _main in demo.cgo2.o

    "_TIFFGetVersion", referenced from:

    _main in demo.cgo2.o

    _tiff2pdf_usage in demo.cgo2.o

    "_TIFFGetWriteProc", referenced from:

    _t2p_write_pdf in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    _t2p_write_pdf_header in demo.cgo2.o

    _t2p_write_pdf_obj_start in demo.cgo2.o

    _t2p_write_pdf_obj_end in demo.cgo2.o

    _t2p_write_pdf_name in demo.cgo2.o

    ...

    "_TIFFIsCODECConfigured", referenced from:

    _t2p_read_tiff_data in demo.cgo2.o

    "_TIFFIsTiled", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    "_TIFFNumberOfDirectories", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    "_TIFFNumberOfStrips", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    "_TIFFNumberOfTiles", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFOpen", referenced from:

    _main in demo.cgo2.o

    "_TIFFReadEncodedStrip", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    "_TIFFReadEncodedTile", referenced from:

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFReadRGBAImageOriented", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    "_TIFFReadRawStrip", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    "_TIFFReadRawTile", referenced from:

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFReverseBits", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFScanlineSize", referenced from:

    _t2p_read_tiff_size in demo.cgo2.o

    "_TIFFSetDirectory", referenced from:

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    "_TIFFSetField", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFStripSize", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFTileRowSize", referenced from:

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFTileSize", referenced from:

    _t2p_read_tiff_size_tile in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "_TIFFWarning", referenced from:

    _main in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    "_TIFFWriteDirectory", referenced from:

    _t2p_write_pdf in demo.cgo2.o

    _t2p_write_advance_directory in demo.cgo2.o

    "_TIFFWriteEncodedStrip", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "__TIFFfree", referenced from:

    _t2p_free in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    "__TIFFmalloc", referenced from:

    _main in demo.cgo2.o

    _t2p_init in demo.cgo2.o

    _t2p_write_pdf in demo.cgo2.o

    _t2p_read_tiff_init in demo.cgo2.o

    _t2p_read_tiff_data in demo.cgo2.o

    _t2p_readwrite_pdf_image in demo.cgo2.o

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    ...

    "__TIFFmemcpy", referenced from:

    _t2p_readwrite_pdf_image_tile in demo.cgo2.o

    _t2p_tile_collapse_left in demo.cgo2.o

    "__TIFFmemset", referenced from:

    _main in demo.cgo2.o

    _t2p_init in demo.cgo2.o

    _t2p_read_tiff_init in demo.cgo2.o

    "__TIFFrealloc", referenced from:

    _t2p_readwrite_pdf_image in demo.cgo2.o

    ld: symbol(s) not found for architecture x86_64

    clang: error: linker command failed with exit code 1 (use -v to see
    invocation)

    make: *** [all] Error 2


    It seems to be suggesting it can't find the libtiff header files referenced
    by tiff2pdf.c, but I've gone in circles a few times on this trying various
    LDFLAGS and CFLAGS options - I either go back a step (e.g. getting
    "tiff2pdf.c not found" errors), or nothing changes.
    I can compile libtiff directly using its configure script and generated
    Makefile, but haven't had the same luck with go build.

    I'm no C expert by the way, so probably missing something really obvious :/

    Any help will be appreciated!

    Thanks
    Ian



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    It's finding the headers fine, but when it gets to the linker it can't
    find the actual code because it just has the headers, not the library
    itself. Try adding "#cgo LDFLAGS: -L<location of your libtiff.so>
    -ltiff" to your import "C" comment.

    Or if you want to import statically do "#cgo LDFLAGS:
    -L<location>/libtiff.a".

    Emily

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Kent at Nov 18, 2014 at 6:48 pm
    Thanks both!

    It turned out to be an issue with how libtiff imports some of its C files -
    its done at compile time via the Makefile, and they weren't referenced from
    any other file.

    I had to alter my Makefile to clone libtiff and build it before attempting
    to build the Go app (some .h files I think are created during compilation):
    all:
      go build ./...

    deps:
      -git clone git@github.com:vadz/libtiff.git ../../vadz/libtiff
      pushd ../../vadz/libtiff; ./configure; make; popd

    .PHONY: deps

    And then do a bit of reverse engineering to build up this fun include list
    - it needs a lot of cleaning up (and tiff2pdf.c was stolen from
    libtiff/tools, with its main() function removed), but it compiles:
    package main


    /*
    #cgo CFLAGS: -D_THREAD_SAFE -pthread -I../../vadz/libtiff/libtiff
    #cgo LDFLAGS: -lm
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "tif_aux.c"
    #include "tif_close.c"
    #include "tif_codec.c"
    #include "tif_color.c"
    #include "tif_compress.c"
    #include "tif_dir.c"
    #include "tif_dirinfo.c"
    #include "tif_dirread.c"
    #include "tif_dirwrite.c"
    #include "tif_dumpmode.c"
    #include "tif_error.c"
    #include "tif_extension.c"
    #include "tif_fax3.c"
    #include "tif_fax3sm.c"
    #include "tif_flush.c"
    #include "tif_getimage.c"
    #include "tif_jbig.c"
    #include "tif_jpeg.c"
    #include "tif_jpeg_12.c"
    #include "tif_lzma.c"
    #include "tif_lzw.c"
    #include "tif_luv.c"
    #include "tif_next.c"
    #include "tif_ojpeg.c"
    #include "tif_open.c"
    #include "tif_packbits.c"
    #include "tif_pixarlog.c"
    #include "tif_predict.c"
    #include "tif_print.c"
    #include "tif_read.c"
    #include "tif_strip.c"
    #include "tif_swab.c"
    #include "tif_thunder.c"
    #include "tif_tile.c"
    #include "tif_version.c"
    #include "tif_warning.c"
    #include "tif_write.c"
    #include "tif_zip.c"
    #include "tif_unix.c"
    #include "c/tiff2pdf.c"
    */
    import "C"

    func main() {
      C.tiff2pdf_usage()
    }


    One thing I am still having an issue with is how cgo handles the .c files.
    godoc implies that any .c file will be automatically built once Go detects
    the 'import "C"' line, which it does. However, in this example, I had to
    move tiff2pdf out of the current directory, otherwise Go complained about
    "tiff2pdf_usage" not being defined (first use in this file).

    It works fine now I've moved it to a subdirectory and manually included it,
    but I'm sure godoc is right and I'm missing something obvious!

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 18, '14 at 4:52p
activeNov 18, '14 at 6:48p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase