2
votes

I have a problem building my Qt5 project via cmake.

I run the command cmake .. && make from the directory build and I receive the following error:

/usr/bin/ld: cannot find -lengine-lib
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/blacklist-engine-cli.dir/build.make:102: src/blacklist-engine-cli] Error 1
make[1]: *** [CMakeFiles/Makefile2:117: src/CMakeFiles/blacklist-engine-cli.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I have searched this topic briefly, however when I ran this project without Qt5Sql, using only Qt5Core I have no problem at all building the project. In order to build the project without Qt5Sql I just have to remove the db folder, and delete lines referring to that in my other CMakeLists.txt files. My question is:

Why does it work if I want to include only Qt5Core, and why does it not work when I also include Qt5Sql? What am I doing wrong including Qt5Sql?

Please do not include answers related to QtCreator, or Qt installation errors. I have checked my Qt installation folder, and I have Qt5Core and Qt5Sql on the same directory level installed.

I am using Ubuntu 20.04, cmake version 3.16.3, Qt version 5.12.8

ls /usr/lib/x86_64-linux-gnu/cmake

Qt5 Qt5Core Qt5Gui Qt5OpenGL Qt5PrintSupport Qt5Test Qt5Xml Qt5Concurrent Qt5DBus Qt5Network Qt5OpenGLExtensions Qt5Sql Qt5Widgets

I have the following structure in my project:

root
├── CMakeModules
│   └── Qt.cmake
├── build
├── src
│   ├── db
│   │    ├── dbmanager.cpp
│   │    ├── dbmanager.h
│   │    └── CMakeLists.txt
│   ├── engine
│   │    ├── scanner.cpp
│   │    ├── scanner.h
│   │    └── CMakeLists.txt
│   ├── CMakeLists.txt
│   └── main.cpp
└── CMakeLists.txt

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

project(blacklist-engine)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules)

add_subdirectory(src)

CMakeModules/Qt.cmake:

set(CMAKE_AUTOMOC ON)

find_package(Qt5 REQUIRED COMPONENTS Core Sql)

src/CMakeLists.txt:

include(Qt)

add_subdirectory(
    db   
    engine
)

add_executable(blacklist-engine-cli main.cpp)

target_link_libraries(
    blacklist-engine-cli
    Qt5::Core
    Qt5::Sql
    engine-lib
    db-lib
)

src/main.cpp:

#include <QtCore>

#include "db/dbmanager.h"
#include "engine/scanner.h"
...

src/db/CMakeLists.txt (updated):

set (db-lib-source
    dbmanager.h
    dbmanager.cpp
)

add_library(db-lib ${db-lib-source})

target_link_libraries(
    db-lib
    Qt5::Sql    
)

src/db/dbmanager.h:

#include <QtSql/QSqlDatabase>
...

src/db/dbmanager.cpp:

#include "dbmanager.h"

#include <QtSql/QSqlQuery>
...

src/engine/CMakeLists.txt:

set(engine-lib-source
    scanner.h
    scanner.cpp
)

add_library(engine-lib ${engine-lib-source})

src/engine/scanner.h:

#include <QtCore>
...

src/engine/scanner.cpp:

#include "scanner.h"
...
1
shouldn't it just be `#include <QSqlDatabase>'?Alan Birtles
Who downvoted this question could explain it to me, what did I wrong questioning this topic? Sorry for being a beginner in cmake, but I think I proposed my question briefly and I don't understand why you had to downvote this... I have been struggling with this problem for 2 days now... There are multiple other topics regarding cmake that are just 2 lines and upvoted... I don't blame you for downvoting me, just explain what did I do wrong and how can I improve...mordes
I don't understand what is the include(Qt) for in your CMakeLists.txt. Do you need to add this include directory?ignacio
Maybe engine-lib did not successfully build.drescherjm
I'm not sure why this question was down-voted. It's a great, well-documented question, and indicates the issue was already researched.squareskittles

1 Answers

2
votes

The reason for the error is because engine-lib is never built, and its CMake file is never even processed. The offending line in your CMake file is this one:

add_subdirectory(
    db   
    engine
)

When using add_subdirectory in this manner, the second argument becomes the binary directory for the generated content related to db. As a result, you may notice that CMake placed some build artifacts in your src/engine directory, which is probably not what you want.

To fix this, you must call add_subdirectory consecutive times for including multiple sub-directories.

add_subdirectory(db)
add_subdirectory(engine)