Hello there,
I encountered a problem recently where I get a series of link errors while attempting to build an application on an M1 (ARM) CPU (this is on a 2021 MacBook) when linking against libraries built using ExternalProject_Add
. I have a ‘fix’, but I’d like to check if it’s sufficient or if there’s a better, more idiomatic way of handling it.
This is the application in question - sdl-bgfx-imgui-starter
It’s a little graphics demo starter that relies on three separate dependencies (SDL2, bgfx and Dear ImGui). I have third-party folder that contains a single CMakeLists.txt
file that uses ExternalProject_Add
to download, build and install all the libraries.
The main application then uses DCMAKE_PREFIX_PATH
from the cli and find_package
from the CMakeLists.txt
file to find and link the libraries.
This all seems to work fine for the most part, but when building on M1 hardware, when the main application tries to link against the built libraries, I get an avalanche of link errors for the calls in the libraries I’m consuming.
... building for macOS-arm64 but attempting to link with file built for macOS-x86_64
"_SDL_Init", referenced from:
_main in main.cpp.o
"bgfx::shutdown()", referenced from:
_main in main.cpp.o
"ImGui::GetDrawData()", referenced from:
main_loop(void*) in main.cpp.o
etc...
To ‘fix’ this, I found I can simply add this line at the top of the application CMakeLists.txt
file (source).
if (APPLE)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif ()
And then if I reconfigure and build, everything works fine. My concern is, is this the right thing to do?
It’s not clear to me why the libraries fetched using ExternalProject_Add
are built for “x86_64” as opposed to “ARM” (there does not seem to be any mention of CMAKE_OSX_ARCHITECTURES
in any of the dependencies CMakeLists.txt
files). When building the application the default appears to be ARM which mismatches and causes all the link errors.
It would be nice to be able to specify which architecture the user of the application would like to build for as opposed to hardcoding it in the CMakeLists.txt
file. I could then ensure the user chooses explicitly (at least on Apple/macOS).
I’d be very grateful if anyone could offer any advice/suggestions on how to cleanly handle this in a future proof and idiomatic way.
Thank you very much for your time!
Tom