14

source file

rsetti::fastidio { /tmp }-> cat foo.c

    #include <stdio.h>
    void ACFunction() {
      printf("ACFunction()\n");
      AGoFunction();
    }

compilation of shared lib

rsetti::fastidio { /tmp }-> clang -shared -o libfoo.so foo.c

    foo.c:4:3: warning: implicit declaration of function 'AGoFunction' is invalid in C99 [-Wimplicit-function-declaration]
      AGoFunction();
      ^
    1 warning generated.
    Undefined symbols for architecture x86_64:
      "_AGoFunction", referenced from:
          _ACFunction in foo-lFDQ4g.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

rsetti::fastidio { /tmp }->

the same code on linux + gcc can be easily compiled.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
giskard
  • 698
  • 1
  • 7
  • 11
  • possible duplicate of [What is the deal with undefined symbols in a shared library or dylib?](http://stackoverflow.com/questions/3695234/what-is-the-deal-with-undefined-symbols-in-a-shared-library-or-dylib) – nos Feb 20 '14 at 12:52
  • Where is the code or object for AGoFunction? – mmmmmm Feb 20 '14 at 13:58

1 Answers1

36

By using:

-Wl,-undefined -Wl,dynamic_lookup

or

clang -shared -undefined dynamic_lookup -o libfoo.so foo.c

seems to maintain the same behaviour of GCC.

giskard
  • 698
  • 1
  • 7
  • 11
  • That did the trick. I knew it must have been something simple. Onwards! – oorst Nov 22 '16 at 02:24
  • 2
    `.dylib` is the canonical extension for a shared library on macOS, using `-dynamiclib` instead of `-shared`. See https://stackoverflow.com/questions/2339679/what-are-the-differences-between-so-and-dylib-on-osx – rgov Jan 06 '19 at 18:21
  • 2
    Please note that "-undefined dynamic_lookup" has unwanted side effects, like silencing the compiler diagnostics when you have some undefined symbols in your classes. These undefined symbols will then pop up later when trying to load your library at runtime with misleading errors, like "dlopen: image not found". See https://stackoverflow.com/a/30934307/71689 for details. – ezolotko Oct 25 '19 at 13:31