0

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:

  • libpq.so
  • libpq.so.5
  • libpq.so.5.13
  • libpq (directory)
  • postgres_ext.h

Some important informations about my system:

  • I am using CentOS 8
  • System architecture: x86-64
  • GCC version: gcc (GCC) 8.4.1 20200928 (Red Hat 8.4.1-1)
  • Postgres version: 13.3

Thanks in advance!

2
  • 2
    Don't copy/move files around that are managed by your package manager. You will mess up your system. The only files that are safe to touch are mostly under /home/igoralberte and, if you know what you are doing, /etc, /usr/local. Commented Jun 15, 2021 at 23:06
  • I just made a copy... I hope it doesn't mess up my system haha But I will undo it. Thanks! Commented Jun 15, 2021 at 23:15

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.