`undefined reference` and `bad reloc address` errors while using BinaryBuilder

I’m trying to use BinaryBuilder for a private package, but I am getting some errors that I don’t understand. I can’t share the code, but I’m hoping that something in the error message or in my build_tarballs.jl or makefile (included below) will spark an idea from someone.

Error messages
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x141d): undefined reference to `bodn2c_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1433): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x161e): undefined reference to `ltime_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1623): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1818): undefined reference to `subslr_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1842): undefined reference to `reclat_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1847): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1a68): undefined reference to `spkpos_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1a92): undefined reference to `recrad_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1af5): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1c3e): undefined reference to `lspcn_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1c50): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1f4a): undefined reference to `spkgeo_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x1f4f): undefined reference to `failed_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x2190): undefined reference to `latsrf_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x22d3): undefined reference to `ilumin_c'
.build/common/source/Ephemeris.o:Ephemeris.cpp:(.text+0x22d8): undefined reference to `failed_c'
/opt/x86_64-w64-mingw32/bin/../lib/gcc/x86_64-w64-mingw32/4.8.5/../../../../x86_64-w64-mingw32/bin/ld: .build/common/source/Ephemeris.o: bad reloc address 0x0 in section `.data'
collect2: error: ld returned 1 exit status
make: *** [makefile:12: .build/libgram.dll] Error 1
Previous command exited with 2
build_tarballs.jl
using BinaryBuilder

name = "GRAM"
version = v"1.4.0"
sources = [
    DirectorySource("GRAM"),
]

script = raw"""
cd ${WORKSPACE}/srcdir
make

cp .build/libgram.${dlext} ${prefix}/lib/
"""

platforms = supported_platforms(
    exclude = function(platform)
        startswith(triplet(platform), "armv6l-linux") ||
            startswith(triplet(platform), "aarch64-apple-darwin")
    end,
)
platforms = expand_cxxstring_abis(platforms)

products = [
    LibraryProduct("libgram", :libgram),
]

dependencies = [
    Dependency("CSPICE_jll"),
]

build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies; julia_compat = "1.6")
makefile
SUBDIRECTORIES := common Earth Jupiter Mars Neptune Titan Uranus Venus
SOURCE_FILES := $(foreach dir,$(SUBDIRECTORIES),$(wildcard $(dir)/source/*.cpp))
INCLUDE_DIRECTORIES := $(foreach dir,$(SUBDIRECTORIES),$(dir)/include)
OBJECT_FILES := $(addprefix .build/,$(patsubst %.cpp,%.o,$(SOURCE_FILES)))

.PHONY: all
all: .build/libgram.$(dlext)

.build/libgram.$(dlext): $(OBJECT_FILES)
	$(CXX) $(LDFLAGS) $(LIBS) -shared -fPIC -L$(prefix)/lib -lcspice -o $@ $^

.build/%.o: %.cpp
	mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) $(foreach dir,$(INCLUDE_DIRECTORIES),-I$(dir)) -I${prefix}/include -std=c++11 -fPIC -Wall -c -o $@ $<

.PHONY: clean
clean:
	$(RM) -r .build

The undefined reference errors are all related to the CSPICE library, which is in Yggdrasil.

My setup works for many platforms, but yields the above errors for a few. Most recently it failed for x86_64-w64-mingw32 and i686-w64-mingw32. It may only be Windows platforms where it fails, but I’m not certain, yet.

Does anyone have any ideas for me on how to proceed?

I think this is a link order problem. Try moving -lcspice to the end of the link command:

$(CXX) $(LDFLAGS) $(LIBS) -shared -fPIC -L$(prefix)/lib -o $@ $^ -lcspice

Good catch! Thank you! I knew that within 15 minutes of posting this, one of the many smart people here would spot my error…

Side note, when having this kind of problems it’d be helpful to always show the compiler invocation, not just the error message.