编程与调试 -- CMake CMakeLists.txt 笔记 Notes

CMake 备忘录

$<$:Release> $<$:Debug>

这是一个 CMake generator expression。 像 $<…>这样的表达式是在 CMake 2.8 中引入的 generator exressions。这些表达式的主要特征是它们在构建时进行评估,而不是在配置时进行评估,就像正常的 CMake 变量一样。 https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html

Debug & Relase 加载不同的 lib。

set(ADDITIONAL_LIBRARY_DEPENDENCIES
    "$<$<CONFIG:Debug>:"
        "${OPENCV_DIR}/lib/Debug/opencv_core453d.lib"
        "${OPENCV_DIR}/lib/Debug/opencv_imgproc453d.lib"
        "${OPENCV_DIR}/lib/Debug/opencv_highgui453d.lib"
        "${OPENCV_DIR}/lib/Debug/opencv_imgcodecs453d.lib"
    ">"
    "$<$<NOT:$<CONFIG:Debug>>:"
        "${OPENCV_DIR}/lib/Release/opencv_core453.lib"
        "${OPENCV_DIR}/lib/Release/opencv_imgproc453.lib"
        "${OPENCV_DIR}/lib/Release/opencv_highgui453.lib"
        "${OPENCV_DIR}/lib/Release/opencv_imgcodecs453.lib"
    ">"
)

target_link_libraries(resizer "${ADDITIONAL_LIBRARY_DEPENDENCIES}")

Debug & Relase 拷贝不同的 dll。

SET(RUNDIR
    "$<$<CONFIG:Debug>:" "${PROJECT_BINARY_DIR}/Debug" ">"
    "$<$<NOT:$<CONFIG:Debug>>:" "${PROJECT_BINARY_DIR}/Release" ">"
)
string(REPLACE ";" "" RUNDIR ${RUNDIR})
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
        ${OPENCL_DIR}/bin/OpenCL.dll
        ${RUNDIR}/OpenCL.dll)

区分 Win 32x64

IF (CMAKE_CL_64)
set(OpenCL_INCLUDE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x64/OpenCL-SDK-2022.4.1-win32/include)
set(OpenCL_LIBRARY
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x64/OpenCL-SDK-2022.4.1-win32/lib/OpenCL.lib)
ELSE (CMAKE_CL_64)
set(OpenCL_INCLUDE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x86/OpenCL-SDK-2022.4.1-win32/include)
set(OpenCL_LIBRARY
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x86/OpenCL-SDK-2022.4.1-win32/lib/OpenCL.lib)
ENDIF (CMAKE_CL_64)

Build Boost.Python & Numpy in Windows

本机环境不一样,一般都需要自己编译出来。 Build Boost.Python & Numpy in Windows Building Boost Python

boost_1_78_0-msvc-14.2-64.exe boost_1_78_0-msvc-14.2-32.exe cmake-3.22.2-windows-i386.zip

.\bootstrap.bat vc142
.\b2

user-config.jam

# Boost.Build Configuration
# Automatically generated by bootstrap.bat
using python : 3.8 : C:\\Python\\Python38\\python.exe
   : C:\\Python\\Python38\\include # directory that contains pyconfig.h
   : C:\\Python\\Python38\\libs    # directory that contains python38.lib
   ;

import option ;

using msvc : 14.2 ;

# option.set keep-going : false ;
b2 --with-python --prefix=D:\\local\\boost_1_78_0 --user-config=user-config.jam address-model=64 variant=release link=static threading=multi runtime-link=shared install
b2 --with-python --prefix=D:\\local\\boost_1_78_0 --user-config=user-config.jam address-model=64 variant=release link=static threading=multi runtime-link=shared install -debug-configuration -d0

编译出来后:

D:\local\boost_1_78_0\lib\libboost_python38-vc142-mt-x64-1_78.lib

使用:

# cmake .. -G "Visual Studio 16 2019" -A Win32
# D:\local\cmake-3.22.2-windows-i386\bin\cmake.exe .. -G "Visual Studio 16 2019"
set(BOOST_ROOT "D:/local/boost_1_78_0")

# find boost
#
# set(Boost_USE_STATIC_LIBS ON)
# set(Boost_USE_MULTITHREADED ON)
# set(Boost_USE_STATIC_RUNTIME ON)
FIND_PACKAGE(Boost COMPONENTS python REQUIRED)

Boost _Boost_COMPONENT_DEPENDENCIES

