CMake No such file or directory - c++

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"
...

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)

Related

CMake warning: CMake Targets may link only to libraries. CMake is dropping the item [duplicate]

This question already has answers here:
Understanding "requests linking to directory" cmake warning
(1 answer)
CMake link to external library
(6 answers)
Closed 10 months ago.
I am making an anpr algorithm that requires tesseract to decode the image to text. When running cmake .. inside my build dir, I get a warning saying:
┌──(user㉿MacBookArch)-[~/dev/anpr/build]
└─$ cmake ..
-- Configuring done
CMake Warning at CMakeLists.txt:15 (target_link_libraries):
Target "main" requests linking to directory
"/home/user/dev/anpr/build/libs/leptonica". Targets may link only to
libraries. CMake is dropping the item.
CMake Warning at CMakeLists.txt:16 (target_link_libraries):
Target "main" requests linking to directory
"/home/user/dev/anpr/build/libs/tesseract". Targets may link only to
libraries. CMake is dropping the item.
-- Generating done
-- Build files have been written to: /home/user/dev/anpr/build
This leads me into the thoughts that I have made something wrong. I have copied the repos for leptonica and tesseract into the libs directory for portability.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.2.2)
project( ANPR )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include )
include_directories(${PROJECT_SOURCE_DIR}/libs/tesseract/include)
include_directories(${PROJECT_SOURCE_DIR}/libs/leptonica/include)
link_directories(${PROJECT_SOURCE_DIR}/libs/tesseract)
link_directories(${PROJECT_SOURCE_DIR}/libs/leptonica)
add_executable( main src/main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
target_link_libraries( main ${CMAKE_CURRENT_BINARY_DIR}/libs/leptonica)
target_link_libraries( main ${CMAKE_CURRENT_BINARY_DIR}/libs/tesseract)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/samples/001.jpg
${CMAKE_CURRENT_BINARY_DIR}/samples/001.jpg
COPYONLY
)
and this is my project structure:
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── LeptonicaTargets.cmake
│ ├── libs
│ ├── main
│ ├── Makefile
│ └── samples
├── CMakeLists.txt
...
├── include
│ └── main.hpp
├── libs
│ ├── leptonica
│ └── tesseract
...
├── samples
│ └── 001.jpg
└── src
└── main.cpp
The contents of the build dir is auto generated with cmake.
make command inside the build dir goes fine without errors even after make clean:
┌──(user㉿MacBookArch)-[~/dev/anpr/build]
└─$ make
[ 50%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
How can I resolve this warning? I am open to all improvements, thank you!

Cant find a OpenCV header file (CMake, OpenCV4, C++)

When I am creating custom libraries using OpenCV4 in the libs directory I am getting the following error:
/Documents/opencv-palisade/libs/image.cpp:2:10: fatal error: opencv2/opencv.hpp: No such file or directory
2 | #include <opencv2/opencv.hpp>
| ^~~~~~~~~~~~~~~~~~~~
Directory Structure:
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── image.jpg
│ ├── include
│ ├── libs
│ ├── Makefile
│ ├── opencv-palisade
│ ├── test.jpg
│ └── transformed_image.jpg
├── CMakeLists.txt
├── libs
│ ├── CMakeLists.txt
│ ├── image.cpp
│ └── image.hpp
└── main.cpp
5 directories, 12 files
libs/CMakeLists.txt:
project(libs)
include_directories(${OpenCV_INCLUDE_DIRS})
add_library(
image_compute
SHARED
image.hpp
image.cpp
)
root CMakeLists.txt:
cmake_minimum_required(VERSION 3.16.3)
project(opencv-palisade VERSION 1.0)
#Include header files
add_subdirectory(libs)
include_directories(libs)
# Import OpenCV library
find_package(OpenCV 4 REQUIRED)
if(OpenCV_FOUND)
message(STATUS "Found OpenCV version ${OpenCV_VERSION}")
message(STATUS "OpenCV directories: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS})
else()
message(FATAL_ERROR "OpenCV not found, please read the README.md")
endif(OpenCV_FOUND)
#build main.cpp
add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(${PROJECT_NAME} PUBLIC libs)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} image_compute)
How can I fix this issue to be able to create libraries using opencv api?

