简体   繁体   中英

Why does lazy linking option cause clang “illegal data reference” error?

When using the "lazy linking" link option "-lazy-lz" referenced in this question to delay the loading of a dependent dynamic library, the linker that's part of Xcode 7.2.1 (Apple LLVM version 7.0.2 (clang-700.1.81)), generates this error:

ld: illegal data reference to __ZN9WBRefSpecD1Ev in lazy loaded dylib

...where the mangled C++ symbol refers to the class destructor in my single-class dylib: _WBRefSpec::~WBRefSpec()

I can't find a direct reference anywhere to indicate what this error could possibly mean -- or what could cause it.

In the .cpp file, the destructor is defined:

EXPORT WBRefSpec::~WBRefSpec(void)
{
    ClearEntireRefSpec();  // commenting out this call doesn't affect error message!
}

...where EXPORT is the usual:

#define EXPORT __attribute__((visibility("default")))

...and of course, defined in the header file as a public member of the class:

~WBRefSpec(void);

Anyone ever seen this or have a clue what causes this error?

EDIT / ANSWER:

The answer to the illegal data reference was that there WAS a .cpp file with a class member function defined that declared "static WBRefSpec foo;" Removed that, and bingo, no link error.

(removed link details, since they were not relevant to the issue)

The answer to the illegal data reference was that there WAS a .cpp file with a class member function defined that declared "static WBRefSpec foo;"

WBRefSpec& XMLErrorLogFile::GetLogFileInAppFolder()
{
    static WBRefSpec logFileInAppFolder;
    return logFileInAppFolder;
}

As a test, I removed static, and bingo, no link error.

But NOTE: just editing out "static" in this case would a Very Bad Idea and create another Very Serious Problem: returning a reference to an object allocated on the stack!

I think if I make a class data member that's a static WBRefSpec*, and just initialize it in my function (but only once) then the issue will also go away.

The irony is that I choose that pattern based on an excellent SO answer to someone else's unrelated posting .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM