c++ 使用asmjit
1. 下载源码
在github下载源码放在"desctop/asmjit-master"
2. 使用cmake生成构建系统
> Desktop\asmjit-master> cmake CMakeLists.txt
3. 使用vs2019编译
使用vs2019打开"asmjit.sln",使用release x64打包
打包后会将"asmjit.dll"和"asmjit.lib"输出到"desctop/asmjit-master/Release"
4. 在项目中使用asmjit
- 设置"asmjit"头文件位置,通常是"Desktop\asmjit-master\src"
- 设置"asmjit.lib"位置
- 设置"asmjit.dll"位置
5. 拷贝官网demo到main.cpp,然后运行
MessageBoxA Example
#include <iostream>
#include <Windows.h>
#include <asmjit/asmjit.h>
using namespace asmjit;
typedef int (*Func)(void);
int main(int argc, char* argv[]) {
JitRuntime rt;
CodeHolder code;
code.init(rt.environment());
x86::Assembler a(&code);
a.push(x86::rbp);
a.mov(x86::rbp, x86::rsp);
a.sub(x86::rsp, 32);
a.mov(x86::rcx, 0);
a.mov(x86::rdx, "body");
a.mov(x86::r8, "title");
a.mov(x86::r9, 3);
a.call(MessageBoxA);
a.add(x86::rsp, 32);
a.mov(x86::rsp, x86::rbp);
a.pop(x86::rbp);
a.ret();
Func fn;
Error err = rt.add(&fn, &code);
if (err) return 1;
printf("%d\n", fn());
rt.release(fn);
return 0;
}
如果你要同时编译asmjit和asmtk,将两个放在同级目录,asmtk的cmakelist会去"../asmjit/"找asmjit的cmakelist