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

WSL Ubuntu下CMake+Conan构建FLTK项目报Xft链接错误

问题背景

本人接触C++/CMake时间不长,希望使用CMake搭配Conan包管理器创建FLTK项目。当前运行环境为Windows 11系统下的WSL(Ubuntu 20.04),所用WSL版本已支持GUI应用运行。

不使用Conan时,通过命令行调用fltk-config编译官方“fltk-hello-world”示例可正常运行,但使用Conan配合CMake构建项目时出现如下链接错误:

/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Font_Descriptor::Fl_Font_Descriptor(char const*, int, int)':
fl_font.cxx:(.text+0x232): undefined reference to `XftFontOpenXlfd'
/usr/bin/ld: fl_font.cxx:(.text+0x47e): undefined reference to `XftFontMatch'
/usr/bin/ld: fl_font.cxx:(.text+0x48f): undefined reference to `XftFontOpenPattern'
/usr/bin/ld: fl_font.cxx:(.text+0x4e1): undefined reference to `XftFontOpen'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::width(char const*, int)':
fl_font.cxx:(.text+0x954): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::width(unsigned int)':
fl_font.cxx:(.text+0xa5e): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::text_extents(char const*, int, int&, int&, int&, int&)':
fl_font.cxx:(.text+0xb07): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x1140): undefined reference to `XftDrawChange'
/usr/bin/ld: fl_font.cxx:(.text+0x1176): undefined reference to `XftDrawSetClip'
/usr/bin/ld: fl_font.cxx:(.text+0x1209): undefined reference to `XftDrawString32'
/usr/bin/ld: fl_font.cxx:(.text+0x12d9): undefined reference to `XftDrawCreate'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::rtl_draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x149b): undefined reference to `XftTextExtents32'
/usr/bin/ld: fl_font.cxx:(.text+0x14cc): undefined reference to `XftDrawChange'
/usr/bin/ld: fl_font.cxx:(.text+0x1502): undefined reference to `XftDrawSetClip'
/usr/bin/ld: fl_font.cxx:(.text+0x1595): undefined reference to `XftDrawString32'
/usr/bin/ld: fl_font.cxx:(.text+0x15f9): undefined reference to `XftDrawCreate'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `fl_destroy_xft_draw(unsigned long)':
fl_font.cxx:(.text+0x10d9): undefined reference to `XftDrawChange'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/hello.dir/build.make:84: bin/hello] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

当前使用的CMakeLists.txt配置如下:

cmake_minimum_required(VERSION 3.2.3)

project(Hello VERSION 0.1.0)

set(EXECUTABLE_NAME hello)
set(EXE_SOURCES hello.cc)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${EXECUTABLE_NAME} ${EXE_SOURCES})

target_include_directories(hello PUBLIC ${FLTK_INCLUDE_DIRS})

target_link_libraries(hello
    ${CONAN_LIBS})

推测问题为未正确链接Xft库导致,但不清楚具体配置方法,不确定是否需要在target_link_libraries中添加对应链接项。

解决方案

判断没错,这个报错就是Xft库没链接上导致的。Conan 1.x的FLTK包在Linux平台不会自动传递X11相关的系统依赖,需要手动补配置,按下面步骤处理:

  • 先确认系统装全了FLTK依赖的X11开发包,执行以下命令安装:
sudo apt update && sudo apt install libxft-dev libx11-dev libxext-dev libxinerama-dev libxcursor-dev

哪怕直接用fltk-config能正常编译,也建议先跑一遍这个命令补全依赖,避免后续出其他链接错误。

  • 修改CMakeLists.txt,在链接阶段加入Xft和其他FLTK依赖的X11系统库,修改后的完整配置如下:
cmake_minimum_required(VERSION 3.2.3)

project(Hello VERSION 0.1.0)

set(EXECUTABLE_NAME hello)
set(EXE_SOURCES hello.cc)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(${EXECUTABLE_NAME} ${EXE_SOURCES})

# 查找系统X11及相关扩展库
find_package(X11 REQUIRED)

target_include_directories(hello PUBLIC ${FLTK_INCLUDE_DIRS} ${X11_INCLUDE_DIR})

target_link_libraries(hello
    ${CONAN_LIBS}
    # 补充X11相关依赖库,注意链接顺序:Xft要放在X11基础库前面
    ${X11_Xft_LIB}
    ${X11_Xinerama_LIB}
    ${X11_Xext_LIB}
    ${X11_Xcursor_LIB}
    ${X11_LIBRARIES}
    fontconfig
    pthread
    dl
)
  • 清空之前的构建缓存目录,重新走一遍Conan安装、CMake配置、编译的流程就能正常构建了。

补充:如果用的是Conan 2.x版本,直接用CMake自带的find_package(fltk)配合Conan的CMakeDeps生成器即可,新版FLTK的Conan包已经修复了系统依赖传递的问题,不需要手动补这些链接项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 18:06:08