使用 Cmake 构建 asmJit 示例

PIN*_*INK 2 c++ cmake visual-studio

我正在尝试构建一个基于 asmJit 的示例项目。

我有以下设置 AsmTest

  • 主程序
  • CmakeLists.txt
    • asmjit(里面是来自 github 存储库的 CmakeList.txt)。
    • CMakeLists.txt(内容:add_subdirectory(asmjit))
  • 建造

我的第一个CmakeLists.txt的内容是:

cmake_minimum_required(VERSION 2.8)
project(asmJitTest)

add_subdirectory(libs)
include_directories(${asmJitTest_SOURCE_DIR} ${asmJitTest_SOURCE_DIR}/libs/asmjit/src)
add_executable(JitTest main.cpp)
target_link_libraries(JitTest asmjit)
Run Code Online (Sandbox Code Playgroud)

我可以成功构建这个项目,获得视觉工作室解决方案。但是,如果我尝试在视觉工作室中运行它,我会收到各种“未解决的外部错误”,例如这样。

1   error LNK2001: unresolved external symbol "struct asmjit::X86RegData   
    const asmjit::x86RegData" (?x86RegData@asmjit@@3UX86RegData@1@B)    main.obj    JitTest
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会出现链接错误。我是 cmake 的新手,这整个过程是从头开始的。有人可以指出我正确的方向吗?

Pet*_*etr 6

您可以将 asmjit 编译为动态链接库,只需将其包含CMakeLists.txt在您的 cmake 脚本中即可:

Set(ASMJIT_DIR "/relative/dir/to/your/asmjit")
Include("${ASMJIT_DIR}/CMakeLists.txt")

# AsmJit should have already taken care of include directories, if you
# are not sure you can add it, but shouldn't be necessary.
Include_Directories(${ASMJIT_DIR})

# Then in your target you should be able to use:
Target_Link_Libraries(YourTarget asmjit ${ASMJIT_DEPS})
Run Code Online (Sandbox Code Playgroud)

或者,我发现这对于将 asmjit 作为静态库嵌入更可靠,将整个 asmjit 嵌入到您的项目中。AsmJit 对此有内置支持:

# Tell asmjit that it will be embedded.
Set(ASMJIT_EMBED TRUE)
Add_Definitions(-DASMJIT_STATIC)

Set(ASMJIT_DIR "/relative/dir/to/your/asmjit")
Include("${ASMJIT_DIR}/CMakeLists.txt")

# If you add a library / executable, include asmjit sources.
Add_Executable(YourTarget main.cpp ${ASMJIT_SRC})

# You still have to include asmjit dependencies.
Target_Link_Libraries(YourTarget ${ASMJIT_DEPS})
Run Code Online (Sandbox Code Playgroud)

与动态或静态构建 asmjit 相比,第二种方法有一个很大的优势,即它可以通过这种方式嵌入到动态链接库中,而不会在 Linux 和所有需要使用的平台下出现问题-fPIC,因为默认情况下 cmake 不会将其放入-fPIC静态库构建,但这将需要更长的讨论,并且与您的问题并不真正相关。