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

SFML项目本地编译正常,GitLab CI编译报错:sf::Rect无getPosition成员

解决GitLab CI中SFML编译错误:sf::Rect<float> 无 getPosition 成员

问题根源

本地编译正常是因为你的CMake配置通过FetchContent拉取了SFML 2.6.x版本,这个版本新增了sf::Rect::getPosition()方法;而GitLab CI环境中通过apt-get install libsfml-dev安装的是系统仓库里的旧版SFML(通常Debian/Ubuntu系列仓库默认是2.5.x),该版本的sf::Rect并没有这个成员函数,因此触发编译错误。

解决方案

方案1:让CI环境复用本地的FetchContent配置(推荐)

直接删除CI配置中安装libsfml-dev的步骤,CMake的FetchContent会自动下载、编译并链接你指定的2.6.x版本SFML,无需系统预安装。修改后的CI配置如下:

image: gcc

before_script:
    - apt-get update --yes
    - apt-get install cmake --yes # Cmake
    - apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev --yes # OpenGL
    - apt-get install freetype* --yes # freetype
    - apt-get install libogg-dev --yes # OGG
    - apt-get install libvorbis-dev --yes # vorbis
    - apt-get install libflac-dev --yes # flac
    - apt-get install libudev-dev --yes # UDev
    - apt-get install libopenal1 libopenal-dev --yes # OpenAL
    - apt-get install libx11-dev libxrandr-dev libxcursor-dev --yes # X11

stages:
    - build

BuildCode:
    stage: build
    script:
        - cmake -B build -S .
        - cmake --build build
    artifacts:
        paths:
            - "build/bin/"
        when: on_success
        expire_in: 30 days

方案2:在CI中安装指定版本的SFML包

如果不想依赖FetchContent编译SFML,可以在CI中添加官方PPA来获取2.6.x版本的预编译包,修改CI的before_script部分:

before_script:
    - apt-get update --yes
    - add-apt-repository ppa:sonkun/sfml-stable --yes
    - apt-get update --yes
    - apt-get install cmake --yes # Cmake
    - apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev --yes # OpenGL
    - apt-get install freetype* --yes # freetype
    - apt-get install libogg-dev --yes # OGG
    - apt-get install libvorbis-dev --yes # vorbis
    - apt-get install libflac-dev --yes # flac
    - apt-get install libsfml-dev --yes # SFML(现在会安装2.6.x版本)
    - apt-get install libudev-dev --yes # UDev
    - apt-get install libopenal1 libopenal-dev --yes # OpenAL
    - apt-get install libx11-dev libxrandr-dev libxcursor-dev --yes # X11

额外提示

其实你代码中想获取文本的位置,直接调用text.getPosition()即可,不需要从getGlobalBounds()返回的Rect对象中获取,这可以避免依赖Rect的新增方法。

内容的提问来源于stack exchange,提问作者kirill.iceland

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:28:09