CMake is working wierdly in terms of searching and adding libraries

I am building openboard from source, with an SDK for Qt libraries and building poppler from the latest release. Now, while compiling openboard it looks for poppler and finds the correct poppler==25.12 using pkg-configwhich is located in /root/stage/usr/include/poppler. The SDK also contains an installaton of poppler==24.02. From PR #1387 at openboard,

Poppler 25.12 allows string_view and char* for GooString. QByteArray can be converted to both, so choose one explicitly to avoid the “call of overloaded ‘GooString(QByteArray)’ is ambiguous” error:
```
/build/openboard-git/src/OpenBoard/src/pdf/XPDFRenderer.cpp:65:55: required from here
65 | mDocument = new PDFDoc(std::make_unique(filename.toLocal8Bit()));
| ~^~
/usr/include/c++/15.2.1/bits/unique_ptr.h:1084:30: error: call of overloaded ‘GooString(QByteArray)’ is ambiguous
1084 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)…)); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/15.2.1/bits/unique_ptr.h:1084:30: note: there are 9 candidates
In file included from /usr/include/poppler/Object.h:46,
from /build/openboard-git/src/OpenBoard/src/pdf/XPDFRenderer.h:41:
/usr/include/poppler/goo/GooString.h:91:14: note: candidate 1: ‘GooString::GooString(std::string_view)’
91 | explicit GooString(std::string_view str) : std::string(str) { }
| ^~~~~~~~~
/usr/include/poppler/goo/GooString.h:73:14: note: candidate 2: ‘GooString::GooString(const char*)’
73 | explicit GooString(const char *sA) : std::string(sA ? sA : “”) { }
| ^~~~~~~~~
```

Now, when compiling here in my case, openboard is picking the goo libraries from /root/stage/usr/include/poppler/goo for the new headers of GooString, but when needing to look for poppler-version.hit is fallbacking and reading version 24.02 which is served by the SDK. Why is this happening? I did make sure that the CMAKE_PREIFX_PATH=/root/stage/usr:/snap/kde-qt6-core24-sdk/current/usr:/snap/kf6-core24-sdk/current/usr:/usr:/root/stage which makes sure that the /root/stage/usr prefixed is looked into first. Then why is this anomoly showing up? Can someone kindly help me figure this problem out and understand CMake properly?

EDIT: I treid this with CMake 4.2.1 also, and it is still the same

To shed some light on the possible fix, the pkg-config from poppler has cflags which adds the custom installation prefix only upto /root/stage/usr/include/poppler but doesn’t add /root/stage/usr/include. From here, if I remove the poppler from the include definition, i.e changing

#include <poppler/cpp/poppler-version.h>

to

#include <cpp/poppler-version.h>

fixes the search path.

For now, I am adding -I/root/stage/usr/include in DCMAKE_C_FLAGS and DCMAKE_CXX_FLAGS.