Modern CMake 简体中文版

│  CMakeLists.txt
│  triangle.cpp
│
├─include
│  │  vmath.h
│  │
│  ├─glad
│  │      glad.h
│  │
│  ├─GLFW
│  │      glfw3.h
│  │      glfw3native.h
│  │
│  └─KHR
│          khrplatform.h
│
└─lib
    │  glad.c
    │
    └─GLFW
            glfw3.dll
            libglfw3.a
            libglfw3dll.a

用CMake编译跨shell平台cpp代码

CMake 生成,需要显式指定生成器为*“MinGW Makefiles”,避免Windows平台MSVC优先级使CMakelists指定gcc/g++*失效

cmake -G "MinGW Makefiles" -S . -B buil

CMake install

cmake --build build

run exe

./build/*.exe

CMakelists

cmake_minimum_required(VERSION 3.10)

# 设置C语言编译器
set(CMAKE_C_COMPILER "C:/msys64/ucrt64/bin/gcc.exe")

# 设置C++语言编译器
set(CMAKE_CXX_COMPILER "C:/msys64/ucrt64/bin/g++.exe")

project(TriangleProject)

# 添加可执行文件
add_executable(triangle triangle.cpp lib/glad.c)

# 包含头文件目录
target_include_directories(triangle PRIVATE include)

# 添加链接库文件
# 需要${CMAKE_SOURCE_DIR}指定路径
target_link_libraries(triangle PRIVATE ${CMAKE_SOURCE_DIR}lib/GLFW/libglfw3.a ${CMAKE_SOURCE_DIR}/lib/GLFW/libglfw3dll.a )