您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

cmake 学习笔记

cmake 指令不分大小写,变量区分大小写。

message

管理文件:

  • file   file(GLOB <var>  "*.cpp" "*.h")    :CMakeFile命令之file_T的专栏-CSDN博客_cmake file
  • aux_source_directory  (无法添加头文件  不知道原因)
aux_source_directory(. SRC_SOURCES)
#set(sources
#	Main.cpp)
add_executable(cherno ${SRC_SOURCES})
#target_include_directories(cherno PUBLIC ${CMAKE_INSTALL_PREFIX}/GLFW/include)
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
set(GLFW_BIN ${PROJECT_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
target_include_directories(cherno PUBLIC ${GLFW_INCLUDE_DIR})
message("src GLFW_INCLUDE_DIR:" ${GLFW_INCLUDE_DIR})
target_link_libraries(cherno glfw)

这是一个系列的教程,很适合新手入门:《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX_Primeprime的专栏-CSDN博客 

原文:《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX - 52php - 博客园 (cnblogs.com)

一些常用变量:

  • CMAKE_INTSALL_PREFIX

cmake .. -DCMAKE_INSTALL_PREFIX='c:/Program Files (x86)'

添加源文件方式:target_include_directories()

  • 方式1:find_package() + target_include_directories()  需要glfw3Config.cmke(使用cmke+vs2019 编译glfw源码会生成该文件)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(src bin)

project(cherno)

#set GLFW. if you  have glfw3Config.cmake file config like this

find_package(glfw3 REQUIRED HINTS ${CMAKE_INSTALL_PREFIX}/GLFW/lib/cmake/glfw3)
if(NOT glfw3_FOUND)
	message(FATAL_ERROR "glfw3 not found, please use cmake -DCMAKE_INSTALL_PREFIX='c:\Program Files (x86)'")
endif()

#include_directories(${GLFW3_INCLUDE_DIRS})
#message("GLFW3_INCLUDE_DIRS:" ${GLFW3_INCLUDE_DIRS})

    src中CMakeLists.txt

set(sources
	Main.cpp)
add_executable(cherno ${sources})
target_include_directories(cherno PUBLIC ${CMAKE_INSTALL_PREFIX}/GLFW/include)
target_link_libraries(cherno glfw)
  • 方式2:target_include_directories()  官方推荐
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(src bin)

project(cherno)

     src中CMakeLists.txt

set(sources
	Main.cpp)
add_executable(cherno ${sources})
#target_include_directories(cherno PUBLIC ${CMAKE_INSTALL_PREFIX}/GLFW/include)
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
set(GLFW_BIN ${PROJECT_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
target_include_directories(cherno PUBLIC ${GLFW_INCLUDE_DIR})
message("src GLFW_INCLUDE_DIR:" ${GLFW_INCLUDE_DIR})
target_link_libraries(cherno glfw)
  • 方式3: include_directories() 本次没有试出来,之前有成功过 (有文章说是要在main函数之后? 使用include_directories ,分析可能原因时在下面的这个CmakeLists.txt文件是最上层的project,没有管理具体的main函数,在src文件夹中使用add_executable()或者add_library()的CMakeLists.txt文件当中再使用include_directories(), 由于时间关系不再尝试)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
add_subdirectory(src bin)

project(cherno)

#set GLFW. if you don't have a glfw3.cmake file config like this
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
set(GLFW_BIN ${PROJECT_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
message("1 GLFW_INCLUDE_DIR:" ${GLFW_INCLUDE_DIR})
message("2 PROJECT_SOURCE_DIR:" ${PROJECT_SOURCE_DIR})
message("3 CMAKE_SOURCE_DIR:" ${CMAKE_SOURCE_DIR})
message("4 CMAKE_MODULE_PATH:" ${CMAKE_MODULE_PATH})
message("5 CMAKE_INNSTALL_PREFIX:" ${CMAKE_INSTALL_PREFIX})

the inlcude_directories donot work, so I use target_include_directories in the CMakeLists.txt of src
include_directories(${GLFW_INCLUDE_DIR})
#link_directories(${GLFW_LIBRARY_DIR})

#set
#find_package(glfw3 REQUIRED HINTS ${CMAKE_INSTALL_PREFIX}/GLFW/lib/cmake/glfw3)
#if(NOT glfw3_FOUND)
#	message(FATAL_ERROR "glfw3 not found, please use cmake -DCMAKE_INSTALL_PREFIX='c:\Program Files (x86)'")
#endif()

#include_directories(${GLFW3_INCLUDE_DIRS})
#message("GLFW3_INCLUDE_DIRS:" ${GLFW3_INCLUDE_DIRS})


添加静态链接库: 

  • 添加  lib 方式1:将文件夹内所有lib文件定义到一个 变量当中GLFW_LIBS,然后使用target_link_libraries(${GLFW_LIBS}
aux_source_directory(. SRC_SOURCES)
#set(sources
#	Main.cpp)
add_executable(cherno ${SRC_SOURCES})
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBS ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019/*.lib)
target_include_directories(cherno PUBLIC ${GLFW_INCLUDE_DIR})
target_link_libraries(cherno ${GLFW_LIBS})
  • 添加  lib 方式2 :使用 target_link_directories() 添加lib文件所在的文件夹,然后使用 target_link_libraries() 单独添加所使用到的lib
aux_source_directory(. SRC_SOURCES)
add_executable(cherno ${SRC_SOURCES})
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
target_include_directories(cherno PUBLIC ${GLFW_INCLUDE_DIR})
target_link_directories(cherno PUBLIC ${GLFW_LIBRARY_DIR})
target_link_libraries(cherno glfw3.lib ) 
  • 添加 lib 方式3:link_directories() 不好用

添加动态链接库DLL 方式 : 编译成功后将DLL 文件放到exe 文件夹中

aux_source_directory(. SRC_SOURCES)
add_executable(cherno ${SRC_SOURCES})
set(GLFW_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/include)
set(GLFW_LIBRARY_DIR ${CMAKE_SOURCE_DIR}/Dependencies/GLFW/lib-vc2019)
target_include_directories(cherno PUBLIC ${GLFW_INCLUDE_DIR})
target_link_directories(cherno PUBLIC ${GLFW_LIBRARY_DIR})
target_link_libraries(cherno glfw3dll.lib ) 

添加lib方式4 

内部lib,在 target_link_libraries()中直接写lib name就OK,但是必须在同一个proj下:target_link_libraries(TARGET  libName)

 

sourc_group : 在 CMake 生成的 VS2015 工程中保持源码文件的目录组织_Right here, Code is Magic-CSDN博客 

file + source_group : [CMake笔记] CMake向解决方案添加源文件兼头文件 - dilex - 博客园 (cnblogs.com)

 

install  :【CMake】cmake的install指令_Yngz_Miao的博客-CSDN博客_cmake install

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进