makefilecmakesdlchipmunk

SDL + Chipmunk CMake config


I want to link the Chipmunk2D physics framework with SDL via CMake.

I have the following project structure:

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

I read about static and shared libraries, and decide I want to use a static library, so I went in the chipmunk folder and I ran cmake .

1.The first CMakeList file sets the options:

  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)

and after that:

add_subdirectory(src)
  1. The CMakeList.txt from src enters the action:

    
    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 library without success:


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


Solution

  • 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)
    

    Then when you are building your executable (or library, whatever), you can just list libraries built by sub-projects by name. In my case, glfw, chipmunk_static, and enet are all built by CMake in sub-projects:

    target_link_libraries(my_executable
        ${OPENGL_LIBRARIES}
        glfw
        chipmunk_static
        enet
    )