Liam Marshall
Liam Marshall

Reputation: 1613

How to make "common headers" for a library work when it's installed in /usr/include?

I'm working on packaging a library which has a bunch of header files, and a .a static library. The C headers for the library are all in the root folder of the library, but the headers use some external typedefs held in a common/ directory.

I tried copying all the .h files into a directory /usr/include/libcapriltags, and all the common/*.h files into /usr/include/libcapriltags/common. Then I symlinked the main .h file from /usr/include/libcapriltags/apriltag.h to /usr/include/apriltag.h

I also put the .a file in /usr/lib.

I could link against the library, but when I imported apriltag.h, gcc couldn't find anything in /usr/include/libcapriltags/common.

What am I doing wrong installing the library?

Upvotes: 0

Views: 166

Answers (1)

John Bollinger
John Bollinger

Reputation: 181319

Supposing that the "library" headers reference the shared internal headers via the form ...

#include "common/my_typedefs.h"

..., it is incorrect to both install those headers in /usr/include/libcapriltags/common and at the same time symlink /usr/include/libcapriltags/apriltag.h to /usr/include/apriltag.h. The symlink will also be an issue if apriltag.h refers to other headers directly in /usr/include/libcapriltags/ via double-quote syntax.

When the compiler locates /usr/include/apriltag.h, it is unlikely to know or care whether that's a symlink. Any relative lookup it performs for other headers #included by apriltag.h will be relative to the path at which it found apriltag.h (the symlink).

On the other hand, if any of these headers refer to each other via the angle-bracket include syntax, then the root issue is that the directory in which you've installed them is not in the (default) include search path. Your symlink does nothing to address that, except with respect to its specific target file.

Supposing that you do want to put all the headers in and under /usr/include/libcapriltags, which is perfectly reasonable, you should be prepared to add that directory to the include file search path when you compile code that uses them. The traditional compiler option for doing so is spelled -I/usr/include/libcapriltags. Depending on how they're written, you might be also able to reference them via qualified form (#include <libcapriltags/apriltags.h>). Either way, you neither need nor want to symlink any files from that directory into /usr/include.

Upvotes: 2

Related Questions