CD86
CD86

Reputation: 1079

Cmake issue with building a small library

I am currently trying to deepen my understanding regarding CMake. I try to use http://libqglviewer.com/introduction.html]LibQGLViewer as a Third-Party library in a C++ Project of mine.

The CMakeLists.txt in the associated subdirectory looks like the following where the part, I have questions is the add_libary section and some header and source files were omitted for clarity

cmake_minimum_required (VERSION 3.5.1 FATAL_ERROR)

set(target_name QGLViewerQt5)
project(${target_name})

message(STATUS "BUILDING QGLViewer-2.7.0 FROM SOURCE!")

set(BASE_DIR QGLViewer)
set(VRENDER_DIR VRender) 
set(CMAKE_AUTOMOC ON)

set(QGLheaders
    ${BASE_DIR}/camera.h
    ${BASE_DIR}/config.h
    ${BASE_DIR}/${VRENDER_DIR}/AxisAlignedBox.h
    ${BASE_DIR}/${VRENDER_DIR}/Exporter.h
)


set(QGLsources
    ${BASE_DIR}/camera.cpp
    ${BASE_DIR}/${VRENDER_DIR}/Exporter.cpp
)

add_library(${target_name} ${QGLsources} ${QGLheaders}) 
target_include_directories(${target_name} 
 PUBLIC .
)

target_link_libraries(${target_name}
  ${OPENGL_LIBRARIES}
  Qt5::Core
  Qt5::Widgets
  Qt5::Xml
  Qt5::OpenGL
)

set(CMAKE_AUTOMOC OFF)

install(TARGETS ${target_name} DESTINATION lib)

My Application runs and everything is fine.

However, I read that one should only include the source files with add_library and then use target_include_directories to consider the associated header files. So I changes the above part to

add_library(${target_name} SHARED ${QGLsources})
target_include_directories(${target_name} 
  PUBLIC 
    ${PROJECT_SOURCE_DIR}/QGLViewer 
    ${PROJECT_SOURCE_DIR}/QGLViewer/VRender
  )

but now, I get an error trying to make my project

fatal error: QGLViewer/qglviewer.h: No such file or directory compilation terminated.

can you please tell me

thank you in advance

PS: The folder structure is as follows

enter image description here

enter image description here

Upvotes: 1

Views: 220

Answers (1)

Liastre
Liastre

Reputation: 1323

Firts one, target_include_directories() has following syntax according official docs target_include_directories(<target> [SYSTEM] [BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...]) as you see there few keywords might be used:

  • PRIVATE: adds directories to INCLUDE_DIRECTORIES property of <target>
  • INTERFACE: adds directories to INTERFACE_INCLUDE_DIRECTORIES property of <target>
  • PUBLIC: adds directories to INCLUDE_DIRECTORIES and INTERFACE_INCLUDE_DIRECTORIES properties of <target>

What about those properties:

  • INCLUDE_DIRECTORIES - contains list of directories to search header files you used in project
  • INTERFACE_INCLUDE_DIRECTORIES - contains list of directories to search header files too, but it has transitivity, that means you able to inherit your include directories to project linked through target_link_libraries()

Second one, your code doesn't work because you are using another include folders paths, your source file must be contain something like #include <QGLViewer/qglviewer.h> but since you do not include root folder anymore (. in your previous code), but linking QGLViewer directly - you must type #include <qglviewer.h>. Fix it for each #include you are using or add ${PROJECT_SOURCE_DIR} to target_include_directories instead.

Upvotes: 1

Related Questions