5

I am new to StackOverflow, Conan, and CMake.

I am trying to build a hello world program that depends on Qt with CMake. I have cloned it from a public repository cmake-hello-world and compiled it successfully.

Now, I am trying to conanize this project and I have installed the Qt/5.9@bincrafters/stable jfrog-bintray component which is readily available on bintray and changed the CMakelist.txt.

This is my conan profile:

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})

from the root of the directory, I am running these commands:

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

I am getting the following error:

[ 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

I couldn't find much help online. Please help

Muhammad Usman Bashir
  • 1,441
  • 2
  • 14
  • 43
Kanth
  • 131
  • 2
  • 6
  • You are not linking the Qt5 target you previously found with `find_package` command. Try to add `Qt5::Widgets` to the `target_link_libraries` command. The imported target also carries information about include directories among other settings. – vre Feb 27 '20 at 22:25
  • 1
    @vre: It is `CONAN_LIBS` variable which should contain all libraries found with `find_package`. – Tsyvarev Feb 27 '20 at 23:36
  • thanks for the comments. I did updated but didn't work for me. I am add more info at below comment. thanks :) – Kanth Feb 28 '20 at 05:44

3 Answers3

1

My solution is slightly simpler than the previous solutions suggested, but more importantly, this does not require to have conan specific entries in your cmake file making it portable.

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)

Then, you can just run the following commands to build your project:

conan install -pr my_profile -if build . 
cmake -GNinja -DCMAKE_TOOLCHAIN_FILE=conan_paths.cmake -Bbuild 
cmake --build build
László Papp
  • 51,870
  • 39
  • 111
  • 135
0
  • Ubuntu 18.04

Based on the conan-qt test I modified the 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)

and the conanfile.txt you use for the test is:

[requires]
qt/5.14.1@bincrafters/stable

[generators]
cmake
qt

[options]
*:shared=True

And you have to execute the command:

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

The Qt xcb plugin needs some libraries so some packages must be installed using the following command:

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

Qt 5.14 has requirements that are not available in Ubuntu 16.04 so Qt 5.9 must be installed:

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

And you have to execute the command:

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

It also installs the dependencies:

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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I have updated the CMakefile.txt and I have cloned your repo too to compile and run (https://github.com/eyllanesc/Qt-CMake-HelloWorld). I am able to compile and build successfully but when I am running I am getting below error: ``` qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem. Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb. Aborted (core dumped) ``` – Kanth Feb 28 '20 at 05:53
  • FYI, I just need any sample hello world CMake project where it has dependency of conan qt. When I compile, build and run it, it should open a window to the show "hello world" or some greet – Kanth Feb 28 '20 at 06:03
  • @Kanth execute `QT_DEBUG_PLUGINS=1 ./helloworld` and share the log you get, also tell me What is your OS? – eyllanesc Feb 28 '20 at 06:14
  • 1
    did you add the `qt` generator to your conanfile, and make sure the `qt.conf` generated file is next to your executable before starting it ? – Eric Lemanissier Feb 28 '20 at 08:35
  • @Kanth execute: `sudo apt-get install libgl1-mesa-dev libxcb-xinerama0 libxcb-shm0 libxcb-xinput0` – eyllanesc Feb 28 '20 at 13:15
  • @eyllanesc My production equivalent system is ubuntu 16.04. I tried installing but I cannot install one package (libxcb-xinput0), because this one available 18.04 or up. When I tried with Qt_Debug, I am still getting the same error - (https://github.com/ramk2020/rkadaveru/blob/master/Error_Output/conan_cmake_integrations/QT_DEBUG_OUTPUT) – Kanth Feb 28 '20 at 15:37
  • @Eric Lemanissier yes added the conanfile here github.com/ramk2020/rkadaveru/blob/master/Error_Output/conanfile.txt and yes I am also putting qt.conf file next to executable. – Kanth Feb 28 '20 at 15:43
  • I am still struggling with this. And I suspect is QT component built successfully or not. I don't know if someone able to make this integrations before. – Kanth Feb 28 '20 at 21:13
  • @eyllanesc qt package has a dependency on xcb conan package, and conan is able to build and provide xcb, so you should not have to install the system package. As a consequence it is not necessary to downgrade to Qt5.9 – Eric Lemanissier Mar 02 '20 at 13:25
  • @Kanth can you please try to add "virtualrunenv" to your generators and run "activate_run" before ./helloworld ? – Eric Lemanissier Mar 02 '20 at 13:27
  • @EricLemanissier It is not mandatory to install these packages (ie write apt-get install -X) since it seems that the conan compilation script installs them, but I had to separate them since for my test I used travis-ci and this only allows installing packages in the initial parts and not during the execution of the instruction. On the other hand I do not see anything new when activating virtualrunenv since the executable does not continue pointing to the same ".so" (maybe I am not seeing an obvious advantage since I am just starting in conan). – eyllanesc Mar 02 '20 at 16:13
  • @eyllanesc Qt 5.9 recipe used to install these packages via apt, but qt 5.14.1 does not. Instead it installs the conan version of these dependencies, which is why they are available on all distributions. Regarding the virtualrunenv, my instructions were not accurate. what you need to run is "source activate_run.sh" before ./helloworld (in the same shell) – Eric Lemanissier Mar 03 '20 at 08:41
  • The approach above will work on osx and linux, but most likely will not work on Windows since Windows does not have RPATH concept. To start conanized qt app on windows you either need to activate run env or deploy your app. – ymochurad Mar 10 '20 at 14:14
0

thank you for all your answers. The below solution worked for me and this is how my CMakeList file looks like. I am posting because the below info might help others.

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})

This my 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"]
Kanth
  • 131
  • 2
  • 6