使用CMake在LLVM中使用多个源文件

时间:2016-04-11 00:31:24

标签: c++ cmake llvm

我正在尝试使用两个源文件创建一个LLVM Pass,但是当我编译时,cmake对我大喊:

[1/1] Linking CXX shared module lib/LLVMHydrogen.so
FAILED: : && /usr/bin/c++  -fPIC -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -std=c++11 -fPIC -g   -shared -Wl,-soname,LLVMHydrogen.so -o lib/LLVMHydrogen.so lib/Transforms/Hydrogen/CMakeFiles/LLVMHydrogen.dir/Hydrogen.cpp.o lib/Transforms/Hydrogen/CMakeFiles/LLVMHydrogen.dir/conditional_blocks.cpp.o  -Wl,-rpath,"\$ORIGIN/../lib" && :
/usr/bin/ld: lib/Transforms/Hydrogen/CMakeFiles/LLVMHydrogen.dir/conditional_blocks.cpp.o: relocation R_X86_64_PC32 against undefined symbol `_ZTVN12_GLOBAL__N_18HydrogenE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value

关键点是:

  

针对未定义的符号重新定位R_X86_64_PC32   `_ZTVN12_GLOBAL__N_18HydrogenE'在共享时不能使用   宾语;用-fPIC重新编译

我尝试将-fPIC添加到我的编译标志(和链接标志以获得良好的衡量标准),但没有任何帮助。这是我的CMakeLists.txt:

SET(COMPILE_FLAGS "-fPIC")
SET(LINK_FLAGS    "-lPIC")

SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}" )
SET( CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${LINK_FLAGS}" )

add_llvm_loadable_module( LLVMHydrogen
  Hydrogen.cpp
  conditional_blocks.cpp

  DEPENDS
  intrinsics_gen
)

但我得到了相同的输出。我究竟做错了什么?我觉得使用多个源文件应该非常简单:/

由于

源代码:

hydrogen.h:

#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"

using namespace std;
using namespace llvm;

namespace {
    struct Hydrogen : public FunctionPass {

    static char ID; // Pass identification, replacement for typeid
    Hydrogen() : FunctionPass(ID) {}

    bool runOnFunction(Function &f) override;
    void getAnalysisUsage(AnalysisUsage &AU) const override {
        AU.setPreservesAll();       
    }
    };
}

static const char h_name[] = "Hydrogen pass";
char Hydrogen::ID = 0;
static RegisterPass<Hydrogen> X("Hydrogen", "Hydrogen Pass");

hydrogen.cpp :(第一个&#39;主要&#39;文件)

#include "hydrogen.h"

bool Hydrogen::runOnFunction(Function &fun)
{
        return true;
}

conditional_blocks.cpp :(第二个文件)

#include "hydrogen.h"

1 个答案:

答案 0 :(得分:1)

将代码更改为以下内容解决了问题。我相信这是在标题中添加“虚拟”关键字,同时包含“virtual~H氢气(){}”

使用多个LLVM源文件的工作源:

相同的CMakeLists.txt已发布。

hydrogen.h:

#include ....

using namespace std;
using namespace llvm;

struct Hydrogen : public FunctionPass {

    static char ID; // Pass identification, replacement for typeid
    Hydrogen() : FunctionPass(ID) {}

    virtual ~Hydrogen() { }

    virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
        AU.setPreservesAll();       
    }

    virtual bool runOnFunction(Function &f) override;
};

hydrogen.cpp:

#include "hydrogen.h"

using namespace hydrogen_ns;

static const char h_name[] = "Hydrogen pass";
char Hydrogen::ID = 0;
static RegisterPass<Hydrogen> X("Hydrogen", "Hydrogen Pass");

bool Hydrogen::runOnFunction(Function &fun)
{
        return true;
}

conditional_blocks.cpp:

#include "hydrogen.h"