简单动态库调用CMakeLists配置

1. 动态库生成

fun.h和fun.cpp

int my_add(int a, int b);
#include "fun.h"
int my_add(int a, int b)
{
    return a + b;
}

动态库生成的CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
set(CMAKE_DEBUG_POSTFIX "d")
project(funso)
set(SRC_ fun.h fun.cpp)
add_library(funso SHARED ${SRC_})

2. 外部调用函数

cmake_minimum_required(VERSION 3.14)
project(demo)

set(_SRC main.cpp)

include_directories(
    ${CMAKE_SOURCE_DIR}/include
)

set(DEPEND_LIB_DIR ${CMAKE_SOURCE_DIR}/linux/)
set(DEPEND_LIB libfunsod.so)

add_executable(${PROJECT_NAME} ${_SRC})

target_link_directories(${PROJECT_NAME} PUBLIC ${DEPEND_LIB_DIR})
target_link_libraries(${PROJECT_NAME} ${DEPEND_LIB})

你可能感兴趣的:(CMAKE,算法,数据结构)