Cycling74 / min-devkit

Tools, documentation, and reference implementation of a Max Package built using the Min-API.
MIT License
156 stars 30 forks source link

Guidance on using an external library in a min project #208

Open isabelgk opened 1 year ago

isabelgk commented 1 year ago

I would certainly appreciate some better (or simpler) guidance on how to use CMake to link an external library (on an arbitrary path) with a min project.

I don't understand CMake, and trying to link my project to an arbitrary .lib file already compiled from a solution is doing my head in. I have managed the include file, but getting the subsequent project to link has proved impossible.

Originally posted by @scblakely in https://github.com/Cycling74/min-devkit/issues/204#issuecomment-1437506468

isabelgk commented 1 year ago

For now, how I'd do it something like this in your external's CMakeLists.txt. I haven't tested this exact set up, but I've done it similarly to this in the past.

cmake_minimum_required(VERSION 3.0)

set(C74_MIN_API_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../min-api)
include(${C74_MIN_API_DIR}/script/min-pretarget.cmake)

include_directories( 
    "${C74_INCLUDES}"
)

set( SOURCE_FILES
    ${PROJECT_NAME}.cpp
)

add_library( 
    ${PROJECT_NAME} 
    MODULE
    ${SOURCE_FILES}
)

#   ====== see this section
#  /
# v
if (MSVC)
    # include any headers needed by your library that's kept at <repository root>/libs/include,
    # and you can change for your situation
    include_directories("${CMAKE_SOURCE_DIR}/libs/include")

    # this links to a library that's kept at <repository root>/libs/foo.lib, and you can change for your situation
    target_link_libraries(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/libs/foo.lib")
endif()

include(${C74_MIN_API_DIR}/script/min-posttarget.cmake)