简单的cmake demo
一个简单的cmake 工程,包括生成动态库和链接动态库的demo工程和test工程。
demo下载链接: https://download.csdn.net/download/sinat_35178307/87243966
目录结构
该工程可以生成一个dll,一个调用dll的demo,一个test dll接口的工程。
- build :cmake生成目录,过程文件保存在该目录下,output保存在build/build目录下
- include:包含dll需要 头文件
- src: 包含生成dll的源码文件
- test: 包含测试dll的main.cpp和cmake文件
- demo:包含调用dll的main.cpp和cmake文件
最顶层cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.7.0)
# project name
project(cmakeDemo)
message(STATUS "----------------------------------------------------------")
message(STATUS "PROJECT_SOURCE_DIR = " ${PROJECT_SOURCE_DIR})
set(BINARY_DIR ${PROJECT_SOURCE_DIR}/build/build/bin/)
set(LIBRARY_DIR ${PROJECT_SOURCE_DIR}/build/build/lib)
# set bin dir
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BINARY_DIR})
#set lib dir
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${LIBRARY_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${LIBRARY_DIR})
message(STATUS "BINARY_DIR = " ${BINARY_DIR})
message(STATUS "LIBRARY_DIR = " ${LIBRARY_DIR})
set(INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/include)
message(STATUS "INCLUDE_DIRS = " ${INCLUDE_DIRS})
add_subdirectory(demo)
add_subdirectory(src/dll_src)
add_subdirectory(test/test_dll)
include 文件夹
该目录下存放生成动态库的头文件
src文件夹
该目录下存放生成动态库的源码文件和cmake文件,该cmake文件会最终生成hello.dll
add_definitions(-DLICENSEVERIFY_EXPORTS)
set(HEADERS ${INCLUDE_DIRS}/hello.h )
add_library (hello SHARED ${HEADERS} hello.cpp )
target_compile_definitions(hello PRIVATE WIN_EXPORTS)
target_include_directories(hello PRIVATE ${INCLUDE_DIRS})
test文件夹
该目录下存放生成test_dll工程的源码文件和cmake文件,该cmake文件中会链接hello.dll文件
add_executable(test_dll test_dll.cpp)
target_include_directories(test_dll PRIVATE ${INCLUDE_DIRS})
target_link_libraries(test_dll hello)
add_dependencies(test_dll hello)
demo
和test目录下的文件类似,生成helloDemo.exe
#./demo/CMakeLists.txt
add_executable(HelloDemo main.cpp )
target_include_directories(HelloDemo PRIVATE ${INCLUDE_DIRS})
target_link_libraries(HelloDemo LicenseVerify)
add_dependencies(HelloDemo LicenseVerify )