# CMakeList.txt: Sample01 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.8)
# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project ("Sample01")
set(PROJECT_SOURCE_DIR "C:/glfw-3.4/glfw-3.4")
# 指定要引用的dll的头文件所在路径
include_directories(${PROJECT_SOURCE_DIR}/include)
# 将源代码添加到此项目的可执行文件。
add_executable (Sample01 "Sample01.cpp" "Sample01.h")
# 获取当前二进制目录到RUNTIME_OUTPUT_DIRECTORY这个变量处,也可以讲 ${CMAKE_BINARY_DIR}替换为"/path/to/your/directory"
set_target_properties(Sample01 PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
# 找到OpenGL库
find_package(OpenGL REQUIRED)
# 获取运行时Directory
get_target_property(TARGET_DIR Sample01 RUNTIME_OUTPUT_DIRECTORY)
# 拷贝lib到运行时DirectoryExe同级目录
add_custom_command(TARGET Sample01 POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${PROJECT_SOURCE_DIR}/bin/Release/glfw3.lib
${TARGET_DIR})
# 这里需要链接库
target_link_libraries(Sample01 glfw3.lib)
# 链接OpenGL库
if(OPENGL_FOUND)
target_link_libraries(Sample01 ${OPENGL_gl_LIBRARY})
endif()
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET Sample01 PROPERTY CXX_STANDARD 20)
endif()
# TODO: 如有需要,请添加测试并安装目标。
标签:set,CMAKE,target,Sample01,学习,CMake,DIR,TARGET
From: https://www.cnblogs.com/jobshenlei/p/18514685