Steve Dell
Steve Dell

Reputation: 605

Clang - "symbol not found" when linking

I'm trying to compile this source in clang:

extern "C" void __declspec(dllexport) TEST(int num)
{
return;
}

Problem is that on link, clang reports "Cannot export _TEST: symbol not found"

I can't find a way, however, to prevent clang from mangling names

I've been reading that this is a bug in clang, but it's hard to believe they haven't fixed it by now

Any ideas?

Upvotes: 1

Views: 1295

Answers (1)

user1129665
user1129665

Reputation:

The content of the .drectve section of the generated assembly file by clang contains:

.section    .drectve,"r"
.ascii  " -export:_TEST"

while it should've been as it with gcc:

.section    .drectve,"r"
.ascii  " -export:TEST"

You can work around it by removing __declspec(dllexport) and creating a file file.s containing the symbols you want to export as the following:

.section .drectve,"r"
    # list of symbols you want to export
    .ascii " -export:TEST"

keep an empty line at the end, it's not displayed here.

When ever you want to export a symbol add .ascii " -export:SYMBOL_NAME". Now you can compile it with:

clang++ file.cpp file.s -shared -o file.dll

it should work fine.

Upvotes: 4

Related Questions