Search code examples
gcccmakearmapple-m1

Can't compile Jabcode on M1 (ARM)


I'am trying to compile jab code (cf. https://jabcode.org/create) on my MacBook with M1 ARM. I do succeed with the first step of compiling jabcode with the given Makefile:

gcc -c -I. -I./include -O2 -std=c11 binarizer.c -o binarizer.o
gcc -c -I. -I./include -O2 -std=c11 decoder.c -o decoder.o
gcc -c -I. -I./include -O2 -std=c11 detector.c -o detector.o
detector.c:51:7: warning: using floating point absolute value function 'fabs' when argument is of integer type [-Wabsolute-value]
                                         fabs(state_count[1] - state_count[3]) < layer_tolerance; //layer 1 and layer 3 shall be of the same size
                                         ^
detector.c:51:7: note: use function 'abs' instead
                                         fabs(state_count[1] - state_count[3]) < layer_tolerance; //layer 1 and layer 3 shall be of the same size
                                         ^~~~
                                         abs
1 warning generated.
gcc -c -I. -I./include -O2 -std=c11 encoder.c -o encoder.o
gcc -c -I. -I./include -O2 -std=c11 image.c -o image.o
gcc -c -I. -I./include -O2 -std=c11 interleave.c -o interleave.o
gcc -c -I. -I./include -O2 -std=c11 ldpc.c -o ldpc.o
gcc -c -I. -I./include -O2 -std=c11 mask.c -o mask.o
gcc -c -I. -I./include -O2 -std=c11 pseudo_random.c -o pseudo_random.o
gcc -c -I. -I./include -O2 -std=c11 sample.c -o sample.o
gcc -c -I. -I./include -O2 -std=c11 transform.c -o transform.o
ar cru build/libjabcode.a binarizer.o decoder.o detector.o encoder.o image.o interleave.o ldpc.o mask.o pseudo_random.o sample.o transform.o
ranlib build/libjabcode.a

However, when trying to compile the Reader or Writer, I end up with:


gcc -c -I. -I../jabcode -I../jabcode/include -O2 -std=c11 jabwriter.c -o jabwriter.o
gcc jabwriter.o -L../jabcode/build -ljabcode -L../jabcode/lib -ltiff -lpng16 -lz -lm -O2 -std=c11 -o bin/jabcodeWriter
ld: warning: ignoring file ../jabcode/lib/libpng16.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
ld: warning: ignoring file ../jabcode/lib/libtiff.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
ld: warning: ignoring file ../jabcode/lib/libz.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
Undefined symbols for architecture arm64:
"\_TIFFClose", referenced from:
\_saveImageCMYK in libjabcode.a(image.o)
"\_TIFFDefaultStripSize", referenced from:
\_saveImageCMYK in libjabcode.a(image.o)
"\_TIFFOpen", referenced from:
\_saveImageCMYK in libjabcode.a(image.o)
"\_TIFFSetField", referenced from:
\_saveImageCMYK in libjabcode.a(image.o)
"\_TIFFWriteScanline", referenced from:
\_saveImageCMYK in libjabcode.a(image.o)
"\_png_image_begin_read_from_file", referenced from:
\_readImage in libjabcode.a(image.o)
"\_png_image_finish_read", referenced from:
\_readImage in libjabcode.a(image.o)
"\_png_image_free", referenced from:
\_readImage in libjabcode.a(image.o)
"\_png_image_write_to_file", referenced from:
\_saveImage in libjabcode.a(image.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: \*\*\* \[bin/jabcodeWriter\] Error 1

I already tried to add -lstdc++ to CFLAGS in all Makefiles, as suggested here, which generates warnings of "'linker' input unused" for jabcode and does not resolve the problems when trying to compile jacbodeReader of jabcodeWriter.

Also I tried to compile as x86_64, following this question, by adding -arch x86_64 to CFLAGS in each Makefile. The error remained as above Undefined symbols for architecture x86_64 and


ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Sorry if the answer is obvious, I'm no expert in C/C++ and Makefiles and could not find any solution here.


Solution

  • I've checked the sources on github and I've checked the flags that you pass to g++ and the answer is quite simple.

    You are linking against a static tiff library that wasn't build for your architecture. Look at the folder to which you point with the -L flag.

    The author states that he tested it on his ubuntu machine so presumably it is build for the amd64 architecture.

    To solve this you need to get the dependencies that you link (tiff is one of them) for your machine and link against it. On MacOS that is usually done using the homebrew package manager.