Обсуждение: How to include the header files effectively

Поиск
Список
Период
Сортировка

How to include the header files effectively

От
Andy Fan
Дата:
I find the dependency is complex among header files in PG.   At the same time,  I find the existing code still can use the header file very cleanly/alphabetically.   so I probably missed some knowledge here.

for example,  when I want the LOCKTAG in .c file,   which is defined in "storage/lock.h".  then I wrote the code like this:

#include "storage/lock.h"
...

LOCKTAG tag;


compile and get errors. 

In file included from 
.../src/include/storage/lock.h:21:
/../../../src/include/storage/lockdefs.h:50:2: error: unknown type name
      'TransactionId'
        TransactionId xid;                      /* xid of holder of AccessExclusiveLock */ 

so I HAVE TO 
1.  include the header file which contains the TransactionId 
2.  add it before the lock.h.  

normally I think we can add the dependency in lock.h directly to resolve this issue.

so how can I include header file effectively ?

Thanks

Re: How to include the header files effectively

От
Tom Lane
Дата:
Andy Fan <zhihui.fan1213@gmail.com> writes:
> for example,  when I want the LOCKTAG in .c file,   which is defined in
> "storage/lock.h".  then I wrote the code like this:

> #include "storage/lock.h"
> ...
> LOCKTAG tag;

> compile and get errors.

> In file included from
> .../src/include/storage/lock.h:21:
> /../../../src/include/storage/lockdefs.h:50:2: error: unknown type name
>       'TransactionId'
>         TransactionId xid;                      /* xid of holder of
> AccessExclusiveLock */

The reason that's failing is that you didn't include postgres.h first.

The general expectation --- and we do mechanically verify this,
periodically --- is that any Postgres header should have enough #include's
that you can include it without further work, so long as you included
postgres.h (or postgres_fe.h, or c.h, depending on context) beforehand.
One of those three headers must be the first inclusion in every Postgres
.c file.  There are portability reasons behind that rule, which you
don't really want to know about ;-) ... just do it like that.

            regards, tom lane



Re: How to include the header files effectively

От
Alvaro Herrera
Дата:
On 2019-Apr-12, Andy Fan wrote:

> for example,  when I want the LOCKTAG in .c file,   which is defined in
> "storage/lock.h".  then I wrote the code like this:
> 
> #include "storage/lock.h"
> ...
> 
> /../../../src/include/storage/lockdefs.h:50:2: error: unknown type name
>       'TransactionId'
>         TransactionId xid;                      /* xid of holder of AccessExclusiveLock */

What are you trying to do?  Your .c file must include "postgres.h"
before any other header file.  There should be no other dependencies.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: How to include the header files effectively

От
Michael Paquier
Дата:
On Thu, Apr 11, 2019 at 10:22:56PM -0400, Alvaro Herrera wrote:
> What are you trying to do?  Your .c file must include "postgres.h"
> before any other header file.  There should be no other dependencies.

The usual rule when it comes to develop extensions or a patch is to
include headers in the following order:
1) postgres.h for backend code and postgres_fe.h for frontend (use
ifdef FRONTEND if a file is used in both context, see src/common/*.c).
2) System-related headers, like <unistd.h> or such.
3) Other PostgreSQL internal headers, in any patch posted to the lists
these in alphabetical order is warmly welcome.
--
Michael

Вложения