0

I'm working on an arm64 box. nm shows that my liblib1.so file has the defined function foo_incremented, ld cannot find it.

~/code/pack/inlib$ nm -D --demangle --defined --extern-only liblib1.so
0000000000011020 B __end__
0000000000000748 T _fini
0000000000000730 T foo_incremented
00000000000005c0 T _init

My project simply has 5 files: lib1.h, lib1.c, a.c, a.cc and Makefile.

// lib1.c
int foo_incremented(int x) {
  return x + 1;
}
// lib1.h
#ifndef LIB1_H
#define LIB1_H

#ifdef __cplusplus__
extern "C" {
#endif

extern int foo_incremented(int x);

#ifdef __cplusplus__
}
#endif

#endif
// a.c
#include <lib1.h>
#include <stdio.h>


int main(int argc, char* argv[]) {
    int n = foo_incremented(11);
    printf("foo%d\n", n);
    return 0;
}
//a.cc
#include <lib1.h>
#include <iostream>


int main(int argc, char* argv[]) {
    int n = foo_incremented(11);
    std::cout<<n<<"\n";
    std::cout<<"foo"<<"\n";
    return 0;
}
# Makefile
.phony:
    clean

lib1_source=lib1.c lib1.h


clean:
    -rm lib1.c.235r.ira

liblib1.so: $(lib1_source)
    cc -dynamic -o $@ $<

a: a.c lib1.h
    cc -v -Wl,-rpath=. -L. -I. -llib1 -o $@ $<

acc: a.cc lib1.h
    c++ -Wl,-v,-rpath=. -L. -I. -llib1 -o $@ $<

The output of make a:

~/code/pack/inlib$ make a
cc -Wl,-v,-rpath=. -L. -I. -llib1 -o a a.c
collect2 version 5.4.0 20160609
/usr/bin/ld -plugin /usr/lib/gcc/aarch64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/aarch64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccimfYnQ.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 --sysroot=/ --build-id --eh-frame-hdr --hash-style=gnu --as-needed -dynamic-linker /lib/ld-linux-aarch64.so.1 -X -EL -maarch64linux --fix-cortex-a53-843419 -z relro -o a /usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu/crt1.o /usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu/crti.o /usr/lib/gcc/aarch64-linux-gnu/5/crtbegin.o -L. -L/usr/lib/gcc/aarch64-linux-gnu/5 -L/usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu -L/usr/lib/gcc/aarch64-linux-gnu/5/../../../../lib -L/lib/aarch64-linux-gnu -L/lib/../lib -L/usr/lib/aarch64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/aarch64-linux-gnu/5/../../.. -v -rpath=. -llib1 /tmp/cc0K5Hhf.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/aarch64-linux-gnu/5/crtend.o /usr/lib/gcc/aarch64-linux-gnu/5/../../../aarch64-linux-gnu/crtn.o
GNU ld (GNU Binutils for Ubuntu) 2.26.1
/tmp/cc0K5Hhf.o: In function `main':
a.c:(.text+0x14): undefined reference to `foo_incremented'
collect2: error: ld returned 1 exit status
Makefile:14: recipe for target 'a' failed
make: *** [a] Error 1

The output of make acc is pretty much the same as the previous one.

ld version: GNU ld (GNU Binutils for Ubuntu) 2.26.1, gcc version: gcc (Ubuntu/Linaro 5.4.0-6kord1~16.04.12) 5.4.0 20160609

Michaelzh
  • 441
  • 5
  • 14
  • When building some code against some library provided by others on this computer, I also encounter this problem: undefined reference to `xxx... – Michaelzh Jun 08 '22 at 03:02
  • There is no such macro `#ifdef __cplusplus__`. It should be `#ifdef __cplusplus`. It is a reason why `foo_incremented` is not found if the name is mangled, since `extern "C"` has not been applied. – 273K Jun 08 '22 at 03:03
  • 2
    The position of `-l` flags matters. They should follow _after_ the object files. So move `-llib1` after `$<`. – user17732522 Jun 08 '22 at 03:04
  • @273K I don't know why the C++ part is even shown in the question. The compilation actually never gets to it and fails on the `a.c` target. (Although of course that would be the next problem.) – user17732522 Jun 08 '22 at 03:05
  • @user17732522 You are right. I just saw `//a.cc` and skipped the further info. – 273K Jun 08 '22 at 03:06
  • "There is no such macro `#ifdef __cplusplus__`". That's true, but I think there used to be in some compiler in the past, as I've seen it used in older codebases. – kirsch Feb 22 '23 at 13:51

1 Answers1

0

@user17732522 gave me a good answer, thanks! After change the position of -llib1 to the end, it builds.

Michaelzh
  • 441
  • 5
  • 14