gpt4 book ai didi

c++ - cmake、conan 和 qt hello world 项目构建错误

转载 作者:行者123 更新时间:2023-12-01 17:54:35 25 4
gpt4 key购买 nike

我是 StackOverflow、Conan 和 CMake 的新手。

我正在尝试使用 CMake 构建一个依赖于 Qt 的 hello world 程序。我已经从公共(public)存储库 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

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

最佳答案

我的解决方案比之前建议的解决方案稍微简单一些,但更重要的是,这不需要在您的 cmake 文件中包含 conan 特定条目,使其可移植。

conanfile.py

from conans import ConanFile

class TestbedConan(ConanFile):
name = "Testbed"
description = "Testbed"
license = "None"
url = "None"
settings = "os", "arch", "compiler", "build_type"
generators = [
"cmake_paths",
]

requires = [
"Qt/5.15.2"
]

CMakeLists.txt

cmake_minimum_required(VERSION 3.21.0)

project(helloworld VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

add_executable(helloworld
main.cpp
)

target_link_libraries(helloworld Qt::Widgets)

然后,您只需运行以下命令即可构建您的项目:

conan install -pr my_profile -if build . 
cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=conan_paths.cmake -Bbuild
cmake --build build

关于c++ - cmake、conan 和 qt hello world 项目构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60442347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com