2

I was looking up how to compile QT dynamically linked projects in cmake with clion IDE. I've run into issues with the following project set up. I've followed this tutorial, compiled, and ran it with mingw32 bit and a kit with that links to Qt compiled with mingw-w64 (whose qmake file is found in C:/msys64/mingw64/bin)

My project setup looks like this:

CMakeLists.txt
main.cpp
notepad.cpp
notepad.h
notepad.ui

In clion, I've used the follwing cmake file in order to build this project.

cmake_minimum_required(VERSION 3.7)
set(CMAKE_PREFIX_PATH C:/msys64/mingw64/bin)
project(qttest)

set(QT_ROOT_DIR "C:/msys64/mingw64/bin")

set(QT_QMAKE_EXECUTABLE ${QT_ROOT_DIR}/qmake)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)


find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)

add_library(notepad notepad.cpp)
target_link_libraries (notepad Qt5::Core Qt5::Widgets Qt5::Gui)




set(SOURCE_FILES main.cpp)
add_executable(qttest ${SOURCE_FILES})
target_link_libraries(qttest notepad)

After getting the following errors on build:

{projectdir}/qttest/main.cpp:6: undefined reference to `__imp__ZN12QApplicationC1ERiPPci'
... (1000 of these types of errors) ...
CMakeFiles\qttest.dir\build.make:127: recipe for target 'qttest.exe' failed
CMakeFiles\Makefile2:104: recipe for target 'CMakeFiles/qttest.dir/all' failed
mingw32-make.exe[3]: *** [qttest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/qttest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/qttest.dir/rule] Error 2
CMakeFiles\Makefile2:116: recipe for target 'CMakeFiles/qttest.dir/rule' failed
Makefile:130: recipe for target 'qttest' failed
mingw32-make.exe: *** [qttest] Error 2

I googled for a solution, and it appears that it was linking to the 32bit mingw installation of QT. This would make sense since I'm compiling with mingw-w64, so I look for a solution here(to include qt via findpackages) to solve this issue. This doesn't really work, so I go to find another solution here, which also happens to not work (setting enviroment variable to qmake directory). This one also failed do do anything (appending path variable specifically to C:/msys64/mingw64/bin).

Not sure what the issue is, but any help would be appreciated.

EDIT: Current sitatuation, I feel like I've tried everything, but I foudn that when doing the following:

get_target_property(QtCore_location Qt5::Core LOCATION)
message("qtcore location ${QtCore_location}")

the cmake output is actually

qtcore location C:/ProgramData/Anaconda3/Library/bin/Qt5Core.dll

So clearly its linking to my anaconda installation, but why how do I stop it!!! It doesn't seem to want to use anything else, Anaconda appears in path but I need to keep it there.

Community
  • 1
  • 1
Krupip
  • 4,404
  • 2
  • 32
  • 54

1 Answers1

1

I figured it out...

This was a cmake cache issue most likely. I had thought It might have been this type of issue before, but I was clearing the wrong cmake version cache. I installed a different version of cmake on my computer (which is newer) than the one clion came with (I told it to use the default version i hadn't installed). So by deleting that cmake's cache it did absolutely nothing to my clion version of cmake (so IIRC from reading cmake docs, things like CMAKE_PREFIX_PATH would use cache versions by default) This made it impossible to switch off of anaconda version despite it, for example, when I put in the wrong version info IE find_package(Qt5 8.0 REQUIRED COMPONENTS Core Widgets Gui) clearly understanding and knowing the installation directory of the 5.8.0 directory (since it listed it):

Could not find a configuration file for package "Qt5" that is compatible
  with requested version "8".

  The following configuration files were considered but not accepted:

    C:/msys64/mingw64/lib/cmake/Qt5/Qt5Config.cmake, version: 5.8.0
    C:/ProgramData/Anaconda3/Library/lib/cmake/Qt5/Qt5Config.cmake, version: 5.6.2

Cmake couldn't let me use that version when I set the cmake path var, switching between both versions of cmake now builds correctly (i believe clion might clear cache when you switch from the internal version, I'm not sure). So hopefully any one else who happens upon this particular problem will now be able to fix it.

The tutorial actually working!

picture of qt working with cmake in clion

Krupip
  • 4,404
  • 2
  • 32
  • 54