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

CMake链接Qt时无法关联QML/QtQuick函数问题

Your issue is definitely rooted in a mixed Qt version conflict—your build is trying to link against both the system-preinstalled Qt and your manually deployed Qt 5.9.1, leading to undefined symbol errors (like the qrand()@Qt_5 references) since symbol versions don’t match across releases. Here’s how to resolve this step by step:

1. Pin Qt 5.9.1 Explicitly in CMake

Instead of relying on CMAKE_PREFIX_PATH alone (which can still pick up system Qt paths), directly set Qt5_DIR to the exact CMake config directory of your 5.9.1 installation. This tells find_package to only look for your targeted Qt version.

Modify your CMakeLists.txt like this:

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)

# Explicitly point to Qt 5.9.1's CMake config (absolute path is more reliable)
set(Qt5_DIR "/home/onilsson/DevRoot/Dependencies/Qt/v5.9.1/5.9.1/gcc_64/lib/cmake/Qt5")

# Prevent CMake from searching system paths for Qt packages
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Now find Qt modules as before
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Qml REQUIRED)
find_package(Qt5QuickControls2 REQUIRED)

target_link_libraries(${PROJECT_NAME} 
    ... 
    Qt5::Widgets 
    Qt5::Core 
    Qt5::Gui 
    Qt5::OpenGL 
    Qt5::Quick 
    Qt5::Qml 
    Qt5::QuickControls2 
)

2. Clean CMake Cache and Rebuild

Old CMake cache files often retain references to system Qt paths. Delete your build directory entirely, then re-run CMake to generate fresh build files:

rm -rf build/
mkdir build && cd build
cmake ..
make

3. Ensure Runtime Library Paths Are Correct

Even if the build succeeds, runtime might still load the system Qt libraries. Set LD_LIBRARY_PATH before running your application to prioritize your Qt 5.9.1 libraries:

export LD_LIBRARY_PATH=/home/onilsson/DevRoot/Dependencies/Qt/v5.9.1/5.9.1/gcc_64/lib:$LD_LIBRARY_PATH
./your_app_binary

Why This Works

The Qt5_DIR variable directly points CMake to the version-specific CMake configuration files (instead of letting it search arbitrary paths). The CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY flag ensures CMake doesn’t fall back to system-installed Qt packages if it can’t find your targeted version.

Your original error messages (like the libQt5Sql.so.5 conflict warning) confirm that CMake was seeing both system and custom Qt libraries—this setup eliminates that ambiguity entirely.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:17:52