SDL+花栗鼠CMake配置



我想通过CMake将花栗鼠2D物理框架与SDL链接起来。

我有以下项目结构:

MyProject
-chipmunk:
--include
--demo:
---CMakeList.txt
--src:
---CMakeList.txt
--CMakeList.txt
-src:
--main.cpp
-CMakeLists.txt

我读过关于静态和共享库的文章,决定使用静态库,所以我进入花栗鼠文件夹,运行cmake

1.第一个CMakeList文件设置选项:

message(STATUS "Set BUILD_STATIC option ON")
option(BUILD_SHARED "Build and install the shared library" ON)
option(BUILD_STATIC "Build as static library" ON)
option(INSTALL_STATIC "Install the static library" ON)

之后:

add_subdirectory(src)
  1. src中的CMakeList.txt进入操作:

    
    if(BUILD_STATIC)
    message(STATUS "BUILDING chipmunk_static")
    add_library(chipmunk_static STATIC ${chipmunk_source_files})
    set_target_properties(chipmunk_static PROPERTIES OUTPUT_NAME chipmunk)
    if(INSTALL_STATIC)
    message(STATUS "INSTALL chipmunk_static ${LIB_INSTALL_DIR}")
    install(TARGETS chipmunk_static ARCHIVE DESTINATION {LIB_INSTALL_DIR})
    endif(INSTALL_STATIC)
    endif(BUILD_STATIC)
    

  2. In the demo folder the CmakeFile does the follows:

    
    set(chipmunk_demos_libraries
    chipmunk_static
    ${GLEW_LIBRARIES}
    ${OPENGL_LIBRARIES}
    )
    

S0 my questions are:

  1. Do I need to run the Makefiles from the chipmunk libraries only once so I can build the static library?
  2. After I included the CMakefile from chipmunk in my Cmakefile it seems it cannot found the static library (I'm on Linux btw)
  3. If I have the static library built, can I delete all the src content from chipmunk and keep only the headers?

My attempt to find the chipmunk static librarywithout success:


add_subdirectory(chipmunk)
find_package(SDL2 REQUIRED)
find_library(CHIPMUNK_LIB chipmunk_static)
message(${CHIPMUNK_LIB})

So with CMake, when you "find" a library it looks for an installed one, not one built by a sub-project. So somewhere you should have a line where you reference the directory that has Chipmunk in it. In my project:

add_subdirectory(external/Chipmunk2D)

然后,当您构建可执行文件(或库,不管怎样(时,您可以按名称列出子项目构建的库。在我的案例中,glfw、花栗鼠_静态和enet都是由CMake在子项目中构建的:

target_link_libraries(my_executable
${OPENGL_LIBRARIES}
glfw
chipmunk_static
enet
)

最新更新