# https://cmake.org/cmake/help/latest/module/FindBoost.html
set(Boost_USE_STATIC_LIBS        ON)  # only find static libs
set(Boost_USE_DEBUG_LIBS        OFF)  # ignore debug libs and
set(Boost_USE_RELEASE_LIBS       ON)  # only find release libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
set(BOOST_ROOT D:/local/boost_1_78_0)
FIND_PACKAGE(Boost COMPONENTS python REQUIRED)

报错 note

CMake Warning at C:/Program Files/CMake/share/cmake-3.15/Modules/FindBoost.cmake:1125 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.15/Modules/FindBoost.cmake:1247 (_Boost_COMPONENT_DEPENDENCIES)
  C:/Program Files/CMake/share/cmake-3.15/Modules/FindBoost.cmake:1885 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:35 (find_package)

解决:

Boost 1.63 requires CMake 3.7 or newer.
Boost 1.64 requires CMake 3.8 or newer.
Boost 1.65 and 1.65.1 require CMake 3.9.3 or newer.
Boost 1.66 requires CMake 3.11 or newer.
Boost 1.67 requires CMake 3.12 or newer.
Boost 1.68, 1.69 require CMake 3.13 or newer.
Boost 1.70 requires CMake 3.14 or newer.
Boost 1.71 requires CMake 3.15.3 or newer.
Boost 1.72 requires CMake 3.16.2 or newer.
Boost 1.73 requires CMake 3.17.2 or newer.
Boost 1.74 requires CMake 3.19 or newer.
Boost 1.75 requires CMake 3.19.5 or newer.
Boost 1.76 requires CMake 3.20.3 or newer.
Boost 1.77 requires CMake 3.21.3 or newer.
Boost 1.78 requires CMake 3.22.2 or newer.

Python config failure: Python is 64-bit, chosen compiler is 32-bit

10>CMake Error at libs/numpy/cmake/FindPythonLibsNew.cmake:109 (message):
10>  Python config failure: Python is 64-bit, chosen compiler is 32-bit
10>Call Stack (most recent call first):
10>  CMakeLists.txt:26 (find_package)
12>Performing build step for 'png'
10>
10>
10>-- Configuring incomplete, errors occurred!

git@github.com:hawkhai/OpenCL_img_resize.git

cmake_minimum_required(
  VERSION 3.7
)

project(rotation
  LANGUAGES CXX
)

set(CMAKE_MODULE_PATH
  ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules
)

set(OpenCL_INCLUDE_DIR
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x86/OpenCL-SDK-2022.4.1-win32/include)
set(OpenCL_LIBRARY
    ${CMAKE_CURRENT_SOURCE_DIR}/../OpenCL-SDK-v2022.04.01-Win-x86/OpenCL-SDK-2022.4.1-win32/lib/OpenCL.lib)

find_package(OpenCL REQUIRED)
find_package(Threads REQUIRED)

set(Sources rotation.cpp rot_res_gam.cl)

add_executable(${PROJECT_NAME}
  ${Sources}
)

target_compile_features(${PROJECT_NAME}
  PRIVATE
    cxx_std_17
)

set_target_properties(${PROJECT_NAME}
  PROPERTIES
    CXX_EXTENSIONS OFF
)

target_link_libraries(${PROJECT_NAME}
  PRIVATE
    OpenCL::OpenCL
    Threads::Threads
)

target_compile_definitions(${PROJECT_NAME}
  PRIVATE
    CL_HPP_MINIMUM_OPENCL_VERSION=120
    CL_HPP_TARGET_OPENCL_VERSION=120
    CL_HPP_ENABLE_EXCEPTIONS
)

source_group("Sources" FILES ${Files_SRCS})

git@github.com:hawkhai/opencl-resizer.git

cmake_minimum_required(VERSION 3.14)
project(resizer)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )

include_directories(.)
include_directories(OpenCL)
include_directories(OpenCL/kernels)

#cmake .. -G "Visual Studio 16 2019" -A Win32
if (APPLE)
  include_directories(/opt/libjpeg-turbo/include)
  include_directories(/usr/local/Frameworks/opencv2.framework)
endif (APPLE)

SET(OPENCV_DIR
  "E:/kpdf/pdfreader_story/image/opencv-4.5.3"
)
SET(OPENCL_DIR
  "E:/kSource/pythonx/shader/opengl-3rd/OpenCL-SDK-v2022.04.01-Win-x86"
)
SET(JPEG_DIR
  "E:/kSource/pythonx/shader/opengl-3rd/libjpeg-turbo"
)
SET(CUDA_DIR
  "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0"
)

