简体   繁体   English

如何组织库中的头文件

[英]How to organize header files in a library

Say I am writing a small libary in C , with most of the source code in two folders src/A and src/B , and where the header file src/A/ah needs to include src/B/bh .假设我正在用C编写一个小库,其中大部分源代码位于src/Asrc/B两个文件夹中,并且头文件src/A/ah需要包含src/B/bh When writing code for a non-library project, I usually write在为非库项目编写代码时,我通常会写

#include "B/b.h"

in ah and use the -Isrc flag to tell the compiler where to look for header files.ah并使用-Isrc标志告诉编译器在哪里查找头文件。

Now suppose that my library is installed locally at ~/mylib and that I want to use functions from ah from a different project.现在假设我的库本地安装在~/mylib并且我想使用来自不同项目的ah函数。 Simply including that file using只需使用包含该文件

#include "~/mylib/src/A/a.h"

would not work, because ~/mylib/src/B/bh might not be part in the search path.不起作用,因为~/mylib/src/B/bh可能不在搜索路径中。 My question is about the canonical way to solve this issue.我的问题是关于解决这个问题的规范方法。 It's probably quite basic, but I haven't done any advanced programming in C and have been unsuccessful in my attemps to find a solution online.这可能是非常基本的,但我没有用C进行任何高级编程,并且在尝试在线找到解决方案时没有成功。

Possible solutions I thought of are the following:我想到的可能解决方案如下:

  • Add ~/mylib to the search path, but that might lead to problems if the library and client projects have header files with the same name (say src/helpers.h ).~/mylib添加到搜索路径,但如果库和客户端项目具有相同名称的头文件(比如src/helpers.h ),这可能会导致问题。 Is it possible to include one header file without cluttering the search space with files I won't need?是否可以包含一个头文件,而不会用我不需要的文件弄乱搜索空间?

  • Use relative paths in the library header files, but that doesn't feel very robust.在库头文件中使用相对路径,但感觉不是很健壮。

Thank you.谢谢。

The normal approach is to have a separate directory specifically for the headers which form the public interface of your library.通常的方法是有一个单独的目录,专门用于构成库公共接口的头文件。 Usually this directory would be called 'include'.通常这个目录被称为'include'。

You would then place the public headers for your library under a library-specific directory in there, ie "mylib/include/mylib/bh".然后,您可以将库的公共头文件放在特定于库的目录下,即“mylib/include/mylib/bh”。 This extra 'mylib' directory prevents clashes if you're using some other library that also has a "bh".如果您使用其他一些也有“bh”的库,这个额外的“mylib”目录可以防止冲突。 You can also, if you wish, keep other private headers, which do not form the public interface of your library, under the 'src' directory instead, to stop them being exposed to users of the library.如果您愿意,您还可以将其他不构成库的公共接口的私有头文件保存在“src”目录下,以防止它们暴露给库的用户。

This means a user of the library can then use "-I mylib/include" to include this directory, and include the individual files with, for example, "#include "mylib/bh".这意味着库的用户然后可以使用“-I mylib/include”来包含此目录,并使用例如“#include“mylib/bh”来包含单个文件。

Why aren't you using the standard implementation?你为什么不使用标准实现? Break out into header and source files into their own directories.将头文件和源文件分解到它们自己的目录中。 Add #define headers to avoid multiple includes or namespace corruption.添加#define标头以避免多个包含或命名空间损坏。

Here is your directory structure:这是您的目录结构:

~/mylib/headers/a.h
                b.h
~/mylib/src/a.c
            b.c

Now ah will have at the very top of the file...现在ah将在文件的最顶部......

#ifndef __A_H__
#define __A_H__
    //  code

#include "~/mylib/headers/b.h"

    // end of file
#endif

Now bh will have at the very top of the file...现在bh将位于文件的最顶部...

#ifndef __B_H__
#define __B_H__
    //  code

    // end of file
#endif

Then just compile.然后编译即可。 gcc -I~/mylib/headers

If you have 2 helpers.h just change the #define __HELPERS_H__ in one of the files to something else like #define __HELPERS2_H__如果您有 2 个helpers.h只需将其中一个文件中的#define __HELPERS_H__更改为其他内容,例如#define __HELPERS2_H__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM