1

I'm trying to migrate a QtProject file, exactly the QtAwesome library from https://github.com/gamecreature/QtAwesome

The project file that im trying to migrate is:

TARGET = QtAwesome
TEMPLATE = lib
CONFIG += staticlib c++11

SOURCES += QtAwesome.cpp
HEADERS += QtAwesome.h

isEmpty(PREFIX) {
    unix {
        PREFIX = /usr
    } else {
        PREFIX = $$[QT_INSTALL_PREFIX]
    }
}

install_headers.files = QtAwesome.h
install_headers.path = $$PREFIX/include
target.path = $$PREFIX/lib
INSTALLS += install_headers target

RESOURCES += \
    QtAwesome.qrc

And my translation is:

cmake_minimum_required(VERSION 2.8.11)

project(QtAwesome)
file(GLOB_RECURSE CODE_FILES QtAwesome.cpp)
find_package (Qt5Core REQUIRED)
ADD_DEFINITIONS(-DQTAWESOME_LIBRARY)

qt5_add_resources(RESOURCE_FILES QtAwesome.qrc)
qt5_wrap_cpp(LIB_HEADER_MOC QtAwesome.h)

add_library(QtAwesome STATIC QtAwesome.cpp QtAwesome.h)
target_link_libraries(QtAwesome
    Qt5::Widgets
)

It compiles, but when i try to use this built library i get the following error:

[100%] Linking CXX executable MyProject.exe
QtAwesome.lib(QtAwesome.cpp.obj) : error LNK2019: unresolved external symbol "int __cdecl qInitResources_QtAwesome(void)" (?qInitResources_QtAwesome@@YAHXZ) referenced in function "public: bool __cdecl QtAwesome::initFontAwesome(void)" (?initFontAwesome@QtAwesome@@QEAA_NXZ)

MyProject.exe : fatal error LNK1120: 1 unresolved externals
rocambille
  • 15,398
  • 12
  • 50
  • 68
Alejandro Rosas
  • 272
  • 1
  • 5
  • 12

2 Answers2

2

There are several issues in your cmake project:

file(GLOB_RECURSE CODE_FILES QtAwesome.cpp)

You're not using CODE_FILES anywhere. Moreover, using glob with a file name instead of a file pattern is non-sense, and glob should not be used to collect sources (see the documentation).

ADD_DEFINITIONS(-DQTAWESOME_LIBRARY)

QTAWESOME_LIBRARY is not used anywhere in QtAwesome's code (see search result in the repository). Anyway, if you must add defintions, you may consider target_compile_definitions instead.

qt5_add_resources(RESOURCE_FILES QtAwesome.qrc)
qt5_wrap_cpp(LIB_HEADER_MOC QtAwesome.h)

RESOURCE_FILES and LIB_HEADER_MOC are not added to the sources when calling add_library: resources and moc files will not be compiled into your library (I guess that is your main issue here). You may consider using CMAKE_AUTOMOCand CMAKE_AUTORCC instead.

find_package (Qt5Core REQUIRED)

# ...

target_link_libraries(QtAwesome
    Qt5::Widgets
)

You're linking against the Widgets module while only looking for the Core module.

You didn't require C++11 standard in your cmake project: you may require it using CMAKE_CXX_STANDARD, or, if you know which part of the standard is required, using compile features.

Once cleaned, your cmake project may look like this:

cmake_minimum_required(VERSION 2.8.11)

project(QtAwesome)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt5 REQUIRED COMPONENTS Widgets)

add_library(QtAwesome STATIC
    QtAwesome.cpp
    QtAwesome.h
    QtAwesome.qrc
)
target_link_libraries(QtAwesome
    Qt5::Widgets
)
rocambille
  • 15,398
  • 12
  • 50
  • 68
0

I found this very helpful https://github.com/qt/qtbase/blob/dev/util/cmake/run_pro2cmake.py

I was able to convert a qmake (.pro) project to cmake. It generated a working CMakeLists.txt