Compiling 32 bit gfortran program with Makefile on Centos 7 gives errors such as: /bin/ld: cannot find -lpthread

I downloaded the latest release of gfortran from: gcc-11.1.0.tar.xz to a VMware Centos 7 32 bit Virtual Machine.

I was able to build gfortran and run a Fortran hello world program.

When I run my company’s Makefile to build a “complex” Fortran proprietary program, I get the following errors. (Note: xyz, abc.o and rst.o are not the actual identifiers in the output.)

Making xyz
Making xyz1.Linux
Loading xyz1 ...
gfortran -w -u -g -m32 -static xyz.o abc.o rst.o ../../libs/lib1/1.a -lpthread ../../libs/lib1/1.a -lpthread -o xyz

/bin/ld: cannot find -lpthread
/bin/ld: cannot find -lpthread
/bin/ld: cannot find -lgfortran
/bin/ld: cannot find -lm
/bin/ld: cannot find -lquadmath
/bin/ld: cannot find -lm
/bin/ld: cannot find -lc

collect2: error: ld returned 1 exit status 
make: *** [PROGRAM] Error 1

I found this in the gfortran directory that I downloaded gfortran to : gcc-11.1.0-32bit/lib/

And, I found these two symbolic links:

cd /bin;ls -laF ld
/bin/ld -> /etc/alternatives/ld*

cd /etc/alternatives;ls -laF ld 
/etc/alternatives/ld -> /usr/bin/ld.bfd*

cd ld.bfd
-bash: cd: ld.bfd: No such file or directory

Question: What do I do to compile everything with this Makefile (Fortran program)?

In order to get past a previous Makefile error: I executed the following command because the Makefile couldn’t find the csh:

sudo yum install csh

Well, you need the compiler and you have installed gfortran. You need the correct libraries and you need to tell your compiler how to find them. You do this in several ways, including setting up files in /etc/; setting environment variables such as LD_LIBRARY_PATH; and by placing proper switches on your compiler command. Starting with the most likely problems:

You are using gfortran but the switches “-w -u -m32” imply your makefile was originally set up for the Intel “ifort” (or “ifx”) command. So first, those switches are probably not correct, and you need to select ones appropriate for gfortran.

It is likely you have at least one pthread library on your machine, but you are using the -static switch, which requires libpthread.a not or something like that on your machine. A static load does not use the same files as a dynamic load, and some systems do not have static versions on them by default.

It would be best to have someone familiar with your programming environment take a look if this is on an administered machine; or someone else that has built this code on a similar platform; but for starters back up and take off the -w, -u, -m32 and -static switches and see if that builds. There are a lot of possibilities, but without being able to see your system and your Makefile that is the most likely place I would say you should start. Can you post your Makefile somewhere like a github repository, or here if short, or …(?). The output of these commands (I think, do not have a CENTOS box handy right now) would help

cat /etc/
ls /etc/
grep . /etc/*.conf
ls /etc/x86_64-linux-gnu/*pthread*
ls /lib*/*pthread*
# and look for files named *pthread* in any directories in the list of directories this shows:
 ld --verbose | grep SEARCH_DIR |xargs -n 1

1 Like

maybe better would be

ld --verbose|grep SEARCH_DIR|xargs -n 1|sed -e 's/.*=//' -e 's/).*//'|xargs -iXX find XX -name '*pthread.*'

I forgot that you said it was 32-bit so a few of the first lines I said to status really will not apply, but the overall output will still tell a lot.

1 Like

Thanks, it now compiles.

Great. If you are just building it to run on your machine so you can run it there, that is probably enough. If you are building it for others to use, you might want to build a static version, and you will need to add some packages (probably) in order to do so. If you are doing development I would recommend you add a number of switches that help detect run-time errors as well as non-standard usage and common errors.

So you might want to add such options to your gfortran command as

-Wall -Wextra -Wimplicit-interface -fmax-errors=3 -g -fcheck=bounds -fcheck=array-temps -fbacktrace

Everyone has their favorites; I would at least add “-Wall” if it still lets you compile.

Sounds like those can wait for another time, though.