使用 Cmake 构建 asmJit 示例
Buliding asmJit Example with Cmake
我正在尝试构建一个基于 asmJit 的示例项目。
我有以下设置
AsmTest
- main.cpp
- 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)
我可以成功构建这个项目,得到一个 visual studio 解决方案。
但是,如果我尝试在 visual studio 秒内 运行 它,我会得到各种 "unresolved external errors" ,例如这样。
1 error LNK2001: unresolved external symbol "struct asmjit::X86RegData
const asmjit::x86RegData" (?x86RegData@asmjit@@3UX86RegData@1@B) main.obj JitTest
我不明白为什么会出现链接错误。
我是 cmake 的新手,这整个过程都是从零开始的。
有人能给我指出正确的方向吗?
您可以将 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
放入静态库构建中,但这需要更长时间的讨论,并且与您的问题无关。
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)
我正在尝试构建一个基于 asmJit 的示例项目。
我有以下设置 AsmTest
- main.cpp
- 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)
我可以成功构建这个项目,得到一个 visual studio 解决方案。 但是,如果我尝试在 visual studio 秒内 运行 它,我会得到各种 "unresolved external errors" ,例如这样。
1 error LNK2001: unresolved external symbol "struct asmjit::X86RegData
const asmjit::x86RegData" (?x86RegData@asmjit@@3UX86RegData@1@B) main.obj JitTest
我不明白为什么会出现链接错误。 我是 cmake 的新手,这整个过程都是从零开始的。 有人能给我指出正确的方向吗?
您可以将 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
放入静态库构建中,但这需要更长时间的讨论,并且与您的问题无关。
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)