1

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

  • What exactly does PUBLIC . do? I know what PUBLIC does but what does the point mean?
  • Why does it not work as before?

thank you in advance

PS: The folder structure is as follows

enter image description here

enter image description here

CD86
  • 979
  • 10
  • 27
  • Does call to `target_link_libraries` remain in the changed code? – Tsyvarev Oct 03 '17 at 17:36
  • yes it does and it is not affecting the outcome – CD86 Oct 03 '17 at 17:43
  • @CarstenD I suggest you replace images of file tree with the [following representation](https://meta.stackexchange.com/questions/147467/is-there-a-good-way-to-represent-file-structure-in-a-question-answer) – Liastre Oct 03 '17 at 22:24

1 Answers1

1

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.

Liastre
  • 1,281
  • 11
  • 29
  • thank you a lot for your extensive answer : ) What is the difference between and "" Syntax? – CD86 Oct 04 '17 at 06:45
  • @CarstenD, I suggest you to use search upon stackoverflow, it might be that your question is already has an [answer](https://stackoverflow.com/a/21594/4121301). And by the way, none of your questions has no accepted answer (even in case it has), if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. That an appropriate way to show gratitude. – Liastre Oct 04 '17 at 09:00
  • thank you, I did check the answer now =) Another question which I have is, how do I know if a certain library, e.g. OpenGL supports alias' like Qt does, i.e. Qt::Widgets and what is the use of those alias' anyway if I can just type ${Boost_LIBRARIES} and it does the same? – CD86 Oct 04 '17 at 10:07
  • @CarstenD that not an alias, it is [namespace](http://en.cppreference.com/w/cpp/language/namespace). I'm not quite understand what you asking about and imho comments not the place to discuss off topic, it would be better to create another one question, or search for answers on the internet. – Liastre Oct 04 '17 at 18:27