10

The OS X dynamic loader often gives errors like

Symbol not found: <some symbol name>
Referenced from: <lib location>
Expected in: flat namespace

If you search for the error you'll find a ton of errors that have it. I understand roughly that the problem is an incorrect linking, but none of the answers explain what "flat namespace" means here, exactly.

1
  • Hi, I'm getting the same error when linking dylib from executable that posses symbol used by that dylib. if I compile the executable in Xcode under debug mode, it works.. but switching to release mode, and the process refuse to start with the same error you mentions. Any idea what trigger this ?
    – Zohar81
    Jan 30, 2018 at 14:25

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

14

Disclaimer: This answer only applies to macOS 10.4 and higher. E.g. In previous versions of 'OSX' flat-namespaces were the default. And on Windows resolving can also work slightly different, depending on the mode though.

Prolog

Let's first talk about the background of namespaces. In MacOS there are two so-called namespaces available. Two-level namespace and single namespace (also known as flat-namespace).

Flat-namespace

Each process has a huge table which contains all symbols (functions, external variables, ..). When a library gets loaded it might depend on a symbol and therefore can look in the flat-namespace to find which other library or executable 'offers' it. After all symbols were found, the library adds his own symbols to the list. One huge disadvantage is the amount of possible collisions. A process might load two or more libraries with the same symbol names. It is up to the OS how to deal with collisions, but in most cases it's simply undefined behaviour and leads to a crash because of mixed libraries.

Two-level namespace

In a two-level namespace, instead of a plain table with symbol names, the symbols are saved with name and their library name (so where they come from). A library that relies on a symbol needs to know exactly from which library it wants to get the symbol XYZ resolved.

What that means for your problem

The lib was obviously compiled with a flat-namespace option and requires the symbol to run properly. A solution is to load the library it depends on before the lib gets loaded that causes the problem.

Also interesting to find the solution to your problem:

Run otool -hV on the library. If you don't see TWOLEVEL, then it was linked with -flat-namespace.

1
  • 2
    The background on symbol lookup makes this error so much easier to understand. Thanks for that.
    – Don
    Oct 14, 2019 at 15:13
0

I faced the error while trying to run psycopg2-binary on macOS Big Sur.

Solution:

Remove psycopg2-binary and reinstall using the --no-cache-dir

0

Uninstall:

pip uninstall psycopg2

Reinstall:

pip install --no-cache-dir psycopg2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.