为 x86_64 构建,但从自制软件安装的 glfw 用于 arm64 (aarch64)

Make building for x86_64 but glfw installed from homebrew is for arm64 (aarch64)

我的项目用 make 构建得很好,直到我导入 stb,现在我得到这个错误

ld: warning: ignoring file /opt/homebrew/lib/libglfw.3.4.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      HelloTriangleApplication::initWindow() in main.cpp.o
  "_glfwCreateWindowSurface", referenced from:
      HelloTriangleApplication::createSurface() in main.cpp.o
  "_glfwDestroyWindow", referenced from:
      HelloTriangleApplication::cleanup() in main.cpp.o
  "_glfwGetFramebufferSize", referenced from:
      HelloTriangleApplication::chooseSwapExtent(VkSurfaceCapabilitiesKHR const&) in main.cpp.o
      HelloTriangleApplication::recreateSwapChain() in main.cpp.o
  "_glfwGetRequiredInstanceExtensions", referenced from:
      HelloTriangleApplication::getRequiredExtensions() in main.cpp.o
  "_glfwGetWindowUserPointer", referenced from:
      HelloTriangleApplication::framebufferResizeCallback(GLFWwindow*, int, int) in main.cpp.o
  "_glfwInit", referenced from:
      HelloTriangleApplication::initWindow() in main.cpp.o
  "_glfwPollEvents", referenced from:
      HelloTriangleApplication::mainLoop() in main.cpp.o
  "_glfwSetFramebufferSizeCallback", referenced from:
      HelloTriangleApplication::initWindow() in main.cpp.o
  "_glfwSetWindowUserPointer", referenced from:
      HelloTriangleApplication::initWindow() in main.cpp.o
  "_glfwTerminate", referenced from:
      HelloTriangleApplication::cleanup() in main.cpp.o
  "_glfwWaitEvents", referenced from:
      HelloTriangleApplication::recreateSwapChain() in main.cpp.o
  "_glfwWindowHint", referenced from:
      HelloTriangleApplication::initWindow() in main.cpp.o
  "_glfwWindowShouldClose", referenced from:
      HelloTriangleApplication::mainLoop() in main.cpp.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[2]: *** [vulkanLearning] Error 1
make[1]: *** [CMakeFiles/vulkanLearning.dir/all] Error 2
make: *** [all] Error 2

程序似乎是为 x86_64 编译的,但我从自制软件安装的 libglfw 是为 aarch64 编译的。我如何让它为 aarch64 编译?

CMakelists.txt 使用 C++20,它是一个 Vulkan 项目(使用自定义环境变量)

cmake_minimum_required (VERSION 3.12)
project(
    vulkanLearning VERSION 0.1.0
    DESCRIPTION "vulkanLearning"
    LANGUAGES CXX
)

# Specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
# Set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

add_executable(${PROJECT_NAME} main.cpp)

find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)

if (VULKAN_FOUND)
    message(STATUS "Found Vulkan, Including and Linking now")
    include_directories(${Vulkan_INCLUDE_DIRS})
    target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)

似乎 CMake 正在为 x86_64 构建,即使您的处理器在 arm64 上运行。这是受CMAKE_HOST_SYSTEM_PROCESSOR的影响,所以应该是arm64.

设置 CMAKE_OSX_ARCHITECTURE 应该覆盖默认值,以便它会为 arm64 构建您的应用程序:

set(CMAKE_OSX_ARCHITECTURES "arm64")

这将强制 clang(或您使用的任何编译器)针对 arm64 进行编译。