modern c++ 教程总结(imgui+glfw+opengl+glad)

cmake_minimum_required(VERSION 3.17)
project(dear-imgui-template)

set(CMAKE_CXX_STANDARD 20)
set(LIBRARIES_DIR lib)

add_executable(dear-imgui-template
        vendor/imgui_impl_glfw.h
        vendor/imgui_impl_glfw.cpp
        vendor/imgui_impl_opengl3.h
        vendor/imgui_impl_opengl3.cpp
        src/main.cpp)

# glad
set(GLAD_DIR "${LIBRARIES_DIR}/glad")
add_library("glad" "${GLAD_DIR}/src/glad.c")
target_include_directories("glad" PRIVATE "${GLAD_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLAD_DIR}/include")
target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}")

# glfw
set(GLFW_DIR "${LIBRARIES_DIR}/glfw")
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
add_subdirectory("${GLFW_DIR}")
target_link_libraries(${PROJECT_NAME} "glfw" "${GLFW_LIBRARIES}"  "${CMAKE_DL_LIBS}")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLFW_DIR}/include")
target_compile_definitions(${PROJECT_NAME} PRIVATE "GLFW_INCLUDE_NONE")

# imgui
set(IMGUI_DIR "${LIBRARIES_DIR}/imgui")
add_library("imgui"
        "${IMGUI_DIR}/src/imgui.cpp"
        "${IMGUI_DIR}/src/imgui_draw.cpp"
        "${IMGUI_DIR}/src/imgui_demo.cpp"
        "${IMGUI_DIR}/src/imgui_widgets.cpp")
target_include_directories("imgui" PRIVATE "${IMGUI_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "${IMGUI_DIR}/include")
target_link_libraries(${PROJECT_NAME} "imgui" "${CMAKE_DL_LIBS}")

modern c++ 教程总结(imgui+glfw+opengl+glad)_第1张图片

 以上述项目为例子

在c++ 中如果要集成第三方库,一般分两种情况,一种是库中不含有CMakeList.txt  一种是库中含有CMakeList.txt

1.对于不含有CMakeList.txt的情况,使用add_Library("自定义库名"  "库文件")函数自定义一个库,使用两个target_include_directories("${PROJECT_NAME}",PRIVATE,"INCLUDE_DIR")函数,一个是库包含include头文件,一个是当前项目链接头文件,保证库本身以及项目本身都能访问到头文件,其中PRIVATE的作用是只能让当前的项目的文件能访问到头文件

2.对于含有CMakeList.txt的情况,先试用add_subdirectories()函数,现将含有CMakeList.txt的目录加入本项目,然后使用target_link_libraries("${Project_Name}" "库名" "${CMAKE_DL_LIBS}"),其中${CMAKE_DL_LIBS}的目的是在需要动态加载库的情况下,将适当的库链接到你的项目中。如果你的项目不需要动态加载库,可以忽略${CMAKE_DL_LIBS},每次都可将"${CMAKE_DL_LIBS}加在后面,以防万一。有一点需要注意,在含有CMakeList.txt的情况,一般在target_link_libraries之后在使用target_include_direcories()添加头文件目录。

3.message()的使用,message的参数必须加双引号,双引号里面的内容会自动解析${}里面的参数变量。

4.target_compile_definitions(${PROJECT_NAME} PRIVATE "BOOST_TIMER_ENABLE_DEPREACATED") 定义一个cmake的变量

你可能感兴趣的:(c++,开发语言)