22

I'm using a library that I cannot compile for Apple M1, so I have decided to compile it and use it using (Rosetta 2) for x86_64 which I successfully did following this to install brew and clang for x86_64.

However when I compile my project and try to link it against this library I get this error:

ld: warning: ignoring file ..../libapronxx.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64
...
ld: symbol(s) not found for architecture arm64

I have tried to the set compiler and linker flags ("-arch x86_64") but still got the same problem.

My question is: What is the proper way to build for macOS-x86_64 using cmake on Apple M1 (arm)?

Additional information: I'm using cmake via CLion.

UPDATE: I successfully compiled my project using the following commands:

# install a x86 cmake
arch -x86_64 /usr/local/bin/brew install cmake
...
# in the build directory
arch -x86_64 /usr/local/bin/cmake ..
make
arch -x86_64 ./my_exe

I also specified the architecture for clang using -arch flag

string(APPEND CMAKE_CXX_FLAGS_RELEASE " -arch x86_64 -O3")
string(APPEND CMAKE_CC_FLAGS_RELEASE " -arch x86_64 -O3")
# did the same for debug too

While this work fine, I still don't believe it is the proper way to use cmake to build for macOS-x86_64, in fact I cannot take the advantages of my IDE with all this manual approach.

2
  • @Tsyvarev, I can successfully compile the library and use with its own test files (the library uses a MakeFile), what I'm hoping to get from this question is a recommended approach to compile for macOS-x86_64 from an apple ARM machine regardless of what I did wrong. Nov 2, 2021 at 8:34
  • Oh, sorry, I incorrectly interpreted your question. After re-reading the error message the things becomes clear.
    – Tsyvarev
    Nov 2, 2021 at 10:07

1 Answer 1

35

After checking CMake source code, I found that it is enough to set CMAKE_OSX_ARCHITECTURES to x86_64:

set(CMAKE_OSX_ARCHITECTURES "x86_64")

This is the cleanest way so far to solve this issue, and work straight forward with CLion too.

3
  • 5
    I did not get this to work properly, and needed to do this set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE INTERNAL "" FORCE)
    – kolis
    Jan 20, 2022 at 11:05
  • What is "set(...)" - is that run from the command line or put somewhere else ? Jan 6 at 9:57
  • 1
    @DuncanGroenewald, set(...) is a CMake command to be put at the beginning of your CMakeLists.txt file. Jan 8 at 2:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.