0

I only find answers to questions where people want to link Qt statically instead of dynamically with cmake. So I really wonder why I have this problem alone.

Maybe it is because I use MSVC2017 Qt 5.12 Kit and not MinGW.

These are my CMake Files: just a blank Qt/QML main with a dummy QObject class in another lib. Everything is fine if I build my lib STATIC, but when I tried building SHARED I saw in the error message below, cmake creates a build command that also links the Qt libraries statically.

I do not want that for licence options. If my lib links statically it's ok but not Qt.

All the Qt libs are available as dll in D:\Dev\Qt.5.12\5.12.0\msvc2017_64\bin - so I do not see the problem. Would be great if somebody here has an idea.

Executable CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)

project(DCTest LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Core Quick REQUIRED)

add_subdirectory(lib)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/lib")
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc")
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PUBLIC Qt5::Core Qt5::Quick qmlsightlinesolver2d)

Library CMakeLists.txt: (the lib should also build standalone that's why it has all the options)

cmake_minimum_required(VERSION 3.1)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)

// All builds and runs fine (but still statically) when I put STATIC here
add_library(qmlsightlinesolver2d SHARED "qmlsightlinesolver2d.h" "qmlsightlinesolver2d.cc")
target_compile_definitions(qmlsightlinesolver2d PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(qmlsightlinesolver2d PRIVATE Qt5::Core Qt5::Quick)

Compile Output:

00:04:04: Running steps for project DCTest...
00:04:04: Starting: "C:\Program Files (x86)\CMake\bin\cmake.exe" --build . --target all
[1/10 23.7/sec] Automatic MOC for target qmlsightlinesolver2d
[2/10 2.3/sec] Building CXX object lib\CMakeFiles\qmlsightlinesolver2d.dir\qmlsightlinesolver2d.cc.obj
[3/10 3.4/sec] Building CXX object lib\CMakeFiles\qmlsightlinesolver2d.dir\qmlsightlinesolver2d_autogen\mocs_compilation.cpp.obj
[4/10 3.2/sec] Linking CXX shared library lib\qmlsightlinesolver2d.dll
LINK : lib\qmlsightlinesolver2d.dll not found or not built by the last incremental link; performing full link
[5/10 3.8/sec] Automatic MOC for target DCTest
[6/10 4.4/sec] Automatic RCC for qml.qrc
[7/10 4.8/sec] Building CXX object CMakeFiles\DCTest.dir\DCTest_autogen\mocs_compilation.cpp.obj
[8/10 5.5/sec] Building CXX object CMakeFiles\DCTest.dir\DCTest_autogen\EWIEGA46WW\qrc_qml.cpp.obj
[9/10 3.7/sec] Building CXX object CMakeFiles\DCTest.dir\main.cpp.obj
[10/10 3.9/sec] Linking CXX executable DCTest.exe
FAILED: DCTest.exe 
cmd.exe /C "cd . && "C:\Program Files (x86)\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\DCTest.dir --manifests  -- C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\bin\Hostx86\x64\link.exe /nologo CMakeFiles\DCTest.dir\main.cpp.obj CMakeFiles\DCTest.dir\DCTest_autogen\mocs_compilation.cpp.obj CMakeFiles\DCTest.dir\DCTest_autogen\EWIEGA46WW\qrc_qml.cpp.obj  /out:DCTest.exe /implib:DCTest.lib /pdb:DCTest.pdb /version:0.0  /machine:x64 /debug /INCREMENTAL /subsystem:console  D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Quickd.lib lib\qmlsightlinesolver2d.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Qmld.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Networkd.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Guid.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Cored.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~2\MIB055~1\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\bin\Hostx86\x64\link.exe /nologo CMakeFiles\DCTest.dir\main.cpp.obj CMakeFiles\DCTest.dir\DCTest_autogen\mocs_compilation.cpp.obj CMakeFiles\DCTest.dir\DCTest_autogen\EWIEGA46WW\qrc_qml.cpp.obj /out:DCTest.exe /implib:DCTest.lib /pdb:DCTest.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Quickd.lib lib\qmlsightlinesolver2d.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Qmld.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Networkd.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Guid.lib D:\Dev\Qt.5.12\5.12.0\msvc2017_64\lib\Qt5Cored.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\DCTest.dir/intermediate.manifest CMakeFiles\DCTest.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : DCTest.exe not found or not built by the last incremental link; performing full link
LINK : fatal error LNK1104: cannot open file 'lib\qmlsightlinesolver2d.lib'
ninja: build stopped: subcommand failed.
00:04:06: The process "C:\Program Files (x86)\CMake\bin\cmake.exe" exited with code 1.
Error while building/deploying project DCTest (kit: Desktop Qt 5.12.0 MSVC2017 64bit)
The kit Desktop Qt 5.12.0 MSVC2017 64bit has configuration issues which might be the root cause for this problem.
When executing step "CMake Build"
00:04:06: Elapsed time: 00:03.
Marcel Blanck
  • 867
  • 7
  • 12
  • 1
    CMake actually links with a dynamic library, the file `lib\qmlsightlinesolver2d.lib` is a shared library export file, not a static library. In your case the file isn't created, probably, because your library doesn't expors any symbol. – Tsyvarev Feb 22 '19 at 23:56
  • Ah wait... I'll check it – Marcel Blanck Feb 23 '19 at 00:31
  • 1
    When cmake detects Qt, it detects a particular installation of Qt, and that installation can **either** be built for static or dynamic linking, but not both. So you don't have any problem at all: when you link with a copy of Qt built for dynamic linking, that's how it will be linked, period, and there's no way to change that other than by building a statically linked copy of Qt, and pointing cmake to that. Since the Qt project itself doesn't distribute Qt for static linking, and since you didn't build a statically linked copy of Qt yourself, you're basically seeing what isn't there. – Kuba hasn't forgotten Monica Feb 23 '19 at 23:03
  • Thanks for the hints both of you. The reason for my own lib not dynamically linking really was the missing __declspec(dllexport). And I think the thing about the Qt Version is correct, maybe there is only Qt 5.12 static right now released for msvc. – Marcel Blanck Feb 25 '19 at 21:29

0 Answers0