skip to Main Content

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.

2

Answers


  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
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search