0

I have a simple subdir project with following file tree in Qt:

nrc.pro
gui
   -gui.pro
   -mainwindow.h
   -mainwindow.cpp
   -main.cpp
nrclib
   -nrclib.pro
   -nrclib.h
   -nrclib.cpp
   -otherfile.h
   -otherfile.cpp

nrclib uses OpenCV, so I have added appropriate "includepath and libs" for both Linux and Windows to nrclib.pro

and I have used nrclib as a library for gui.pro, I have not used any opencv function in gui project, so there is no need to add opencv's includepath and libs to gui.pro, and I have #include <opencv/cv.h> in otherfile.h and #included "otherfile.h" in nrclib.h and #include "nrclib.h" in mainwindow.h.

so far so good. This project worked perfect under both windows and Linux. But today I made a change, I added a function to "nrclib.h" and it needed opencv, so I did #include <opencv/cv.h> to nrclib.h. It works perfectly under Linux. But when I wanted to compile it in Windows it gives me: "<opencv/cv.h>: no such file or directory". If I delete the inclusion, everything works perfectly. and If I don't use "subdir template" in Qt, there won't be any problem. But if I use subdir template in windows I have to add LIBS and INCLUDEPATH of OpenCV to gui.pro, although no opencv related function is used in gui project, I have only included nrclib.h in mainwindow.h.

My question is: why should I add LIBS and INCLUDEPATH of OpenCV in gui.pro? Why does it give me "no such file or directory" for an inclusion that is already being used in another header?

sorry for long and useless question, it is bugging my mind.

edit 1: I am aware of this question but there is not a good answer there, there should be no need to add "OpenCV" to gui.pro, it doesn't need it.

Community
  • 1
  • 1
FSFisGOOD
  • 193
  • 1
  • 11
  • I edited the formatting, I hope I got your meaning right, please check the `<>` vs `""` use at least. – hyde Feb 14 '15 at 11:05
  • I know the difference between `<>` vs `""` , my problem is with "Adding a library to a `.pro` file where it is not needed." – FSFisGOOD Feb 14 '15 at 11:09
  • If source code in the project includes a .h file (even if indirectly though other .h files), then compiler needs to find the .h file... If you include it, it *is* needed. – hyde Feb 14 '15 at 18:41

1 Answers1

1

In C++ (inherited from C), #include effectively works by "copy-pasting" contents of included file, recursively. So if you include file which includes another file, then that another file needs to be found too.

So if nrclib.h includes opencv.h, compiler must find opencv.h when ever you include nrclib.h.

It works on Linux probably because opencv.h is standard include paths. And it works if you don't use subdirs template, because you have just one INCLUDEPATH used for both the library and the application. But with subdirs project on Windows, gui.pro doesn't get it from anywhere.


As a practical suggestion, you could create nrclib/nrclib.pri, and then there add everything nrclib needs, LIBS += ... and INCLUDEPATH += ... and so on. And then in gui.pro, do

include(../nrclib/nrclib.pri)

That way gui.pro doesn't need to directly care about opencv, it just needs to include this .pri file from the another subdir project.


Another option would be to change nrclib.h so that it does not need opencv.h. This probably meanss, you need to wrap whatever you need from OpenCV so that you don't need to use in nrclib.h. How much extra code this is, depends on what exactly you need from there.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Now I understand it. I used `opencv` headers in `nrclib.cpp` and it had no problem, but when I used it in `nrclib.h` it caused problems and that's because I include `nrclib.h` in `mainwindow.h` and the content of `nrclib.h` is copied in `mainwindow.h`. Now it is clear. Thanks for your answer. I should rewrite my code and remove any header in `nrclib.h` that even indirectly is related to `opencv`. – FSFisGOOD Feb 14 '15 at 19:41