我正在尝试构建一个基于 asmJit 的示例项目。
我有以下设置 Asm测试
- main.cpp
- CmakeLists.txt
- 图书馆
- asmjit(里面是来自 github 仓库的 CmakeList.txt)。
- CMakeLists.txt(内容:add_subdirectory(asmjit))
- build
我的第一个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 错误”,例如。
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
放入静态库构建中,但这需要更长时间的讨论,并且与您的问题无关。
关于c++ - 使用 Cmake 构建 asmJit 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28409070/