user289882 user289882 - 6 months ago 23
C Question

Cannot get mixed C and Ada program to link with pthread library

I am writing a program in C. I am getting the error:

file_name.c:(.text+0x5d): undefined reference to `pthread_create'

This specific error comes up when gcc is linking my file_name.o with my main ADA file.

For the actual compilation of the C file, I have tried using both:

gcc -c -pthread file_name.c

as well as:

gcc -c file_name.c -lpthread

Neither of these seem to work. Any thoughts?

One thing I did not realize is that when combining C and ADA, the C libraries are included when using

The problem was solved by using:

gnatmake ada_file.adb -o -largs c_file.o -lpthread


If you are using -c switch, you are not linking your code. You are simply compiling it into object files. There's no point in specifying the libraries at this stage. Your -l parameter is simply ignored - it has no effect and no meaningful purpose when -c is present.

The libraries are supposed to be supplied at the moment of linking, i.e. at the moment when you combine your object files into the final executable (without -c, of course). You are not showing us that command line. That linking line is exactly where you have to add your -lpthread parameter.