CMake find_path include directory prefix
Asked Answered
H

1

10

I am writing a minimal Find*.cmake for OpenNI. To find the header files I wrote

find_path(OPENNI_INCLUDE_PATH XnOS.h)

which is working as expected (OPENNI_INCLUDE_PATH has the value /usr/include/ni). However, in my files I have to include the headers with

#include <ni/XnOS.h>

How can I get rid of the ni prefix, so I can write

#include <XnOS.h>

The problem with the first include is that a XnCppWrapper.h gets included and this file includes again some Xn*.h headers, but without the ni prefix. This results in a compiler error.

Hautboy answered 21/3, 2014 at 12:11 Comment(10)
Are all of the files in the first form? I mean you could adjust the OPENNI_INCLUDE_PATH variable to be the parent of what is returned by find_path() and use that instead.Tony
I think, I don't get your suggestion. Do you mean that I should strap ni from /usr/include/ni? This doesn't solve the include problem.Hautboy
I mean if ni is in /usr/include then OPENNI_INCLUDE_PATH should be /usr/include instead of /usr/include/ni. This way you do not need to generate some script process that changes your c++ source files and headers looking for includes to remove the path. Although I do know if this was the case /usr/include will allready be in your includes so the OPENNI_INCLUDE_PATH would be redundant.Tony
Although you could always set(OPENNI_INCLUDE_PATH ${OPENNI_INCLUDE_PATH};${OPENNI_INCLUDE_PATH}/..)Tony
You are right, ni is in /usr/include. Unfortunatley it doesn't change a thing if I change OPENNI_INCLUDE_PATH to be /usr/include. In either case I have to write #include <ni/XnOs.h> in my files, which results in the above described compilation error.Hautboy
If /usr/include is in your includes "#include <ni/XnOs.h" should not result in a compile error. Although I do not have a lot of experience with gcc.Tony
It results in a compilation error, because in the file XnOs.h is again an include that doesn't have the ni/ prefix. Then the compiler complains that it can not find the file.Hautboy
Then try to include both paths like I said 3 posts ago.Tony
That was no solution. I get the same compilation error.Hautboy
If both /usr/include/ni and /usr/include are in your includes you should not get an error. Something else must be happening.Tony
N
14

Always have the path you use for find_path match the one in your #include statements.

If you want to #include <ni/XnOS.h> you should write

find_path(OPENNI_INCLUDE_PATH ni/XnOS.h)

If instead you want to #include <XnOS.h>, use

find_path(OPENNI_INCLUDE_PATH XnOS.h)

Just be sure to make up your mind beforehand which one you want to use and stick to it. Mixing several include paths for the same library is a sure way to unnecessarily overcomplicate the build environment.

Naturalism answered 21/3, 2014 at 13:21 Comment(4)
In my cmake file I have find_path(OPENNI_INCLUDE_PATH XnOS.h) and I want to include the header with #include <XnOS.h>, but it doesn't work this way.Hautboy
@Hautboy Then you probably have a bug in your CMake script somewhere. Use message to verify that the value returned by find_path matches your expectation. Also be sure to double-check that your targets actually use that value as an include directory.Naturalism
You were right, the mistake was in the main cmake file and not in the find module.Hautboy
I really appreciate your answer which covers both cases. Actually, I had the opposite intention of the OP (to force the use of parent path in the #includes).Supper

© 2022 - 2024 — McMap. All rights reserved.