Creating and using a static library with CMake

I am trying to first create a static library and then link it to an executable using CMake. My project file structure looks like this:
├── CMakeLists.txt
├── build
├── lib
│   ├── CMakeLists.txt
│   ├── build
│   ├── include
│   │   └── Point.hpp
│   └── src
│   └── Point.cpp
└── mainApp.cpp
I first build the library like so.
cmake_minimum_required(VERSION 2.8.9)
project(CAST3)
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_library(CAST3 STATIC ${SOURCES})
However, when i try to link the library to my executable I get an error.
This is my executable
#include"Point.hpp"
int main(int argc, char *argv[]){
Point p = Point(1,2,3);
return 0;
}
This is my CMake file to link the library to the executable.
cmake_minimum_required(VERSION 2.8.9)
project (CAST3)
set ( PROJECT_LINK_LIBS libCAST3.a )
link_directories( ${CMAKE_CURRENT_SOURCE_DIR}/lib/build)
add_executable(libtest mainApp.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS} )
When I run that I get the this error
/mainApp.cpp:1:9: fatal error: 'Point.hpp' file not found
#include"Point.hpp"
^~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/libtest.dir/mainApp.cpp.o] Error 1
make[1]: *** [CMakeFiles/libtest.dir/all] Error 2
make: *** [all] Error 2
What am I missing?
What am I missing?
You are missing target_include_directories(CAST3 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) inside lib/CMakeLists.txt.
You are missing add_subdirectory(lib) from root CMakeLists.txt.
The set ( PROJECT_LINK_LIBS libCAST3.a ) and target_link_libraries(libtest ${PROJECT_LINK_LIBS} ) and link_directories( ${CMAKE_CURRENT_SOURCE_DIR}/lib/build) could be removed. Then you are missing just target_link_libraries(libtest PUBLIC CAST3). Cmake will automatically find the proper .a file and propagate include paths with target_link_libraries.
So your lib/CMakeLists.txt could look like this:
cmake_minimum_required(VERSION 3.0) # I would advise to update
project(CAST3)
include_directories(include)
file(GLOB sources src/*.cpp) # upper case variable names reserved for exported variables by convention
add_library(CAST3 ${sources}) # STATIC by default
target_include_directories(CAST3 PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
And root CMakeLists.txt could look like this:
cmake_minimum_required(VERSION 3.0) # I would advise to update
project(CAST3)
add_subdirectory(lib)
add_executable(libtest mainApp.cpp)
target_link_libraries(libtest CAST3)

Build zLib with CMake

I'm trying to build Zlib and quazip automatic with the rest of my project. I want to do it in a similar way as I add googletest to my project.
zLib and Quazip shall be linked static to my App.
I want to to this because of CI/ CD reasons and if somebody else wants to build the project he do not have to worry about those dependencies (especially under windows)...
Download it at configure time with CMake ExternalProject_Add
add it on target level
My structure is like the following:
project/
├── CMakelists.txt (1)
├── sources/
│ ├── APP/
│ │ ├── CMakeLists.txt (2)
│ │ ├── thirdparty/
│ │ │ ├── CMakeLists.txt (3)
│ │ │ ├── zlib
│ │ │ │ ├── CMakeLists.txt.in
│ │ │ ├── quazip
│ │ │ │ ├── CMakeLists.txt.in
│ │ │ │ ├── CMakeListsBuild.txt.in
Thats complicated enough but let me show you what I do where...
(1) CMakeLists.txt
Noting special just adding all the packages together like APP
(2) CMakeLists.txt
cmake_minimum_required(VERSION 3.15.2)
project(Project VERSION 0.0.1)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
#Add the needed Qt Components
find_package(Qt5 COMPONENTS
Core
Network
REQUIRED)
add_subdirectory(thirdparty)
SET(INCLUDES
...
)
SET(SOURCES
...
)
target_compile_options(${PROJECT_NAME} PUBLIC
...
)
target_compile_definitions(${PROJECT_NAME} PUBLIC
QUAZIP_STATIC )
target_include_directories(${PROJECT_NAME} PRIVATE
OtherIncludes
"${QUAZIP_INCLUDE_DIR}"
)
target_link_libraries(${PROJECT_NAME}
Qt5::Network
Qt5::Core
SomeOtherDep
quazip_static <---- adding static targets here
zlibstatic <---- adding static targets here
)
(3) CMakeLists.txt
# Download and unpack at configure time
configure_file(zlib/CMakeLists.txt.in zlib-download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/zlib-download"
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/zlib-download"
)
add_subdirectory("${CMAKE_BINARY_DIR}/zlib-src"
"${CMAKE_BINARY_DIR}/zlib-build"
)
# Download and unpack at configure time
configure_file(quazip/CMakeLists.txt.in quazip-download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/quazip-download"
)
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/quazip-download"
)
configure_file(quazip/CMakeListsBuild.txt.in ${CMAKE_BINARY_DIR}/quazip-src/CMakeLists.txt COPYONLY)
add_subdirectory("${CMAKE_BINARY_DIR}/quazip-src"
"${CMAKE_BINARY_DIR}/quazip-build"
)
set(QUAZIP_INCLUDE_DIR
"${CMAKE_BINARY_DIR}/quazip-src/quazip" PARENT_SCOPE)
The two CMakeLists.txt.in have nearly the same Content...
cmake_minimum_required(VERSION 2.8.2)
include(ExternalProject)
project(quazip-download NONE)
ExternalProject_Add(quazip
GIT_REPOSITORY git://github.com/stachenov/quazip.git
GIT_TAG v0.8.1
SOURCE_DIR "${CMAKE_BINARY_DIR}/quazip-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/quazip-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
cmake_minimum_required(VERSION 2.8.2)
include(ExternalProject)
project(zlib-download NONE)
ExternalProject_Add(zlib
GIT_REPOSITORY git://github.com/madler/zlib.git
GIT_TAG v1.2.11
SOURCE_DIR "${CMAKE_BINARY_DIR}/zlib-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/zlib-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
Then there is the last CMakeListsBuild.txt.in in the quazip folder. I'm just editing the original one and feed the zlib dependencies directly in. It looks like this:
cmake_minimum_required(VERSION 2.6)
project(QuaZip)
...
EVERYTHING ORIGINAL
...
# Use system zlib on unix and Qt ZLIB on Windows
# will be set from outside ZLIB_LIBRARIES ZLIB_INCLUDE_DIRS
--------------------ADDED BY ME REMOVED find_packages...
set(ZLIB_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/zlib-src
${CMAKE_BINARY_DIR}/zlib-build)
if(UNIX)
set(ZLIB_LIBRARIES
"${CMAKE_BINARY_DIR}/zlib-build/libz.a")
elseif(MINGW)
set(ZLIB_LIBRARIES
"${CMAKE_BINARY_DIR}/zlib-build/libzlibstatic.a")
endif()
...
EVERYTHING ORIGINAL
...
Question
Downloading and building the static lib of zlib works but I get the following error while compiling under linux.
/home/linuxmint/someDirectory/build/zlib-src/test/example.c:8:10: fatal error: zlib.h: No such file or directory
#include "zlib.h"
^~~~~~~~
compilation terminated.
zlib-build/CMakeFiles/example.dir/build.make:75: recipe for target 'zlib-build/CMakeFiles/example.dir/test/example.o' failed
make[2]: *** [zlib-build/CMakeFiles/example.dir/test/example.o] Error 1
CMakeFiles/Makefile2:1355: recipe for target 'zlib-build/CMakeFiles/example.dir/all' failed
make[1]: *** [zlib-build/CMakeFiles/example.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
This is the strange part I dont understand. If I have a look in the CMakeLists of zlib. (LINK)
They add everything of of the source and binary folder to the include with this:
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
So I don't understand why the example of zlib (and everything else which need zlib.h) can't be build...
Perhabs somebody has a hint...
Thanks...
Edit
If I install the headers with apt building works... But why ;) Since nowhere find_package is called...
Thanks to #squarekittles who pointed me in the right direction.
In the CMakeLists.txt of zlib the include dir is specified as ${CMAKE_SOURCE_DIR}. This is of course the wrong directory...
I added a additional CMakeLists.txt.in for zlib which will replace the original one. It's all the same only two lines are different:
...
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
^~~~~~~~~~~~~ Added in
#============================================================================
# zlib
#============================================================================
set(ZLIB_PUBLIC_HDRS
${CMAKE_CURRENT_BINARY_DIR}/zconf.h
${CMAKE_CURRENT_SOURCE_DIR}/zlib.h
) ^~~~~~~~~~~~~ Added in
...
Now the examples compile...
Also I needed to add the headers of zlib to my project did it by editing CMakeLists.txt (3)
...
configure_file(zlib/CMakeListsBuild.txt.in ${CMAKE_BINARY_DIR}/zlib-src/CMakeLists.txt COPYONLY) <--- Replacing CMakeLists.txt for zlib
...
set(QUAZIP_INCLUDE_DIR
"${CMAKE_BINARY_DIR}/quazip-src/quazip"
${ZLIB_INCLUDE_DIR} PARENT_SCOPE)
^~~~~~~~~~~~ Added in
...

cmake mutliple targets - one header only target and another an executable

Based on this stackoverflow answer to a similar question (Cmake include header only target from header only target), I am creating a header only library and trying to use it in an executable.
My folder structure is below:
├── CMakeLists.txt // root folder CMake file
├── srcs // this is the hdr only library
│   ├── src1.hpp
│   ├── CMakeLists.txt
│   ├── src2.hpp
│   └── src3.hpp
│   └── ...
└── test // this is the executable project
├── CMakeLists.txt
└── main.cpp
Root level CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project (MyProj CXX)
add_subdirectory(srcs)
add_subdirectory(test)
src level CMakeLists.txt (for header only library)
add_library(MyLib INTERFACE)
target_sources(MyLib INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/src1.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src2.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src3.hpp"
)
target_include_directories(MyLib
INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
add_subdirectory(plugins)
CMake file for executable test project
add_executable(MyTest main.cpp)
target_sources(MyTest
PRIVATE main.cpp
)
target_include_directories(MyTest PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_link_libraries(MyTest PUBLIC MyLib)
Though this configures cmake without warnings/error, running the make fails, indicating that the executable project is unable to find the header file(s) from library.
/.../nettu2/test/main.cpp:6:10: fatal error: src1.hpp: No such
file or directory #include "src1.hpp"
^~~~~~~~~~~~~~~~ compilation terminated. test/CMakeFiles/MyTest.dir/build.make:62: recipe for target
'test/CMakeFiles/MyTest.dir/main.cpp.o' failed make[2]: *
[test/CMakeFiles/MyTest.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:126: recipe for target
'test/CMakeFiles/MyTest.dir/all' failed make[1]: *
[test/CMakeFiles/MyTest.dir/all] Error 2 Makefile:129: recipe for
target 'all' failed make: *** [all] Error 2
I am sure that I am missing some crucial but trivial thing here, but yet unable to figure out, what is going wrong here. How can I get this build working?
You have a minor mistake in this line:
target_include_directories(MyLib
INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
The include directory you specify for MyLib expands to the build directory corresponding to srcs, i.e. it results in an invocation like
clang++ /path/to/build/test/test.cpp -I /path/to/build/srcs ...
but you want to pass the actual source directory to the preprocessor. To fix this, try
target_include_directories(MyLib
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})