1

I am trying to compile a C program that uses libpq library in a Docker container. Here's my Dockerfile:

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y build-essential libpq-dev

WORKDIR /app

# Copy the source files into the container's working directory
COPY tmp.c .

# Compile the source file directly in the Dockerfile
RUN gcc -Wall -Wextra -pedantic -g -o my_program tmp.c -lpq

CMD ["./my_program"]

When I try to build the Docker image using docker build . -t my_image, I get the following error:

tmp.c:4:10: fatal error: libpq-fe.h: No such file or directory
#include "libpq-fe.h"
         ^~~~~~~~~~~~
compilation terminated.

I have tried installing libpq-dev package, but it doesn't seem to solve the issue. What am I missing here? How can I compile my program with libpq in the Docker container?

As a next step, I also want to test for memory leaks using Valgrind. Can you please advise me on how to do this after compiling the code? I am using Mac for the purpose of analyzing the leakage, I have used leak but it wasn't that much useful.

jian
  • 4,119
  • 1
  • 17
  • 32
AmrShams07
  • 96
  • 1
  • 7

5 Answers5

1

I have some suggestions may help:

  • Add explicit including to the PostgreSQL headers through -I/usr/include/postgresql tells the compiler to look in that directory for header files.
  • Skip the compiling line on the dockerfile and enter an interactive bash session and try to do that manually to figure out what is going on.
  • Try to remove -lpq and make sure it works without that flag, to be aware of the source of the problem (requires code modifications)

I could able to have that running on my machine through adding -I/usr/include/postgresql in the gcc command

RUN gcc -Wall -Wextra -pedantic -g -o my_program tmp.c -lpq  -I/usr/include/postgresql
  • 1
    `-lpq` is irrelevant to this error; it's a _link-time_ flag, the problem is happening during compilation, not linking. The appropriate `-I` is what's missing and essential. (It may or may not be `/usr/include/postgresql`; depends on the particulars of this distro's libpq-dev package). – Charles Duffy Jul 22 '23 at 22:49
  • 1
    I think the only way is going manually into interactive mode and figure out the directory of the lib to be included in the -l – AmrShams07 Jul 23 '23 at 11:33
  • 1
    That or looking up the list of files in libpq-dev in Ubuntu latest via https://packages.ubuntu.com, or instructing the OP to use pkg-config to look up the arguments to use. So there are at least two options that don't involve interactive mode. – Charles Duffy Jul 23 '23 at 12:50
  • yeah except that `-I/usr/include/postgresql` just worked for me with `FROM ubuntu:20.04` – JohnAllen Jul 24 '23 at 09:58
0

Try finding the location of the library with

find / -name "libpq-fe.h"

Then put the full path in the include

#include "postgres/src/interfaces/libpq/libpq-fe.h"

In my case it's located in this directory ~~^

Marco Souza
  • 296
  • 7
0

You need to add include path for PostgreSQL in the Docker file:

RUN gcc -Wall -Wextra -pedantic -g -o my_program tmp.c -lpq -I/usr/include/postgresql/

And for memory leak testing using valgrind. Use this

RUN apt-get install -y build-essential libpq-dev valgrind
CMD ["valgrind", "--leak-check=full", "./my_program"]
adil shahid
  • 125
  • 4
0

It seems some files are missing in your system/docker file.

  1. Update the docker file using this command: RUN apt-get update && \ apt-get install -y libpq-dev

  2. Add the path to docker file using this command:: RUN gcc -Wall -Wextra -pedantic -g -o my_program tmp.c -lpq -I/usr/include/postgresql/

-1

I suggest you try to reinstall the libpq-dev package.

sudo apt-get install --reinstall libpq-dev

If it doesn't work, another solution could be to use an alternative to Docker, like wsl.

Marcos Silva
  • 115
  • 5
  • Using double quotes in the include statement means the header is looked for in the same directory as the C file. Reinstalling the headers or switching to Docker won't help that at all. – Charles Duffy Jul 23 '23 at 03:29