r/programminghelp • • 2d ago

C Compiling gcc: Undefined references

I am trying to compile gcc from source. I have the source, I got the prerequisites (had to separately install some stuff: gmp, mpfr, mpc), and then I go into the gcc build folder and configure using the command

../gcc-16.2.0-source/configure --prefix="$HOME/downloads/gcc-16.2.0-install" LDFLAGS="-L$HOME/downloads/mpfr-4.2.2-install/lib -t -lm" --disable-multilib

(Without the LDFLAGS thing it would complain it couldn't find the gmp/mpfr/mpc libraries)

And then I run "make", and after a long time it shows

fold-const-call.o: In function `fold_const_call_sss(real_value*, combined_fn, real_value const*, real_value const*, real_format const*)':
fold-const-call.cc:(.text+0x624): undefined reference to `mpfr_atan2pi'
fold-const-call.o: In function `fold_const_call_1(combined_fn, tree_node*, tree_node*)':
fold-const-call.cc:(.text+0x2c55): undefined reference to `mpfr_cospi'
fold-const-call.cc:(.text+0x2c72): undefined reference to `mpfr_atanpi'
fold-const-call.cc:(.text+0x2cc8): undefined reference to `mpfr_asinpi'
fold-const-call.cc:(.text+0x2ced): undefined reference to `mpfr_sinpi'
fold-const-call.cc:(.text+0x2d0a): undefined reference to `mpfr_tanpi'
fold-const-call.cc:(.text+0x377b): undefined reference to `mpfr_acospi'
fold-const-call.o: In function `fold_const_call_sss(real_value*, combined_fn, real_value const*, real_value const*, real_format const*)':
fold-const-call.cc:(.text+0x624): undefined reference to `mpfr_atan2pi'
fold-const-call.o: In function `fold_const_call_1(combined_fn, tree_node*, tree_node*)':
fold-const-call.cc:(.text+0x2c55): undefined reference to `mpfr_cospi'
fold-const-call.cc:(.text+0x2c72): undefined reference to `mpfr_atanpi'
fold-const-call.cc:(.text+0x2cc8): undefined reference to `mpfr_asinpi'
fold-const-call.cc:(.text+0x2ced): undefined reference to `mpfr_sinpi'
fold-const-call.cc:(.text+0x2d0a): undefined reference to `mpfr_tanpi'
fold-const-call.cc:(.text+0x377b): undefined reference to `mpfr_acospi'
fortran/simplify.o: In function `gfc_simplify_acosd(gfc_expr*)':
simplify.cc:(.text+0x5cda): undefined reference to `mpfr_acosu'
fortran/simplify.o: In function `gfc_simplify_asind(gfc_expr*)':
simplify.cc:(.text+0x5dca): undefined reference to `mpfr_asinu'
fortran/simplify.o: In function `gfc_simplify_atand(gfc_expr*)':
simplify.cc:(.text+0x5e80): undefined reference to `mpfr_atanu'
fortran/simplify.o: In function `gfc_simplify_atan2d(gfc_expr*, gfc_expr*)':
simplify.cc:(.text+0x6c4f): undefined reference to `mpfr_atan2u'
fortran/simplify.o: In function `gfc_simplify_cosd(gfc_expr*)':
simplify.cc:(.text+0x6de0): undefined reference to `mpfr_cosu'
fortran/simplify.o: In function `gfc_simplify_sind(gfc_expr*)':
simplify.cc:(.text+0x6e70): undefined reference to `mpfr_sinu'
fortran/simplify.o: In function `gfc_simplify_tand(gfc_expr*)':
simplify.cc:(.text+0x6f00): undefined reference to `mpfr_tanu'
fortran/simplify.o: In function `gfc_simplify_cotand(gfc_expr*)':
simplify.cc:(.text+0x6fbe): undefined reference to `mpfr_tanu'
fortran/simplify.o: In function `gfc_simplify_acospi(gfc_expr*)':
simplify.cc:(.text+0x7163): undefined reference to `mpfr_acospi'
fortran/simplify.o: In function `gfc_simplify_asinpi(gfc_expr*)':
simplify.cc:(.text+0x7223): undefined reference to `mpfr_asinpi'
fortran/simplify.o: In function `gfc_simplify_atanpi(gfc_expr*)':
simplify.cc:(.text+0x72ab): undefined reference to `mpfr_atanpi'
fortran/simplify.o: In function `gfc_simplify_atan2pi(gfc_expr*, gfc_expr*)':
simplify.cc:(.text+0x7375): undefined reference to `mpfr_atan2pi'
fortran/simplify.o: In function `gfc_simplify_cospi(gfc_expr*)':
simplify.cc:(.text+0x740b): undefined reference to `mpfr_cospi'
fortran/simplify.o: In function `gfc_simplify_sinpi(gfc_expr*)':
simplify.cc:(.text+0x749b): undefined reference to `mpfr_sinpi'
fortran/simplify.o: In function `gfc_simplify_tanpi(gfc_expr*)':
simplify.cc:(.text+0x752b): undefined reference to `mpfr_tanpi'
fold-const-call.o: In function `fold_const_call_sss(real_value*, combined_fn, real_value const*, real_value const*, real_format const*)':
fold-const-call.cc:(.text+0x624): undefined reference to `mpfr_atan2pi'
fold-const-call.o: In function `fold_const_call_1(combined_fn, tree_node*, tree_node*)':
fold-const-call.cc:(.text+0x2c55): undefined reference to `mpfr_cospi'
fold-const-call.cc:(.text+0x2c72): undefined reference to `mpfr_atanpi'
fold-const-call.cc:(.text+0x2cc8): undefined reference to `mpfr_asinpi'
fold-const-call.cc:(.text+0x2ced): undefined reference to `mpfr_sinpi'
fold-const-call.cc:(.text+0x2d0a): undefined reference to `mpfr_tanpi'
fold-const-call.cc:(.text+0x377b): undefined reference to `mpfr_acospi'
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmma'
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmms'
collect2: error: ld returned 1 exit status
make[3]: *** [lto1] Error 1
make[3]: *** Waiting for unfinished jobs....
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmma'
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmms'
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmma'
/home/CENSORED/downloads/mpc-1.3.1-install/lib/libmpc.so: undefined reference to `mpfr_fmms'
collect2: error: ld returned 1 exit status
make[3]: *** [lto-dump] Error 1
collect2: error: ld returned 1 exit status
make[3]: *** [f951] Error 1
make[3]: Leaving directory `/home/CENSORED/downloads/gcc-16.2.0-build/gcc'
make[2]: *** [all-stage2-gcc] Error 2
make[2]: Leaving directory `/home/CENSORED/downloads/gcc-16.2.0-build'
make[1]: *** [stage2-bubble] Error 2
make[1]: Leaving directory `/home/CENSORED/downloads/gcc-16.2.0-build'
make: *** [all] Error 2

I have no idea what to do, I'm not a programmer, I just need this to work so I can move on and use certain programs (like geant4). What do I do? How do I fix this? I am literally genuinely rolling on the floor sobbing right now

1 Upvotes

17 comments sorted by

2

u/Lewinator56 2d ago

Why are you compiling GCC if you already have a working build of GCC that can compile GCC?

You shouldn't need a special build of GCC to compile geant4

1

u/itsfakenoone 2d ago

This is on a remote computing server where GCC is not up-to-date enough for geant4, but I don't have permission to just update it the 'normal' way, so now the only way as far as I can see (as suggested by my advisor, who is also not a programmer for what that's worth but he has done something similar before) is to install a sufficiently recent version parallelly from scratch in my home folder

1

u/Lewinator56 2d ago

Speak to the admin for the cluster - they should be able to update GCC for you. Chances are that there's also some libraries that are out of date that the GCC on the system needs to load. You may need to provide literally everything for GCC, but then there's no guarantee your version will even run if it expects libraries that don't exist.

1

u/itsfakenoone 2d ago

Okay, I will try and do that, but to better understand, are you saying what I'm attempting is difficult, or impossible? If I find and give it everything it needs, should it not run? The strange part is that it finds references to stuff from mpfr to be undefined, but i have already installed mpfr and 'told' gcc about it...

1

u/gmes78 2d ago

I don't think you're specifying your dependencies correctly. However, there's a much easier way. Get rid of your LDFLAGS and follow what the prerequisites page describes: you can just put the dependency source code inside the gcc tree, and it'll be built alongside it.

If an MPFR source distribution is found in a subdirectory of your GCC sources named mpfr, it will be built together with GCC.

(Same for the other dependencies.)

There's a helper script, contrib/download_prerequisites, that does this for you.

1

u/itsfakenoone 1d ago

Okay, I tried this and of course there are more stupid errors to deal with. Maybe you can help, let me explain in detail. I have a "gcc-source" folder where I have the source for gcc and subfolders with the source for its prerequisites. Then I have empty "gcc-build" and "gcc-install" folders. I go to the "gcc-build" folder and run

../gcc-16.2.0-source/configure --prefix="/home/CENSORED/downloads/gcc-16.2.0-install" --disable-multilib

which completes without errors. Then I run make, which says

configure: error: source directory already configured; run "make distclean" there first

I try going back to the "gcc-source" folder and running make distclean, and it says

make: *** No rule to make target `distclean'.  Stop.

What does it even want me to do? ;_;

2

u/el0j 1d ago

I don't have a source tree to look at, so I would assume this is the default rule, but after ./configure you use 'make bootstrap' to build. If that doesn't work, you screwed something up. Just remove the build directory and redo.

(also you shouldn't do anything but run the contrib/download_prerequisites script. Do not decompress the files downloaded, let the build handle that)

1

u/itsfakenoone 9h ago

running the contrib/download_prerequisites script and then configure and then make bootstrap worked. Not sure how make bootstrap is different from just make or why it's not mentioned in the official instructions, but anyways, Thank you so much! I am forever in your debt.

1

u/gmes78 21h ago

Why are you reusing your existing, dirty source tree? Delete everything and extract the GCC sources again.

1

u/itsfakenoone 9h ago

i would have assumed the building process looks at the source code and does something with it, rather than irreversibly altering the source code itself, which is kind of counter intuituve...

1

u/gmes78 9h ago

It isn't the source code, it's the generated build files.

I didn't notice you were using a separate build directory; deleting that would suffice.

1

u/wahnsinnwanscene 23h ago

You ever get recommendations that are simply spurious? Yes so distclean is a target but it isn't there. Usually it's to remove everything that's been compiled but it really can be anything. When in doubt, remove everything and redownload including mpfr.

1

u/dfx_dj 1d ago

These are errors from the linker, telling you that not all object files that are required to make the final target file are present.

My first guess would be that you manually overriding the LDFLAGS is what causes this. The -L option only tells the build scripts and the linker where the find libraries, but it doesn't actually link any of them. You need additional -l options (lower case) to do that, similar to how the math lib (-lm) is linked in. These may be given in other variables, but may also be in the LDFLAGS that you've overridden.

So, try to figure out which libraries are needed and add them into the LDFLAGS, for example add -lmpfr (assuming that's what the file is called).

(This is just a wild shot in the dark since I'm not familiar with how the gcc build process actually works.)

1

u/itsfakenoone 1d ago

Thanks for the suggestion. I have tried again with -lmpfr, but unfortunately no dice

1

u/Classic-Rate-5104 1d ago

Seriously, you aren't a programmer and you try to build gcc. Why?

1

u/itsfakenoone 16h ago

i need the latest version of gcc for building a program i need to use, on a remote computing server where gcc is not up-to-date but i don't have admin privileges to update it, so i need to install gcc from scratch in my home folder. crazy world out there

1

u/Classic-Rate-5104 4h ago

Using a podman or docker container (if you have privileges to do that) would be a shorter way