Ubuntu16.04编译OpenCV2.4.13时出现Q_WS_WIN未声明CMake错误
Hey there, let's work through this frustrating error you're facing. The Q_WS_WIN macro is a Qt-specific flag for Windows platforms—so it makes total sense it's throwing an error on Ubuntu. Older OpenCV versions like 2.4.13 have some quirks with Qt detection on non-Windows systems, but here are a few solid fixes to try:
1. Disable Qt Integration (Quickest Fix)
If you don't need OpenCV's Qt-based GUI features, the simplest solution is to tell CMake to skip Qt entirely. Delete your existing release build folder first to clear old cached configs, then run:
mkdir -p release && cd release cmake -DWITH_QT=OFF -DCMAKE_BUILD_TYPE=Release .. make -j$(nproc)
This bypasses the problematic Qt-related checks entirely.
2. Fix Qt Detection (If You Need Qt Support)
If you do need OpenCV to work with Qt, Ubuntu 16.04 ships with Qt5 by default, and Q_WS_WIN was deprecated in Qt5. Here's how to configure CMake correctly:
- First, install the required Qt5 dev packages:
sudo apt-get update && sudo apt-get install qt5-default qttools5-dev-tools - Then, run CMake with explicit Qt5 settings (and still avoid the Windows macro check):
rm -rf release && mkdir release && cd release cmake -DWITH_QT=ON -DQT_QMAKE_EXECUTABLE=/usr/bin/qmake -DCMAKE_BUILD_TYPE=Release .. make -j$(nproc)
This tells OpenCV to use the system's Qt5, which doesn't reference the outdated Q_WS_WIN macro.
3. Manually Patch OpenCV's CMake Config
If the above steps don't work, you can directly modify OpenCV's CMake scripts to skip checking for Q_WS_WIN on Linux:
- Navigate to your OpenCV source directory and open
cmake/OpenCVFindQt.cmakein a text editor. - Look for lines that look like this:
check_symbol_exists(Q_WS_WIN "${QT_INCLUDE_DIR}/QtGui/qglobal.h" HAVE_Q_WS_WIN) - Wrap that check in a Windows-only condition:
if(WIN32) check_symbol_exists(Q_WS_WIN "${QT_INCLUDE_DIR}/QtGui/qglobal.h" HAVE_Q_WS_WIN) endif() - Save the file, then re-run your CMake and build commands as usual.
This ensures the macro check only runs on Windows systems where it's relevant.
内容的提问来源于stack exchange,提问作者aasharma90

