0

I'm trying to create my own function (using C) in postgres for which I'm referring this doc: http://www.linuxgazette.cz/139/peterson.html

For compiling - I'm referring: Section 33.9.6 . But, when I run below command:

   cc -fpic -c example.c

I get this error:

   example.c:1:22: fatal error: postgres.h: No such file or directory
 #include "postgres.h"
                  ^
compilation terminated.

I have also installed postgresql-server-dev-all package and tried with the solution provided in this link: How to fix 'postgres.h' file not found problem?

Nothing working for me. Please let me know how I can have postgres.h file. I'm using postgres-12 on centos -7.

Channa
  • 742
  • 17
  • 28
  • The Section 33.9.6 you refer to is for version 8.2 which is well past EOL. The current section is [37.10.5](https://www.postgresql.org/docs/current/xfunc-c.html#DFUNC). It has: `cc -fPIC -c foo.c`. I'm not a C programmer, so I'm not sure what the change to `PIC` means. – Adrian Klaver Sep 06 '20 at 17:47
  • Thanks for the advice. But, even after changing the casing -it's not working for me. – Channa Sep 13 '20 at 19:59

2 Answers2

1

First: If you didn't install PostgreSQL from source, make sure that you installed the headers, which are in the -devel package.

If the problem persists even with the headers installed, the reason is that you forgot to tell the compiler where to look for header files:

cc -fpic -I /location/of/postgres/include -c example.c

To find that location, you can use pg_config:

pg_config --includedir

There is a similar --libdir option to show the library path for when you link the executable.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Thank you so much for providing me this useful info. For me this path is: /usr/include. I ran the compilation command by the first command you provided with header file path and again I got the same error. However, when I went to this path : /usr/include. and search for the header file for postgres.h - I didn't get any. I see many other .h header files there. Can there be any problem with the package I'm working with? – Channa Sep 13 '20 at 20:07
  • If you are using the PGDG packages, that path would be `/usr/pgsql-12/include`. You never said what packages you are using. It is also possible that you have several installations of PostgreSQL. Without knowing details, it is hard to give more concrete advice. – Laurenz Albe Sep 14 '20 at 03:37
  • Sorry, but could you please help me to identify what package I'm using. I know how to check version and the version is 12.4 – Channa Oct 03 '20 at 19:19
  • 1
    That would be `rpm -qa | grep postgres`. But see my updated answer; perhaps you simply forgot to install the headers. – Laurenz Albe Oct 05 '20 at 05:01
1

for ubuntu: apt-get -y install postgresql-server-dev-13 after apt-get -y install postgresql-13

STA
  • 30,729
  • 8
  • 45
  • 59