使用CMake链接Qt时QStringList等符号未找到的问题求助
Hey there, let's work through this linking issue you're hitting with Qt5 and CMake. The error about missing QStringList (and related Qt symbols) almost always boils down to one key step missing in your CMake configuration—not linking the Qt5 libraries to your executable. Let's break down the fixes step by step:
1. The Critical Missing Step: Link Qt5 Libraries to Your Target
Your current CMake code finds the Qt5 components, but it doesn't tell CMake to link those libraries to your stride executable. That's why the linker can't find symbols like QStringList (which lives in Qt5Core).
Add the target_link_libraries command right after your add_executable call. This is the most important fix.
2. Clean Up and Optimize Your CMake Configuration
Here's a revised, complete version of your CMakeLists.txt with all necessary parts:
# Specify minimum CMake version (Qt 5.10 works well with 3.5+) cmake_minimum_required(VERSION 3.5) # Name your project project(stride) # Enable automatic handling of Qt's MOC, UIC, and RCC tools set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) # Add this if you use .ui files set(CMAKE_AUTORCC ON) # Add this if you use .qrc resource files # Point CMake to your Qt5 installation set(CMAKE_PREFIX_PATH "/opt/Qt/5.10.1/gcc_64") # Find required Qt5 components find_package(Qt5 COMPONENTS Core Widgets Gui REQUIRED) # Add your executable with all source files add_executable(stride sim/main.cpp # Replace with your actual source files # Add other .cpp/.h files here as needed ) # Link Qt5 libraries to your executable (THIS IS THE KEY FIX!) target_link_libraries(stride PRIVATE Qt5::Core Qt5::Widgets Qt5::Gui )
3. Additional Checks to Ensure Success
- Verify Qt Paths: Double-check that
/opt/Qt/5.10.1/gcc_64is the correct path to your Qt5 installation. Thelib/cmake/Qt5subdirectory should exist here, containing the Qt5 config files CMake needs. - Clean CMake Cache: If you've run CMake before with incorrect settings, delete your entire build directory and re-run
cmake ..from a fresh build folder. Old cache entries can cause unexpected issues. - Check Header Includes: Make sure your source files include the necessary Qt headers (e.g.,
#include <QStringList>forQStringListusage). While this isn't the cause of your linking error, it's good to confirm to avoid follow-up issues.
Once you make these changes, the linker should be able to find all Qt5 symbols, including QStringList, and your build should complete successfully.
内容的提问来源于stack exchange,提问作者Jens Bijtebier

