1

I am writing a go service which references a C library, and I am having trouble getting it to compile. The library is installed under /usr/local/lib/sgp4 in the directories Linux/IFORT and Linux/GFORTRAN, which contain a bunch of .so files.

An environment variable LD_LIBRARY_PATH is set to /usr/local/lib/sgp4/Linux/IFORT.

Within my project, there is a set of wrapper .h files, and the CGO comments as follows: go project setup

At compile time, I was getting a bunch of warnings stating that several IFORT files required CFORTRAN files, but were not found. After the warning was dozens of undefined reference errors. See the full output below:

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/lto-wrapper
Target: x86_64-alpine-linux-musl
Configured with: /home/buildozer/aports/main/gcc/src/gcc-13-20230527/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl --target=x86_64-alpine-linux-musl --enable-checking=release --disable-cet --disable-fixed-point --disable-libstdcxx-pch --disable-multilib --disable-nls --disable-werror --disable-symvers --enable-__cxa_atexit --enable-default-pie --enable-default-ssp --enable-languages=c,c++,d,objc,go,fortran,ada --enable-link-serialization=2 --enable-linker-build-id --disable-libssp --disable-libsanitizer --enable-shared --enable-threads --enable-tls --with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues --with-system-zlib --with-linker-hash-style=gnu --with-pkgversion='Alpine 13.1.1_git20230527'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.1.1 20230527 (Alpine 13.1.1_git20230527) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-m64' '-o' '/tmp/go-build4211956026/b146/exe/a.out' '-rdynamic' '-O2' '-g' '-L/usr/local/lib/sgp4/Linux/IFORT' '-L/usr/local/lib/sgp4/Linux/GFORTRAN' '-v' '-O2' '-g' '-O2' '-g' '-O2' '-g' '-O2' '-g' '-no-pie' '-mtune=generic' '-march=x86-64' '-dumpdir' '/tmp/go-build4211956026/b146/exe/a.'
 /usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/collect2 -plugin /usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/lto-wrapper -plugin-opt=-fresolution=/tmp/ccoOhnee.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 --as-needed -export-dynamic -dynamic-linker /lib/ld-musl-x86_64.so.1 -z now -o $WORK/b146/exe/a.out /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crt1.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/crtbegin.o -L/usr/local/lib/sgp4/Linux/IFORT -L/usr/local/lib/sgp4/Linux/GFORTRAN -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1 -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../.. --compress-debug-sections=zlib /tmp/go-link-1885643238/go.o /tmp/go-link-1885643238/000000.o /tmp/go-link-1885643238/000001.o /tmp/go-link-1885643238/000002.o /tmp/go-link-1885643238/000003.o /tmp/go-link-1885643238/000004.o /tmp/go-link-1885643238/000005.o /tmp/go-link-1885643238/000006.o /tmp/go-link-1885643238/000007.o /tmp/go-link-1885643238/000008.o /tmp/go-link-1885643238/000009.o /tmp/go-link-1885643238/000010.o /tmp/go-link-1885643238/000011.o /tmp/go-link-1885643238/000012.o /tmp/go-link-1885643238/000013.o /tmp/go-link-1885643238/000014.o /tmp/go-link-1885643238/000015.o /tmp/go-link-1885643238/000016.o /tmp/go-link-1885643238/000017.o /tmp/go-link-1885643238/000018.o /tmp/go-link-1885643238/000019.o /tmp/go-link-1885643238/000020.o /tmp/go-link-1885643238/000021.o /tmp/go-link-1885643238/000022.o /tmp/go-link-1885643238/000023.o /tmp/go-link-1885643238/000024.o /tmp/go-link-1885643238/000025.o /tmp/go-link-1885643238/000026.o /tmp/go-link-1885643238/000027.o /tmp/go-link-1885643238/000028.o /tmp/go-link-1885643238/000029.o /tmp/go-link-1885643238/000030.o /tmp/go-link-1885643238/000031.o /tmp/go-link-1885643238/000032.o /tmp/go-link-1885643238/000033.o /tmp/go-link-1885643238/000034.o /tmp/go-link-1885643238/000035.o /tmp/go-link-1885643238/000036.o -ldllmain -lastrofunc -ltimefunc -lenvconst -ltle -lsgp4prop -lresolv -lpthread -ldl -lssp_nonshared -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/crtend.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crtn.o
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: libm.so.6, needed by /usr/local/lib/sgp4/Linux/IFORT/libdllmain.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: libpthread.so.0, needed by /usr/local/lib/sgp4/Linux/IFORT/libdllmain.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: libc.so.6, needed by /usr/local/lib/sgp4/Linux/IFORT/libdllmain.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: libdl.so.2, needed by /usr/local/lib/sgp4/Linux/IFORT/libdllmain.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: ld-linux-x86-64.so.2, needed by /usr/local/lib/sgp4/Linux/IFORT/libifcoremt.so.5, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: librt.so.1, needed by /usr/local/lib/sgp4/Linux/IFORT/libiomp5.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/sgp4/Linux/IFORT/libiomp5.so: undefined reference to `fdopen@GLIBC_2.2.5'

[...]

collect2: error: ld returned 1 exit status

After discovering this question I tried adding the flag -Wl,-rpath=/usr/local/lib/sgp4/Linux/GFORTRAN to the #cgo LDFLAGS comment, and it appeared to help. All of the reference errors were gone, except for the librt.so.1 file. Whereas the other reference errors were failing to find files under GFORTRAN, librt.so.1 isn't in the library code at all.

Might this one be coming from somewhere else, or would there be something wrong with the library code?

/usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/lto-wrapper
Target: x86_64-alpine-linux-musl
Configured with: /home/buildozer/aports/main/gcc/src/gcc-13-20230527/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl --target=x86_64-alpine-linux-musl --enable-checking=release --disable-cet --disable-fixed-point --disable-libstdcxx-pch --disable-multilib --disable-nls --disable-werror --disable-symvers --enable-__cxa_atexit --enable-default-pie --enable-default-ssp --enable-languages=c,c++,d,objc,go,fortran,ada --enable-link-serialization=2 --enable-linker-build-id --disable-libssp --disable-libsanitizer --enable-shared --enable-threads --enable-tls --with-bugurl=https://gitlab.alpinelinux.org/alpine/aports/-/issues --with-system-zlib --with-linker-hash-style=gnu --with-pkgversion='Alpine 13.1.1_git20230527'
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.1.1 20230527 (Alpine 13.1.1_git20230527) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/
LIBRARY_PATH=/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-m64' '-o' '/tmp/go-build2528347190/b146/exe/a.out' '-rdynamic' '-O2' '-g' '-L/usr/local/lib/sgp4/Linux/IFORT' '-v' '-O2' '-g' '-O2' '-g' '-O2' '-g' '-O2' '-g' '-no-pie' '-mtune=generic' '-march=x86-64' '-dumpdir' '/tmp/go-build2528347190/b146/exe/a.'
 /usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/collect2 -plugin /usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-alpine-linux-musl/13.1.1/lto-wrapper -plugin-opt=-fresolution=/tmp/ccheNcAl.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 --as-needed -export-dynamic -dynamic-linker /lib/ld-musl-x86_64.so.1 -z now -o $WORK/b146/exe/a.out /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crt1.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crti.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/crtbegin.o -L/usr/local/lib/sgp4/Linux/IFORT -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1 -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/lib -L/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../.. --compress-debug-sections=zlib /tmp/go-link-2656843684/go.o /tmp/go-link-2656843684/000000.o /tmp/go-link-2656843684/000001.o /tmp/go-link-2656843684/000002.o /tmp/go-link-2656843684/000003.o /tmp/go-link-2656843684/000004.o /tmp/go-link-2656843684/000005.o /tmp/go-link-2656843684/000006.o /tmp/go-link-2656843684/000007.o /tmp/go-link-2656843684/000008.o /tmp/go-link-2656843684/000009.o /tmp/go-link-2656843684/000010.o /tmp/go-link-2656843684/000011.o /tmp/go-link-2656843684/000012.o /tmp/go-link-2656843684/000013.o /tmp/go-link-2656843684/000014.o /tmp/go-link-2656843684/000015.o /tmp/go-link-2656843684/000016.o /tmp/go-link-2656843684/000017.o /tmp/go-link-2656843684/000018.o /tmp/go-link-2656843684/000019.o /tmp/go-link-2656843684/000020.o /tmp/go-link-2656843684/000021.o /tmp/go-link-2656843684/000022.o /tmp/go-link-2656843684/000023.o /tmp/go-link-2656843684/000024.o /tmp/go-link-2656843684/000025.o /tmp/go-link-2656843684/000026.o /tmp/go-link-2656843684/000027.o /tmp/go-link-2656843684/000028.o /tmp/go-link-2656843684/000029.o /tmp/go-link-2656843684/000030.o /tmp/go-link-2656843684/000031.o /tmp/go-link-2656843684/000032.o /tmp/go-link-2656843684/000033.o /tmp/go-link-2656843684/000034.o /tmp/go-link-2656843684/000035.o /tmp/go-link-2656843684/000036.o -rpath=/usr/local/lib/sgp4/Linux/GFORTRAN -rpath=/usr/local/lib/sgp4/Linux/IFORT -ldllmain -lastrofunc -ltimefunc -lenvconst -ltle -lsgp4prop -lresolv -lpthread -ldl -lssp_nonshared -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/crtend.o /usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../lib/crtn.o
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: warning: librt.so.1, needed by /usr/local/lib/sgp4/Linux/IFORT/libiomp5.so, not found (try using -rpath or -rpath-link)
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/sgp4/Linux/IFORT/libiomp5.so: undefined reference to `shm_open@GLIBC_2.2.5'
/usr/lib/gcc/x86_64-alpine-linux-musl/13.1.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/sgp4/Linux/IFORT/libiomp5.so: undefined reference to `shm_unlink@GLIBC_2.2.5'
collect2: error: ld returned 1 exit status
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
  • You're linking things built against musl against things built against glibc. That doesn't work, in general. Use _only_ things built against musl or _only_ things built against glibc. – Charles Duffy Jul 19 '23 at 21:24
  • (If the question is where the glibc dependency is coming in, _shrug_; you could use `objdump` to check; heck, `grep` might be good enough: it doesn't give you details about binary files, but it at least says where there is or isn't a match, so you can see which files contain `@GLIBC_2.2.5`). – Charles Duffy Jul 19 '23 at 21:27
  • (so, f/e, where did you get this `IFORT` from? If it wasn't compiled against musl itself, you can't use it for things you're compiling for musl now). – Charles Duffy Jul 19 '23 at 21:29
  • @CharlesDuffy it came from Space-Track.org, and is a SGP4 Propagator library. It only contains the `IFORT` and `GFORTRAN` directories, and then provides the wrapper h files and an example of how to use it in Golang. The imports in the Go file that was shared was copied straight from the example in the library (minus the one flag I added). – robbieperry22 Jul 19 '23 at 22:23
  • 1
    If space-track.org doesn't explicitly provide an alpine/musl library, you probably need to build against glibc when using it. Is it really important to you to stick with Alpine? -- switching to a larger / less space-efficient / more mainstream distro using GNU's libc should make this issue go away. (It's also possible to install and compile against glibc on alpine, but honestly, I'm not volunteering to figure out the details of making that work and walking you through it here). – Charles Duffy Jul 19 '23 at 23:49
  • 1
    (mind, if there's enough source provided to be able to try to compile IFORT and GFORTRAN against musl instead of reusing prebuilt object files you may be able to do that; but if the authors never anticipated that it may be some work: software that was developed against glibc often uses extensions and doesn't work properly against a more strictly compliant libc without needing to put some work into porting). – Charles Duffy Jul 19 '23 at 23:52
  • @CharlesDuffy yeah I'm kind of stuck with Alpine here as I am trying to build this in a CI pipeline that I have no control over. I will look into compiling against glibc, thanks! – robbieperry22 Jul 20 '23 at 19:25
  • 1
    I'd hope that your CI person would be willing and able to help you out here -- that's one of the hats I wear, and I'd certainly be glad to take the wheel if a developer escalated this issue to me. – Charles Duffy Jul 20 '23 at 19:34
  • @CharlesDuffy I was able to switch to debian here for the build, and it resolved the issue! If you'd like, you can post an answer and I'll mark it as accepted. – robbieperry22 Jul 26 '23 at 23:32

0 Answers0