LLVM:“导出”班级
我想使用 LLVM 从我的程序中调用此代码:
#include <string>
#include <iostream>
extern "C" void hello() {
std::cout << "hello" << std::endl;
}
class Hello {
public:
Hello() {
std::cout <<"Hello::Hello()" << std::endl;
};
int hello() {
std::cout<< "Hello::hello()" << std::endl;
return 99;
};
};
我使用 clang++ -emit-llvm -c -o hello.bc hello.cpp 将此代码编译为 llvm 字节代码,然后我想从此程序中调用它:(
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/IRReader.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
assert(buff);
Module* m = getLazyBitcodeModule(buff, context, &error);
ExecutionEngine* engine = ExecutionEngine::create(m);
Function* func = m->getFunction(function);
vector<GenericValue> args(0);
engine->runFunction(func, args);
func = m->getFunction("Hello::Hello");
engine->runFunction(func, args);
}
int main() {
callFunction("hello.bc", "hello");
}
使用 g++ -g main.cpp 'llvm-config --cppflags --ldflags --libs core jit native bitreader'
编译)
我可以调用 hello( )
功能没有任何问题。 我的问题是:如何使用 LLVM 创建 Hello
类的新实例? 当我调用 Hello::Hello()
时遇到分段错误
感谢您的任何提示!
曼努埃尔
I'd like to call this code from my program using LLVM:
#include <string>
#include <iostream>
extern "C" void hello() {
std::cout << "hello" << std::endl;
}
class Hello {
public:
Hello() {
std::cout <<"Hello::Hello()" << std::endl;
};
int hello() {
std::cout<< "Hello::hello()" << std::endl;
return 99;
};
};
I compiled this code to llvm byte code using clang++ -emit-llvm -c -o hello.bc hello.cpp
and then I want to call it from this program:
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/IRReader.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
assert(buff);
Module* m = getLazyBitcodeModule(buff, context, &error);
ExecutionEngine* engine = ExecutionEngine::create(m);
Function* func = m->getFunction(function);
vector<GenericValue> args(0);
engine->runFunction(func, args);
func = m->getFunction("Hello::Hello");
engine->runFunction(func, args);
}
int main() {
callFunction("hello.bc", "hello");
}
(compiled using g++ -g main.cpp 'llvm-config --cppflags --ldflags --libs core jit native bitreader'
)
I can call the hello()
function without any problems.
My question is: how can I create a new instance of the Hello
class using LLVM?
I'm getting a segmentation fault when I call Hello::Hello()
Thanks for any hints!!
Manuel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在给定源上运行
clang++ -emit-llvm
不会发出 Hello::Hello,并且m->getFunction("Hello::Hello")
也不会即使它被发射了也能找到它。我猜它崩溃是因为func
为空。通常不建议尝试直接从 LLVM JIT 调用不是
extern "C"
的函数...我建议编写如下所示的包装器,并使用 clang 编译它(或使用clang API,取决于你在做什么):Running
clang++ -emit-llvm
on the given source won't emit Hello::Hello, andm->getFunction("Hello::Hello")
wouldn't find it even if it were emitted. I would guess it's crashing becausefunc
is null.Trying to directly call functions which aren't
extern "C"
from the LLVM JIT is generally not recommended... I'd suggest writing a wrapper like the following, and compiling it with clang (or using the clang API, depending on what you're doing):