You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于非通用ncurses库构建Universal Binary通用二进制文件?

构建通用Fat二进制时链接多架构ncurses库的问题解决

核心结论

直接通过-L指定多架构库路径让clang自动匹配对应架构的库不可行。clang在链接阶段会按-L的顺序查找库,找到第一个同名库后就会停止搜索,不会为不同架构去对应路径找库,因此会出现架构不匹配错误。你需要通过以下两种方式解决:要么用lipo合并不同架构的库为Fat库,要么分架构编译链接后再合并可执行文件。

解决方案一:合并ncurses库为Fat库

  1. 确认两个架构的ncurses库路径:
    • ARM64:/opt/homebrew/opt/ncurses/lib/libncurses.dylib
    • x86_64:/usr/local/homebrew/opt/ncurses/lib/libncurses.dylib
  2. 用lipo合并成Fat库:
    lipo -create /opt/homebrew/opt/ncurses/lib/libncurses.dylib /usr/local/homebrew/opt/ncurses/lib/libncurses.dylib -output libncurses-fat.dylib
    
  3. 用合并后的Fat库链接:
    clang++ -std=c++17 -Wall -fno-objc-arc -finput-charset=UTF-8 -I/opt/homebrew/opt/ncurses/include -arch arm64 -arch x86_64 -c -o main.o main.cpp
    clang++ -L. -lncurses-fat -arch arm64 -arch x86_64 -o test main.o
    

解决方案二:分架构编译链接再合并可执行文件

这种方式不需要合并库,分别处理每个架构的编译和链接:

  1. 编译ARM64目标文件并链接对应库:
    clang++ -std=c++17 -Wall -fno-objc-arc -finput-charset=UTF-8 -I/opt/homebrew/opt/ncurses/include -arch arm64 -c -o main-arm64.o main.cpp
    clang++ -L/opt/homebrew/opt/ncurses/lib -lncurses -arch arm64 -o test-arm64 main-arm64.o
    
  2. 编译x86_64目标文件并链接对应库:
    clang++ -std=c++17 -Wall -fno-objc-arc -finput-charset=UTF-8 -I/usr/local/homebrew/opt/ncurses/include -arch x86_64 -c -o main-x86_64.o main.cpp
    clang++ -L/usr/local/homebrew/opt/ncurses/lib -lncurses -arch x86_64 -o test-x86_64 main-x86_64.o
    
  3. 用lipo合并两个可执行文件:
    lipo -create test-arm64 test-x86_64 -output test
    

测试代码

#define _XOPEN_SOURCE_EXTENDED
#include <ncursesw/curses.h>
#include <clocale>

int main(int argc, char* argv[]) {
    setlocale(LC_ALL, "");
    initscr();
    raw();
    noecho();
    keypad(stdscr, true);
    refresh();
    wborder_set(stdscr, WACS_D_VLINE, WACS_D_VLINE, WACS_D_HLINE, WACS_D_HLINE,
    WACS_D_ULCORNER, WACS_D_URCORNER, WACS_D_LLCORNER, WACS_D_LRCORNER);
    getch();
    endwin();
    return 0;
}

内容的提问来源于stack exchange,提问作者majinnaibu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:00:03