#windows specific
if (WIN32)
  include_directories("${JPEG_DIR}/include")
  #include_directories("${CUDA_DIR}/include")
  include_directories("${OPENCV_DIR}/modules/core/include")
  include_directories("${OPENCV_DIR}/modules/imgproc/include")
  include_directories("${OPENCV_DIR}/modules/highgui/include")
  include_directories("${OPENCV_DIR}/modules/imgcodecs/include")
  include_directories("${OPENCV_DIR}/build")
  include_directories("${OPENCL_DIR}/include")
endif (WIN32)

#https://www.cnblogs.com/zjutzz/p/13340318.html
#https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
#set_property(TARGET resizer PROPERTY
#    MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
#set_target_properties(resizer PROPERTIES
#    CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
#)

add_executable(resizer
    OpenCL/oclManager.cpp
    OpenCL/oclManager.h
    Resizer.cpp
    Resizer.h
    Image.cpp
    Image.h
    IO.cpp
    IO.h
    JPEGImage.cpp
    JPEGImage.h
    main.cpp
    Profiler.h
    Utils.cpp
    Utils.h)

if (APPLE)
  target_link_libraries(resizer "-framework OpenCL")
  target_link_libraries(resizer /opt/libjpeg-turbo/lib64/libturbojpeg.dylib)
  target_link_libraries(resizer /usr/local/Frameworks/opencv2.framework)
endif (APPLE)

#windows specific
if (WIN32)
    set(ADDITIONAL_LIBRARY_DEPENDENCIES
        "$<$<CONFIG:Debug>:"
            "${OPENCV_DIR}/lib/Debug/opencv_core453d.lib"
            "${OPENCV_DIR}/lib/Debug/opencv_imgproc453d.lib"
            "${OPENCV_DIR}/lib/Debug/opencv_highgui453d.lib"
            "${OPENCV_DIR}/lib/Debug/opencv_imgcodecs453d.lib"
        ">"
        "$<$<NOT:$<CONFIG:Debug>>:"
            "${OPENCV_DIR}/lib/Release/opencv_core453.lib"
            "${OPENCV_DIR}/lib/Release/opencv_imgproc453.lib"
            "${OPENCV_DIR}/lib/Release/opencv_highgui453.lib"
            "${OPENCV_DIR}/lib/Release/opencv_imgcodecs453.lib"
        ">"
    )

  target_link_libraries(resizer "${JPEG_DIR}/lib/turbojpeg-static.lib")
  #target_link_libraries(resizer "${CUDA_DIR}/lib/x64/OpenCL.lib")
  target_link_libraries(resizer "${OPENCL_DIR}/lib/OpenCL.lib")
  target_link_libraries(resizer "${ADDITIONAL_LIBRARY_DEPENDENCIES}")
endif (WIN32)

SET(RUNDIR
    "$<$<CONFIG:Debug>:" "${PROJECT_BINARY_DIR}/Debug" ">"
    "$<$<NOT:$<CONFIG:Debug>>:" "${PROJECT_BINARY_DIR}/Release" ">"
)
string(REPLACE ";" "" RUNDIR ${RUNDIR})
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
        ${OPENCL_DIR}/bin/OpenCL.dll
        ${RUNDIR}/OpenCL.dll)

#https://thomas.trocha.com/blog/cmake--copy-files-after-build/
foreach(opencvmodule core imgproc highgui imgcodecs)
    set(COPYFROM
        "$<$<CONFIG:Debug>:" "${OPENCV_DIR}/bin/Debug/opencv_${opencvmodule}453d.dll" ">"
        "$<$<NOT:$<CONFIG:Debug>>:" "${OPENCV_DIR}/bin/Release/opencv_${opencvmodule}453.dll" ">")
    SET(COPYTO
        "$<$<CONFIG:Debug>:" "${PROJECT_BINARY_DIR}/Debug/opencv_${opencvmodule}453d.dll" ">"
        "$<$<NOT:$<CONFIG:Debug>>:" "${PROJECT_BINARY_DIR}/Release/opencv_${opencvmodule}453.dll" ">")

    string(REPLACE ";" "" COPYFROM ${COPYFROM})
    string(REPLACE ";" "" COPYTO ${COPYTO})

    add_custom_command(
        TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy "${COPYFROM}" "${COPYTO}"
)
endforeach(opencvmodule)

参考资料快照
参考资料快照

本文短链接:
If you have any questions or feedback, please reach out .