Igor Alberte
Igor Alberte

Reputation: 5

Problem compiling C-Function to Postgres; compiler didn't find postgres.h

I was asked to create a C-Function to integrate with Postgres. The Postgres documentation to this kind of function is available here: Postgres documentation.

The function I am trying to compile is from the manual and it is called add_one, just for test. But I had a problem while compiling it. The command I followed of the documentation was:

cc -fPIC -c foo.c
cc -shared -o foo.so foo.o

And the problem it returned was:

[igoralberte@localhost inside-postgres]$ cc -fPIC -c serializacao.c 
serializacao.c:1:10: fatal error: postgres.h: Arquivo ou diretório inexistente
 #include "postgres.h"
          ^~~~~~~~~~~~
compilation terminated.

In English, it means: Non-existent file or directory (postgres.h).

I have tried to copy some files I thought were important to /usr/lib directory. They were on /usr/include/pgsql or on /lib64. Those files were:

Some important informations about my system:

Thanks in advance!

Upvotes: 0

Views: 925

Answers (1)

HAL9000
HAL9000

Reputation: 2188

It is a bold step to write a postgres plugin before you have a solid grasp on linux/unix, shell programming and how to compile c programs.

Typically your c compiler has to be told where to find header files using the -I compiler switch. So if postgres.h is in /path/containing/headerfile, you must add -I/path/containing/headerfile to the compile command:

cc -I/path/containing/headerfile -fPIC -c foo.c

The postgres documentation you linked to tells you to use pg_config --includedir-server to find out where the the header files are stored.

I am not familiar with pg_config, but if it acts like similar tools and gives the output -I/path/containing/headerfile when calling it with the paramater --includedir-server, then you don't have to hardcode the path in your compile command. But just write:

cc `pg_config --includedir-server` -fPIC -c foo.c

See "Command Substitution" in your favorite shell documentation.

I also recommend learning how to use a build-tool like make. Things are soon going to be tedious if you have to retype compilation and link commands all the time.

Oh, and by the way, you probably want to write #include <postgres.h> and not #include "postgres.h" (Unless you are a postgres contributor and postgres.h is part of your project files)

Upvotes: 1

Related Questions