【问题标题】:cmake, conan, and qt hello world project build errorcmake、conan 和 qt hello world 项目构建错误
【发布时间】:2020-06-12 00:06:18
【问题描述】:

我是 StackOverflow、Conan 和 CMake 的新手。

我正在尝试使用 CMake 构建一个依赖于 Qt 的 hello world 程序。我已经从公共存储库cmake-hello-world 克隆了它并成功编译。

现在,我正在尝试 conanize 这个项目,我已经安装了 Qt/5.9@bincrafters/stable jfrog-bintray 组件,该组件在 bintray 上很容易获得,并更改了 CMakelist.txt。

这是我的柯南简介:

cat ~/.conan/profiles/default

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=5
compiler.libcxx=libstdc++
build_type=Release
[options]
[build_requires]
[env]

CMakeList.txt:

cmake_minimum_required(VERSION 3.5)

project(helloworld)

# If conan is being used, configure CMake to use conan for dependencies.
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# There may be a way to tell up front if Qt5 is going to be found, but I haven't found
# a foolproof way to do it yet, so settle for the default error message for now.
#if(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)
#    message(WARNING "CMAKE_PREFIX_PATH is not defined, so find_package may not work. 
Set the CMAKE_PREFIX_PATH "
#            "environment variable to the install prefix of Qt 5, either on the command line as "
#            "-DCMAKE_PREFIX_PATH=\"path/to/Qt5/lib/cmake\" or with set(CMAKE_PREFIX_PATH path/to/Qt5/lib/cmake)")
#endif(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)

# Add a compiler flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Make this a GUI application on Windows
if(WIN32)
    set(CMAKE_WIN32_EXECUTABLE ON)
endif()



# Find the QtWidgets library
find_package(Qt5 REQUIRED COMPONENTS Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp mainwindow.cpp mainwindow.ui resources.qrc)

# Add the Qt5 Widgets for linking
target_link_libraries(helloworld ${CONAN_LIBS})

从目录的根目录,我正在运行这些命令:

mkdir build
cd build
conan install .. --build missing
cmake ..
make

我收到以下错误:

[ 16%] Automatic moc, uic and rcc for target helloworld
[ 16%] Built target helloworld_automoc
[ 33%] Building CXX object CMakeFiles/helloworld.dir/main.cpp.o
In file included from /home/ram2020/temp/temp/working_qt_tar/Qt-CMake-HelloWorld/main.cpp:1:0:
/home/ram2020/temp/temp/working_qt_tar/Qt-CMake-HelloWorld/mainwindow.h:4:23: fatal error: QMainWindow: No such file or directory
compilation terminated.
CMakeFiles/helloworld.dir/build.make:62: recipe for target 'CMakeFiles/helloworld.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/helloworld.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/helloworld.dir/all' failed
make[1]: *** [CMakeFiles/helloworld.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

我在网上找不到太多帮助。请帮忙

【问题讨论】:

  • 您没有使用find_package 命令链接您之前找到的Qt5 目标。尝试将Qt5::Widgets 添加到target_link_libraries 命令。导入的目标还包含有关包含目录以及其他设置的信息。
  • @vre:它是CONAN_LIBS 变量,它应该包含使用find_package 找到的所有库。
  • 感谢 cmets。我确实更新了,但没有为我工作。我在下面的评论中添加更多信息。谢谢:)

标签: c++ qt ubuntu cmake conan


【解决方案1】:
  • Ubuntu 18.04

基于conan-qt test我修改了CMakeLists.txt:

cmake_minimum_required(VERSION 3.8.2)

project(helloworld)

# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# There may be a way to tell up front if Qt5 is going to be found, but I haven't found
# a foolproof way to do it yet, so settle for the default error message for now.
#if(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)
#    message(WARNING "CMAKE_PREFIX_PATH is not defined, so find_package may not work. Set the CMAKE_PREFIX_PATH "
#            "environment variable to the install prefix of Qt 5, either on the command line as "
#            "-DCMAKE_PREFIX_PATH=\"path/to/Qt5/lib/cmake\" or with set(CMAKE_PREFIX_PATH path/to/Qt5/lib/cmake)")
#endif(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)

# Add a compiler flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Make this a GUI application on Windows
if(WIN32)
  set(CMAKE_WIN32_EXECUTABLE ON)
endif()

# Find the QtWidgets library
find_package(Qt5 REQUIRED COMPONENTS Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp mainwindow.cpp mainwindow.ui resources.qrc)

# Add the Qt5 Widgets for linking
target_link_libraries(helloworld Qt5::Widgets)

您用于测试的 conanfile.txt 是:

[requires]
qt/5.14.1@bincrafters/stable

[generators]
cmake
qt

[options]
*:shared=True

你必须执行命令:

mkdir build
cd build
conan install ..
cmake ..
make

Qt xcb 插件需要一些库,因此必须使用以下命令安装一些包:

sudo apt-get install libgl1-mesa-dev libxcb-xinerama0 libxcb-shm0 libxcb-xinput0
  • Ubuntu 16.04

Qt 5.14 的要求在 Ubuntu 16.04 中不可用,因此必须安装 Qt 5.9:

cmake_minimum_required(VERSION 3.1.0)

project(helloworld)

# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

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

# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# There may be a way to tell up front if Qt5 is going to be found, but I haven't found
# a foolproof way to do it yet, so settle for the default error message for now.
#if(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)
#    message(WARNING "CMAKE_PREFIX_PATH is not defined, so find_package may not work. Set the CMAKE_PREFIX_PATH "
#            "environment variable to the install prefix of Qt 5, either on the command line as "
#            "-DCMAKE_PREFIX_PATH=\"path/to/Qt5/lib/cmake\" or with set(CMAKE_PREFIX_PATH path/to/Qt5/lib/cmake)")
#endif(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)

# Add a compiler flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Make this a GUI application on Windows
if(WIN32)
  set(CMAKE_WIN32_EXECUTABLE ON)
endif()

# Find the QtWidgets library
find_package(Qt5 REQUIRED COMPONENTS Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp mainwindow.cpp mainwindow.ui resources.qrc)

# Add the Qt5 Widgets for linking
target_link_libraries(helloworld Qt5::Widgets)
[requires]
Qt/5.9@bincrafters/stable

[generators]
cmake

[options]
*:shared=True

你必须执行命令:

mkdir build
cd build
conan install .. --build Qt
cmake ..
make

它还安装依赖项:

sudo apt-get install -y --no-install-recommends \
    libxcursor-dev \
    libxfixes-dev \
    libxi-dev \
    libgl1-mesa-dev \
    libxrandr-dev \
    libx11-xcb-dev \
    libxcb-keysyms1 \
    libxcb-keysyms1-dev \
    libxcb-image0 \
    libxcb-image0-dev \
    libxcb-icccm4 \
    libxcb-icccm4-dev \
    libxcb-sync-dev \
    libxcb-xfixes0-dev \
    libxcb-shape0-dev \
    libxcb-render-util0-dev \
    libxcb-randr0-dev \
    libxcb-render-util0 \
    libxcb-glx0-dev \
    libxcb-xinerama0 \
    libxcb-xinerama0-dev

【讨论】:

  • 我已经更新了 CMakefile.txt 并且我已经克隆了你的 repo 来编译和运行 (github.com/eyllanesc/Qt-CMake-HelloWorld)。我能够成功编译和构建,但是在运行时出现以下错误:``` qt.qpa.plugin:即使找到了,也无法在“”中加载 Qt 平台插件“xcb”。此应用程序无法启动,因为无法初始化 Qt 平台插件。重新安装应用程序可能会解决此问题。可用的平台插件有:eglfs、linuxfb、minimal、minimalegl、offscreen、vnc、xcb。中止(核心转储)```
  • 仅供参考,我只需要任何具有 conan qt 依赖项的示例 hello world CMake 项目。当我编译、构建和运行它时,它应该会打开一个窗口显示“hello world”或一些问候
  • @Kanth 执行QT_DEBUG_PLUGINS=1 ./helloworld 并分享你得到的日志,同时告诉我你的操作系统是什么?
  • 您是否将qt 生成器添加到您的conanfile 中,并在启动之前确保qt.conf 生成的文件位于您的可执行文件旁边?
  • @Kanth 执行:sudo apt-get install libgl1-mesa-dev libxcb-xinerama0 libxcb-shm0 libxcb-xinput0
【解决方案2】:

感谢您的所有回答。下面的解决方案对我有用,这就是我的 CMakeList 文件的样子。我发帖是因为以下信息可能对其他人有所帮助。

cmake_minimum_required(VERSION 3.5)

project(helloworld)


# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_VERBOSE_MAKEFILE TRUE)

# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

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

include_directories(${CONAN_INCLUDE_DIRS})
# There may be a way to tell up front if Qt5 is going to be found, but I haven't found
# a foolproof way to do it yet, so settle for the default error message for now.
#if(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)
#    message(WARNING "CMAKE_PREFIX_PATH is not defined, so find_package may not work. Set the CMAKE_PREFIX_PATH "
#            "environment variable to the install prefix of Qt 5, either on the command line as "
#            "-DCMAKE_PREFIX_PATH=\"path/to/Qt5/lib/cmake\" or with set(CMAKE_PREFIX_PATH path/to/Qt5/lib/cmake)")
#endif(NOT CMAKE_PREFIX_PATH AND NOT Qt5Widgets_DIR)

# Add a compiler flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Make this a GUI application on Windows
if(WIN32)
  set(CMAKE_WIN32_EXECUTABLE ON)
endif()

# Find the QtWidgets library
find_package(Qt5 REQUIRED COMPONENTS Widgets)

# Tell CMake to create the helloworld executable
add_executable(helloworld main.cpp mainwindow.cpp mainwindow.ui resources.qrc)

# Add the Qt5 Widgets for linking
target_link_libraries(helloworld Qt5::Widgets)
# target_link_libraries(helloworld ${CONAN_LIBS})

这是我的 conanfile.py

from conan_base import <your import module here>


 class HelloWorld(your import module here):
     name = "HelloWorld"
     license = "BSD"
     author = "your name"
     url = "git@gitlab.com:<url/project>"
     description = "Base Library A of the example project."
     topics = ("<Put some tag here>", "<here>", "<and here>")

     def build_requirements(self):
         self.build_requires("gtest/1.8.1@bincrafters/stable")

     def requirements(self):
         self.requires("Qt/5.11.2@bincrafters/stable")

        # def package_info(self):
        #     self.cpp_info.libs = ["BaseLibBLibrary"]

【讨论】:

  • 这是在 ubuntu 16.04 上
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 2014-12-23
  • 2018-08-18
  • 1970-01-01
相关资源
最近更新 更多