[LLVMdev] Errors linking against libLLVMCore

131 views
Skip to first unread message

Rick Mann

unread,
Dec 26, 2012, 1:32:14 AM12/26/12
to llv...@cs.uiuc.edu
I'm trying to make a library in Xcode that links against LLVM. I used the STL C++ template in Xcode 4.5.2, added libLLVMCore.a and libLLVMSupport.a to the link binaries phase, and made this call in my code:

llvm::LLVMContext& llvmCTX = llvm::getGlobalContext();

I get link errors against std::string and other STL classes:

Undefined symbols for architecture x86_64:
"std::string::copy(char*, unsigned long, unsigned long) const", referenced from:
llvm::sys::Path::makeUnique(bool, std::string*) in libLLVMSupport.a(Path.o)
"std::string::find(char const*, unsigned long, unsigned long) const", referenced from:
llvm::sys::getDefaultTargetTriple() in libLLVMSupport.a(Host.o)
.
.
.

Builds fine if I take out that line (the only line, so far) of LLVM code.

But code like this compiles fine:

std::string s = "Hello world";
std::printf("%s", s.c_str());

I figure I'm missing a lib, but I have no idea which.

TIA,

--
Rick




_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

John Criswell

unread,
Dec 27, 2012, 1:24:57 PM12/27/12
to Rick Mann, llv...@cs.uiuc.edu
On 12/26/12 12:32 AM, Rick Mann wrote:
> I'm trying to make a library in Xcode that links against LLVM. I used the STL C++ template in Xcode 4.5.2, added libLLVMCore.a and libLLVMSupport.a to the link binaries phase, and made this call in my code:
>
> llvm::LLVMContext& llvmCTX = llvm::getGlobalContext();
>
> I get link errors against std::string and other STL classes:
>
> Undefined symbols for architecture x86_64:
> "std::string::copy(char*, unsigned long, unsigned long) const", referenced from:
> llvm::sys::Path::makeUnique(bool, std::string*) in libLLVMSupport.a(Path.o)
> "std::string::find(char const*, unsigned long, unsigned long) const", referenced from:
> llvm::sys::getDefaultTargetTriple() in libLLVMSupport.a(Host.o)
> .
> .
> .
>
> Builds fine if I take out that line (the only line, so far) of LLVM code.
>
> But code like this compiles fine:
>
> std::string s = "Hello world";
> std::printf("%s", s.c_str());
>
> I figure I'm missing a lib, but I have no idea which.

It is possible (but a bit unlikely) that your problem is because you're
not linking in libstdc++. Some C++ methods are defined in header files;
that might explain why your simple program works but using
libLLVMVMCore.a doesn't work.

Try using std::string::find() in your simple program and see if it
links. If it doesn't, then I think you just need to add -lstdc++.

-- John T.

>
> TIA,

Rick Mann

unread,
Dec 27, 2012, 7:41:48 PM12/27/12
to John Criswell, llv...@cs.uiuc.edu

On Dec 27, 2012, at 10:24 , John Criswell <cris...@illinois.edu> wrote:

> It is possible (but a bit unlikely) that your problem is because you're not linking in libstdc++. Some C++ methods are defined in header files; that might explain why your simple program works but using libLLVMVMCore.a doesn't work.
>
> Try using std::string::find() in your simple program and see if it links. If it doesn't, then I think you just need to add -lstdc++.

I've been comparing how the clang++ CLI builds vs how Xcode builds. For one, Xcode does separate clang -x c++ and clang++ (for linking) steps.

Xcode calls clang++ with -stdlib=libc++.

http://pastebin.com/n9nCat15

Here's the output of each eith -### added (not the Xcode link step);

http://pastebin.com/kufBknaw

--
Rick

Rick Mann

unread,
Dec 29, 2012, 3:40:45 AM12/29/12
to John Criswell, llv...@cs.uiuc.edu

On Dec 27, 2012, at 10:24 , John Criswell <cris...@illinois.edu> wrote:

> It is possible (but a bit unlikely) that your problem is because you're not linking in libstdc++. Some C++ methods are defined in header files; that might explain why your simple program works but using libLLVMVMCore.a doesn't work.

So, you were right. Xcode was invoking the link with -stdlib=libc++, but should have either not been passing that at all, or using -stdlib=libstdc++ (the GNU standard library, and the default if no option is passed).

From the description in Xcode:

libstdc++: traditional C++ standard library that works with GCC and the LLVM Compiler (default).
libc++: highly optimized C++ standard library that works only with the LLVM Compiler, and is designed to support new C++11 features.

Which makes me wonder why the LLVM build doesn't use the better library (libc++). I'm assuming this is the root of the problem, that the LLVM build used libstdc++, instead of libc++.

Can anyone clarify what's going on? Is it possible to build LLVM with libc++? I guess to do that, it would have to be built by clang instead of GCC, huh? Can that be done?

--
Rick
Reply all
Reply to author
Forward
0 new messages