2

I have several C source files and headers. Some headers are for public use and some for internal use by library. I would like to separate them logically into folders so that headers for internal use do not mix with headers for public use.

Are there any commonly used patterns for that simple task?

4

3 回答 3

1

here is a typical pattern, and it may depend on your compiler what is available to you... you have 2 types of include path during compilation:

  1. User Header search path, denoted by double quoted #include "header.h" header names, these are typically just for the project that you are working on; as in you wouldn't address library headers this way typically.
  2. System Header search path, denoted by angle braces #include <mylib/header.h>, this can also be used for your libraries...

lets imagine the following simple scenario...

Project A depends on Lib A.

in lib_a we have one c file and 2 headers...

lib_a.c, lib_a.h, and lib_a_internal.h

when you build lib_a.c you use the following includes:

#include "lib_a.h"
#include "lib_a_internal.h"

in your build script you will copy lib_a.h to include/lib_a/lib_a.h somewhere in your system headers path (maybe not in your real system headers, but a build specific one, depending on how atomic and sophisticated you want to be.)
You would also likely copy your lib_a.a (or .so or .dyld, or .dll) to lib/lib_a.a where lib is typically a peer of include above.

then when you build Project A you use the following:

#include <lib_a/lib_a.h>
于 2013-04-15T15:30:01.003 回答
0

Why not put them into separate directories and use the flags to fetch the appropriate ones during the build?

于 2013-04-15T15:11:27.683 回答
0

Leave the internal headers with the lib's sources and place the headers prototyping the lib's external API to /usr/include/<your lib's name without the prefix "lib">.


Alternativly put the external headers to /usr/lib/<your lib's name without the prefix "lib">/include and link /usr/include/<your lib's name without the prefix "lib"> to the latter.

This allows you to place additional files to you lib under /usr/lib/<your lib's name without the prefix "lib">/include. For example config settings in etc which then can be linked from /etc/<your lib's name without the prefix "lib">.

于 2013-04-15T15:24:09.663 回答