Compiling for x86-64 on M1

1,889 views
Skip to first unread message

tduva

unread,
Aug 25, 2022, 7:49:07 AM8/25/22
to WebP Discussion
Hello,

I need dynamic libraries to load with Java/JNI.

On macOS Monterey 12.0.1 M1 I successfully compiled libwebp-1.2.4 using:

./configure --prefix=$HOME
make
sudo make install

This compiled for arm64 and also seemed to work fine loading it with a arm64 Java through JNA.

Now I also need it for x86-64 Mac.

I tried the following, hoping that it would set the correct things:
./configure --prefix=$HOME --host=x86-64-darwin
./configure --prefix=$HOME --build=x86-64-darwin --host=x86-64-darwin

The compiled libraries looked different (smaller file size), but trying to load it with a x86_64 Java (still on M1) I got an error about "mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')".

I also tried the following, but it didn't even compile:

arch -x86_64 ./configure --prefix=$HOME
arch -x86_64 make

Can anyone point me in the right direction?

Thanks

James Zern

unread,
Aug 25, 2022, 3:14:48 PM8/25/22
to WebP Discussion
Hi,

On Thu, Aug 25, 2022 at 4:49 AM tduva <chatty...@gmail.com> wrote:
Hello,

I need dynamic libraries to load with Java/JNI.

On macOS Monterey 12.0.1 M1 I successfully compiled libwebp-1.2.4 using:

./configure --prefix=$HOME

With autoconf you can do a cross compile using --host and --build. --host is the target platform, so on an x86-64 mac to compile arm64 this works:
$ ../configure --build `../config.guess` --host arm64-apple-darwin CFLAGS='-arch arm64 -O2 -g'
The opposite should work on a M1:
$ ../configure --build `../config.guess` --host x86_64-apple-darwin CFLAGS='-arch x86_64 -O2 -g'
The extra CFLAGS are needed because there isn't a prefixed version of gcc (clang) to match the value in --host.

An alternative form with cmake + Xcode should work too:
$ cmake .. -G Xcode -DCMAKE_OSX_ARCHITECTURES="x86_64"
$ cmake --build . --config=Release

Note using -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" to generate fat libs will work, but not include NEON or x86 assembly code. CMake needs some work to support this correctly.
 
make
sudo make install

This compiled for arm64 and also seemed to work fine loading it with a arm64 Java through JNA.

Now I also need it for x86-64 Mac.

I tried the following, hoping that it would set the correct things:
./configure --prefix=$HOME --host=x86-64-darwin
./configure --prefix=$HOME --build=x86-64-darwin --host=x86-64-darwin

The compiled libraries looked different (smaller file size), but trying to load it with a x86_64 Java (still on M1) I got an error about "mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')".

I also tried the following, but it didn't even compile:

arch -x86_64 ./configure --prefix=$HOME
arch -x86_64 make

Can anyone point me in the right direction?

Thanks

--
You received this message because you are subscribed to the Google Groups "WebP Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webp-discuss...@webmproject.org.
To view this discussion on the web visit https://groups.google.com/a/webmproject.org/d/msgid/webp-discuss/3201f27e-515e-44e7-b638-93d3e8ce62d1n%40webmproject.org.

tduva

unread,
Aug 25, 2022, 6:40:24 PM8/25/22
to WebP Discussion, James Zern
Thanks for the reply. The following seemed to have worked for me:

./configure --build `./config.guess` --host x86_64-apple-darwin CFLAGS='-arch x86_64 -O2 -g'

Building with that got me the dylib files that successfully decoded a WebP image with a x86-64 JRE.

James Zern

unread,
Aug 26, 2022, 2:09:23 PM8/26/22
to WebP Discussion
On Thu, Aug 25, 2022 at 3:40 PM tduva <chatty...@gmail.com> wrote:
Thanks for the reply. The following seemed to have worked for me:

./configure --build `./config.guess` --host x86_64-apple-darwin CFLAGS='-arch x86_64 -O2 -g'

Building with that got me the dylib files that successfully decoded a WebP image with a x86-64 JRE.

That's good to hear. Thanks for letting us know.
Reply all
Reply to author
Forward
0 new messages