enzo1982 / freac

The fre:ac audio converter project
https://www.freac.org/
GNU General Public License v2.0
1.41k stars 72 forks source link

Link with C++ compiler instead of linking with C compiler and adding -lstdc++ #257

Open ryandesign opened 2 years ago

ryandesign commented 2 years ago

The build system uses the C compiler as the linker and adds the flag -lstdc++, for example:

/usr/bin/clang objects/gui.o  -lstdc++ -L/opt/local/lib -lsmooth-0.9  -L/opt/local/lib -Wl,-headerpad_max_install_names -Wl,-syslibroot,/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -arch x86_64 -o bin/freac

This is wrong because the C++ standard library being used might not be libstdc++; the default C++ standard library on macOS has been libc++ since OS X 10.9. Because this mistake is so common the linker seems to silently transform -lstdc++ to -lc++ to correct the problem, but it would be better not to make the mistake in the first place.

Usually, when you're linking stuff that came from C++ code, I've seen projects use the C++ compiler (not the C compiler) as the linker; that way, you don't have to specify to link with a C++ standard library. Make sure to use the CXXFLAGS environment variable when you do so in case the user has specified a -stdlib flag there that differs from what the default C++ standard library is.

enzo1982 commented 1 year ago

This is fixed now with commit 3e9100c.

CXXFLAGS is not used for linking, though, as that would add compile-time flags to the linker command line. To use an alternative stdlib, the -stdlib option should be added to both, CXXFLAGS and LDFLAGS.