尝试链接LLVM库时出现编译错误

时间:2012-01-22 17:17:37

标签: llvm llvm-gcc llvm-3.0

我试图在我的C ++代码中使用一些LLVM API,最终我得到链接器错误。我正在使用Apple MacOSX Lion。使用g ++进行编译。是CreateGlobalStringPtr抛出错误。这是LLVM 3.0。

以下是代码:

#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS

#include "llvm/Support/DataTypes.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
int main()
{
  llvm::LLVMContext & context = llvm::getGlobalContext();
  llvm::Module *module = new llvm::Module("top", context);
  llvm::IRBuilder<> builder(context);
  llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n");
}

g++ e.cpp /Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/libexec/gcc/i686-apple-darwin10/4.2.1/libllvmgcc.dylib 
Undefined symbols for architecture x86_64:
  "llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, llvm::ArrayRef<llvm::Value*>, bool)", referenced from:
      llvm::ConstantExpr::getInBoundsGetElementPtr(llvm::Constant*, llvm::ArrayRef<llvm::Value*>)in ccQDtIAW.o
  "llvm::GetElementPtrInst::getIndexedType(llvm::Type*, llvm::ArrayRef<llvm::Value*>)", referenced from:
      llvm::GetElementPtrInst::GetElementPtrInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, unsigned int, llvm::Twine const&, llvm::Instruction*)in ccQDtIAW.o
  "llvm::PointerType::get(llvm::Type*, unsigned int)", referenced from:
      llvm::GetElementPtrInst::GetElementPtrInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, unsigned int, llvm::Twine const&, llvm::Instruction*)in ccQDtIAW.o
  "llvm::Instruction::Instruction(llvm::Type*, unsigned int, llvm::Use*, unsigned int, llvm::Instruction*)", referenced from:
      llvm::GetElementPtrInst::GetElementPtrInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, unsigned int, llvm::Twine const&, llvm::Instruction*)in ccQDtIAW.o
  "llvm::GetElementPtrInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&)", referenced from:
      llvm::GetElementPtrInst::GetElementPtrInst(llvm::Value*, llvm::ArrayRef<llvm::Value*>, unsigned int, llvm::Twine const&, llvm::Instruction*)in ccQDtIAW.o
  "llvm::IRBuilderBase::CreateGlobalString(llvm::StringRef, llvm::Twine const&)", referenced from:
      llvm::IRBuilder<true, llvm::ConstantFolder, llvm::IRBuilderDefaultInserter<true> >::CreateGlobalStringPtr(llvm::StringRef, llvm::Twine const&)in ccQDtIAW.o
  "llvm::ConstantInt::get(llvm::IntegerType*, unsigned long long, bool)", referenced from:
      llvm::IRBuilder<true, llvm::ConstantFolder, llvm::IRBuilderDefaultInserter<true> >::CreateGlobalStringPtr(llvm::StringRef, llvm::Twine const&)in ccQDtIAW.o
ld: symbol(s) not found for architecture x86_64

1 个答案:

答案 0 :(得分:2)

为什么你要链接到一些随机的.dylib(肯定是从LLVM 2.9或者左右生成的,因为它是llvm-gcc的最后一个版本)?使用llvm-config派生编译器/链接器的正确标志。