使用Cmake构建asmJit示例

时间:2015-02-09 11:48:30

标签: c++ visual-studio cmake

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

我有以下设置 AsmTest

  • 的main.cpp
  • 的CMakeLists.txt
    • asmjit(里面是github repo的CmakeList.txt)。
    • CMakeLists.txt(content: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)

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

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

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

2 个答案:

答案 0 :(得分:4)

您可以将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})

或者,我发现将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})

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

答案 1 :(得分:0)

asmjit 的 CMakeList.txt 包含以下代码:

set(ASMJIT_DIR        "${CMAKE_CURRENT_LIST_DIR}" CACHE PATH "Location of 'asmjit'")
set(ASMJIT_TEST       ${ASMJIT_TEST}              CACHE BOOL "Build 'asmjit' test applications")
set(ASMJIT_EMBED      ${ASMJIT_EMBED}             CACHE BOOL "Embed 'asmjit' library (no targets)")
set(ASMJIT_STATIC     ${ASMJIT_STATIC}            CACHE BOOL "Build 'asmjit' library as static")
set(ASMJIT_SANITIZE   ${ASMJIT_SANITIZE}          CACHE STRING "Build with sanitizers: 'address', 'undefined', etc...")
set(ASMJIT_BUILD_X86  ${ASMJIT_BUILD_X86}         CACHE BOOL "Build X86 backends (X86 and X86_64)")

此信息将允许您在自己的 CMakeList.txt 中执行以下操作

set(ASMJIT_STATIC TRUE)
add_subdirectory(asmjit)
target_link_libraries(${PROJECT_NAME} asmjit::asmjit)