跳转至

CMake引用MeshLib

Windows

目录结构

CMakeLists.txt
thirdparty\
    expected\
        include\
            tl\
                expected.hpp
        Findexpected.cmake
    meshlib\
        ...
        Findmeshlib.cmake

MeshLib

一、下载预编译版本,放到meshlib目录下

https://github.com/MeshInspector/MeshLib/releases

二、编写Findmeshlib.cmake

if(TARGET MeshLib)
    return()
endif()

IF (WIN32)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set(MeshLib_Lib ${CMAKE_CURRENT_LIST_DIR}/lib/Release/*.lib)
    else()
        message(FATAL_ERROR "Todo: Win32")
    endif()
ELSEIF(APPLE)
    message(FATAL_ERROR "Todo: Apple")
ELSEIF(UNIX)
    message(FATAL_ERROR "Todo: UNIX")
ENDIF()

add_library(MeshLib SHARED IMPORTED)
set_property(
    TARGET MeshLib 
    PROPERTY 
        IMPORTED_IMPLIB ${MeshLib_Lib}
) 
target_include_directories(
    MeshLib
    INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include
)

expected

一、下载源码,取include目录即可,放到expected目录下

TartanLlama/expected: C++11/14/17 std::expected with functional-style extensions (github.com)

二、编写Findexpected.cmake文件

if(TARGET expected)
    return()
endif()

add_library(expected INTERFACE)
target_include_directories(expected INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

使用

CMakeLists.txt文件

cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project(cmake-example)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 搜索目录
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/thirdparty/expected")
set(BOOST_ROOT "D:/install/boost_1_71_0")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/thirdparty/meshlib")

find_package(expected)
find_package(Boost COMPONENTS REQUIRED)
find_package(meshlib)

file(GLOB SrcFiles "*.cpp")
add_executable(${PROJECT_NAME} ${SrcFiles})
target_link_libraries(${PROJECT_NAME} Boost::boost expected MeshLib)

add_compile_definitions(DATA_PATH="${CMAKE_SOURCE_DIR}/data/")