开始标配
# 指定编译的最小版本
cmake_minimum_required(VERSION 3.0.0)
# 指定解决方案的名字 和版本
project(<SOLUTION_NAME> VERSION 0.1.0)
# 设置c++标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 设置c++17
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17")
else() # gcc clang
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
endif(MSVC)
# 设置Release or Debug 默认值
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
# 生成pdb调试信息
IF(MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
ENDIF()
# 设置MacOS 平台目标
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.10)
# 增加子目录
add_subdirectory(<子目录路径>)
# 从文件夹从获取文件的决定路径
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.ui")
# 添加可执行项目
add_executable(<target> ${SOURCE_FILES})
# 添加动态库
add_library(<target> SHARED ${SOURCE_FILES})
#添加编译选项(下面是添加以utf-8解码,避免中文跨平台乱码)
if(MSVC)
target_compile_options(13_6 PUBLIC "$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
endif(MSVC)
#设置vs为宽字符
target_compile_definitions(<target> PRIVATE UNICODE _UNICODE)
# 添加包含目录
target_include_directories(<target> PUBLIC
"${PROJECT_BINARY_DIR}"
${EXTRA_INCLUDES}
)
# 添加库目录
target_link_directories(<target> PUBLIC ${CMAKE_SOURCE_DIR}/sdk/win/lib)
# 添加库文件
target_link_libraries(<target> PRIVATE
CURL::libcurl # 外部库,通过find_package()等命令搜索到的
findall polarssl # 外部库,可以在库目录添加库搜索路径,这样就不用给出全路径,文件名后缀也不用
debug ${PYTHON_LIBRARY}/python37_d
optimized ${PYTHON_LIBRARY}/python37
)
############################################
# 这里,常见的依赖库可能是以下几种情况:
# 1.在此次编译的工程里添加的目标,给出目标名;
# 2.外部库,给出路径和库文件全名;
# 3.外部库,通过find_package()等命令搜索到的
##########################################
# 添加依赖
ADD_DEPENDENCIES(main a.so b.so)
# 1.设置可执行文件的输出路径:
message(status ____________"${CMAKE_CURRENT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}")
SET(EXECUTABLE_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE}")
# 设置动态库输出路径
SET(LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH})
# 设置调试目录及相关功能
set_target_properties(${LIBWVDOWNLOADER} PROPERTIES
VS_DEBUGGER_COMMAND "D:/project/local/netflix/test_ffi-napi/js/node_modules/electron/dist/electron.exe"
VS_DEBUGGER_COMMAND_ARGUMENTS "."
VS_DEBUGGER_WORKING_DIRECTORY D:/project/local/netflix/test_ffi-napi/js
)
# 打印所有变量
function(print_directory_variables dir)
# Dump variables:
get_property(_variableNames DIRECTORY ${dir} PROPERTY VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
get_directory_property(_variableValue DIRECTORY ${dir} DEFINITION ${_variableName})
message(STATUS "DIR ${dir}: ${_variableName}=${_variableValue}")
endforeach()
endfunction(print_directory_variables)
# for example
print_directory_variables(.)
# 添加生成后事件
add_custom_command(
TARGET hitpawInstaloader POST_BUILD
COMMAND echo "cp ${LIBPYTHON} ${EXECUTABLE_OUTPUT_PATH}"
COMMAND cp "${LIBPYTHON}" "${EXECUTABLE_OUTPUT_PATH}"
)
qt简单模板
cmake_minimum_required(VERSION 3.1.0)
project(sample_4_5 VERSION 1.0.0 LANGUAGES CXX) //注意LANGUAGES指定了CXX,就不能用c语法了,编译器会把函数名称按照c++的方式粉碎,这样就找不到c语法函数的符号了,即使用了extern "C"也没有,解决办法是不指定语言,因为默认就是c++和c都支持的,或者填 CXX AND C也行
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Widgets REQUIRED)
set(HEADERS
Sample_4_5.h
)
set(SOURCES
main.cpp
Sample_4_5.cpp
)
set(RESOURCES
Sample_4_5.qrc
)
set(UI
Sample_4_5.ui
)
add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES} ${RESOURCES} ${UI})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)