1
votes

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?

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

The problem was solved by using:

 gnatmake ada_file.adb -o -largs c_file.o -lpthread
1
Show us how you link your code. - arrowd
gcc -c does compiling but no linking. What command are you using to link, where the actual error occurs? - aschepler
The linking is being done by gnatmake. The actual linking part is done using the gnatmake flag -largs that is followed by file_name.o - Nautilus
Update the question to show the actual linking line. You'll need to add something to it to make the linker scan the pthread library. And it appears that the problem is in the gnatmake facility — it isn't linking what you need to link. Suggesting that GCC is at fault seems like it might be misdirecting the blame. - Jonathan Leffler
That is not updating the question: it is adding another comment. Please edit the question to include the relevant information. Is gnatmake the Gnat linker process? If so, you might need to add -lpthread after the c_file_name.o argument. Or you might need to RTFM on gnatmake. Does Gnat allow you to mix Ada threads with POSIX threads? - Jonathan Leffler

1 Answers

4
votes

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.