cudnn,一定要选择最老的版本,否则就会报错(真坑,谁能想到)
cmake简介:https://www.huaweicloud.com/articles/ca75903dbb357a87c340fbfcd6b6dfe6.html
cmake 官方文档:https://cmake.org/cmake/help/v3.21/#reference-manuals
由于C++ 编译经验不足,这一块着实,让我怀疑人生。无奈之下,我最终卸载了 CUDA11.4 安装了 CUDA7.5,然后竟然就很顺利的编译完成了。(终究,还是需要吧 cmake弄明白,使用 CUDA11.4 来 编译 caffe 的官方源码,这样才能,进行caffe 的定制 并 编译得到 caffe的 定制版本)
编译 和 多文件 编译
VS 使用 cl 编译器 (cl 类似于 gcc 和 clang 编译器,cl位于 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin)
vs中,一个编译单元 就是 一个 cpp文件。有cpp文件 得到 .o文件,之后 编译器会根据 参数的选择 来生成 不同的结果(包括 .exe可执行文件、lib静态库、dll动态库)
举个 gcc的编译命令 gcc main.cpp -o main 。(不同的编译器 有自己跌 命令规范)
gcc 常用的 -i -l (i 是 include l 是 link)
cmake 用于 自动生成 编译命令(cmake 以 cmakelist.txt 作为输入,并输出makefile,然后 使用makefile 来指导编译器进行 编译操作。)
cmakelist.txt 里 定义了 一些 链接的操作,编译器 在链接 阶段 实际上是 根据 cmakelist.txt 中的定义 来进行链接的。
vs 的编译器 底层 也是调用了 nmake
不同的编译器,会对 cpp 文件 进行 重整 (就是 会有一个 包含 cpp 名字 返回类型 输入参数 要生成的文件类型 等的一条 复杂的长语句。形如 gcc add.cppp .. ... ... ... .lib) 。不能将 不同编译器 得到的文件 ,进行链接,因为,不同的编译器 会 生成 不同语法的 重整文件 ,这样,会出现链接错误。
在编译的过程中,编译器会打印没一条编译命令(也就是 每一个操作),在报错的时候,可以一条一条的分析,看是 在 编译的那个阶段 除了错,然后再 去相应的地方 查找和修改错误。
cmake 常用的 10条指令,几乎可以解决 日常的所有问题。
在cmake中 -l 和 -L 不同,-L 之后会跟 一个 外部库 的路径。
cmake 中 的 find_package() 用于 找包,find_package(OpenCV) 这条 语句 会 自动到 OpenCV 所在的路径下 找到 它的 cmakelist.txt ,并执行它,执行完毕后返回当前文件,继续执行。
cmakelist.txt 是跨平台的 通用的 编译入口。而 vs 怎样的ide 都是 在cmakelist.txt 基础上 封装的得到的编译入口,因此 这些 ide 不能跨平台, 只针对当前的系统环境。
整个 编译 过程 可 看做是 四个阶段 :cmake 中 含有两个阶段 congfig 用于根据 语句 计算 各种参数(这是个根据代码来计算的阶段), genery 用于 根据 计算得到的参数 来生成 makefile (这是个 根据 计算结果 来 定义规则的阶段),之后的操作 编译器 会有两个阶段 编译 cpp 文件(这是个编译代码的阶段),将 编译阶段 得到的文件 和 其他的 库或文件进行链接(这是个找库的阶段)。
make install makefile (首先 make执行编译操作,然后 install 根据 makefile中的 install语句 来 执行install操作)
cmake文件,总体是 是按照顺序执行的。
读懂下面两个 cmakelist.txt 文件,很重要,因为这两个文件,代表了 cmake的通用语法 ,并涵盖了 最常用的一些和语句。
一个自定义的cmakelist.txt (备忘:D:\cmake_test)
project(test)
find_package(OpenCV)
#message(${OpenCV_include_dirs})
#message(${OpenCV_libs})
#opencv_version
include_directories(${OpenCV_include_dirs})
include_directories(add)
add_library(add IMPORT)
add_executable(main main.cpp)
target_link_libraries(main add ${OpenCV_LIBS})
CMD窗口1:
Microsoft Windows [版本 10.0.19041.1052]
(c) Microsoft Corporation。保留所有权利。
C:\Users\73525>cmake
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Run 'cmake --help' for more information.
C:\Users\73525>cmake --version
cmake version 3.21.0-rc2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
C:\Users\73525>cl
'cl' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:\Users\73525> cd C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin>cl
用于 x86 的 Microsoft (R) C/C++ 优化编译器 18.00.30501 版版权所有(C) Microsoft Corporation。 保留所有权利。
用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin>nmake
Microsoft (R) 程序维护实用工具 12.00.21005.1 版
版权所有 (C) Microsoft Corporation。 保留所有权利。
NMAKE : fatal error U1064: 未找到 MAKEFILE 并且未指定目标
Stop.
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin>
cmd窗口2:
Microsoft Windows [版本 10.0.19041.1052]
(c) Microsoft Corporation。保留所有权利。
C:\Users\73525>cd D:\cmake_test
C:\Users\73525>d:
D:\cmake_test>mkdir build
D:\cmake_test>cd build
D:\cmake_test\build>cmake
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Run 'cmake --help' for more information.
D:\cmake_test\build>cmake -G
CMake Error: No generator specified for -G
Generators
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
* Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
Borland Makefiles = Generates Borland makefiles.
NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Ninja Multi-Config = Generates build-<Config>.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles JOM
= Generates CodeBlocks project files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - MinGW Makefiles = Generates CodeLite project files.
CodeLite - NMake Makefiles = Generates CodeLite project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Eclipse CDT4 - NMake Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - MinGW Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
Kate - MinGW Makefiles = Generates Kate project files.
Kate - NMake Makefiles = Generates Kate project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Sublime Text 2 - MinGW Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - NMake Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
D:\cmake_test\build>cmake -G"Visual Studio 12 2013 Win64" ..
-- The C compiler identification is MSVC 18.0.30501.0
-- The CXX compiler identification is MSVC 18.0.30501.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake_test/build
D:\cmake_test\build>cd ..
D:\cmake_test>mkdir build nmakebuild
子目录或文件 build 已经存在。
处理: build 时出错。
D:\cmake_test>mkdir nmakebuild
子目录或文件 nmakebuild 已经存在。
D:\cmake_test>cd nmakebuild
D:\cmake_test\nmakebuild>cmake -G"NMake Makefiles" ..
CMake Error at CMakeLists.txt:1 (project):
Running
'nmake' '-?'
failed with:
绯荤粺鎵句笉鍒版寚瀹氱殑鏂囦欢銆
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "D:/cmake_test/nmakebuild/CMakeFiles/CMakeOutput.log".
D:\cmake_test\nmakebuild>namke
'namke' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\cmake_test\nmakebuild>nmake
'nmake' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\cmake_test\nmakebuild>cd ..\build\Debug
D:\cmake_test\build\Debug>main.exe
3
D:\cmake_test\build\Debug>main.exe
3
D:\cmake_test\build\Debug>cd ..
D:\cmake_test\build>cd ..
D:\cmake_test>ls
'ls' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\cmake_test>dir
驱动器 D 中的卷是 Data
卷的序列号是 1A5E-4142
D:\cmake_test 的目录
06/07/2021 18:58 <DIR> .
06/07/2021 18:58 <DIR> ..
06/07/2021 19:00 <DIR> build
06/07/2021 18:54 46 CMakeLists.txt
06/07/2021 19:02 113 main.cpp
06/07/2021 18:59 <DIR> nmakebuild
2 个文件 159 字节
4 个目录 214,069,063,680 可用字节
D:\cmake_test>touch add.cpp
'touch' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\cmake_test>cd build
D:\cmake_test\build>cmake -G"Visual Studio 12 2013 Win64" ..
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake_test/build
D:\cmake_test\build>cd Debug
D:\cmake_test\build\Debug>main.exe
3
D:\cmake_test\build\Debug>cd ..
D:\cmake_test\build>cmake -G"Visual Studio 12 2013 Win64" ..
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake_test/build
D:\cmake_test\build>cmake -G"Visual Studio 12 2013 Win64" ..
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake_test/build
D:\cmake_test\build>cmake -G"Visual Studio 12 2013 Win64" ..
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.21)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cmake_test/build
D:\cmake_test\build>cd Debug
D:\cmake_test\build\Debug>main.exe
3
D:\cmake_test\build\Debug>
caffe的cmake.list.txt
cmake_minimum_required(VERSION 2.8.7)
if(MSVC)
# CMake 3.4 introduced a WINDOWS_EXPORT_ALL_SYMBOLS target property that makes it possible to
# build shared libraries without using the usual declspec() decoration.
# See: https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/
# and https://cmake.org/cmake/help/v3.5/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html
# for details.
cmake_minimum_required(VERSION 3.4)
endif()
if(POLICY CMP0046)
cmake_policy(SET CMP0046 NEW)
endif()
if(POLICY CMP0054)
cmake_policy(SET CMP0054 NEW)
endif()
# ---[ Caffe project
project(Caffe C CXX)
# ---[ Caffe version
set(CAFFE_TARGET_VERSION "1.0.0" CACHE STRING "Caffe logical version")
set(CAFFE_TARGET_SOVERSION "1.0.0" CACHE STRING "Caffe soname version")
add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION})
# ---[ Using cmake scripts and modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
include(ExternalProject)
include(GNUInstallDirs)
include(cmake/Utils.cmake)
include(cmake/Targets.cmake)
include(cmake/Misc.cmake)
include(cmake/Summary.cmake)
include(cmake/ConfigGen.cmake)
include(cmake/WindowsCreateLinkHeader.cmake)
include(cmake/TargetResolvePrerequesites.cmake)
# ---[ Options
caffe_option(CPU_ONLY "Build Caffe without CUDA support" OFF) # TODO: rename to USE_CUDA
caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON IF NOT CPU_ONLY)
caffe_option(USE_NCCL "Build Caffe with NCCL library support" OFF)
if(MSVC)
# default to static libs
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
else()
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
endif()
caffe_option(BUILD_python "Build Python wrapper" ON)
set(python_version "2" CACHE STRING "Specify which Python version to use")
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF)
caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE)
caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
caffe_option(USE_OPENMP "Link with OpenMP (when your BLAS wants OpenMP and you get linker errors)" OFF)
caffe_option(protobuf_MODULE_COMPATIBLE "Make the protobuf-config.cmake compatible with the module mode" ON IF MSVC)
caffe_option(COPY_PREREQUISITES "Copy the prerequisites next to each executable or shared library directory" ON IF MSVC)
caffe_option(INSTALL_PREREQUISITES "Install the prerequisites next to each executable or shared library directory" ON IF MSVC)
if(MSVC AND BUILD_SHARED_LIBS)
if(CMAKE_GENERATOR MATCHES "Visual Studio")
# see issue https://gitlab.kitware.com/cmake/cmake/issues/16552#note_215236
message(FATAL_ERROR "The Visual Studio generator cannot build a shared library. Use the Ninja generator instead.")
endif()
# Some tests (solver tests) fail when caffe is built as a shared library. The problem comes
# from protobuf that has a global static empty_string_ variable. Since caffe and test.testbin
# link to a static protobuf library both end up with their own instance of the empty_string_
# variable. This causes some SEH exception to occur. In practice if the caffe executable does not link
# to protobuf this problem should not happen. Use at your own risk.
message(WARNING "Some tests (solvers) will fail when building as a shared library with MSVC")
endif()
# ---[ Prebuild dependencies on windows
include(cmake/WindowsDownloadPrebuiltDependencies.cmake)
# ---[ Dependencies
include(cmake/Dependencies.cmake)
# ---[ Flags
if(UNIX OR APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
endif()
caffe_set_caffe_link()
if(USE_libstdcpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
message("-- Warning: forcing libstdc++ (controlled by USE_libstdcpp option in cmake)")
endif()
# ---[ Warnings
caffe_warnings_disable(CMAKE_CXX_FLAGS -Wno-sign-compare -Wno-uninitialized)
# ---[ Config generation
configure_file(cmake/Templates/caffe_config.h.in "${PROJECT_BINARY_DIR}/caffe_config.h")
# ---[ Includes
set(Caffe_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(Caffe_SRC_DIR ${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_BINARY_DIR})
# ---[ Includes & defines for CUDA
# cuda_compile() does not have per-call dependencies or include pathes
# (cuda_compile() has per-call flags, but we set them here too for clarity)
#
# list(REMOVE_ITEM ...) invocations remove PRIVATE and PUBLIC keywords from collected definitions and include pathes
if(HAVE_CUDA)
# pass include pathes to cuda_include_directories()
set(Caffe_ALL_INCLUDE_DIRS ${Caffe_INCLUDE_DIRS})
list(REMOVE_ITEM Caffe_ALL_INCLUDE_DIRS PRIVATE PUBLIC)
cuda_include_directories(${Caffe_INCLUDE_DIR} ${Caffe_SRC_DIR} ${Caffe_ALL_INCLUDE_DIRS})
# add definitions to nvcc flags directly
set(Caffe_ALL_DEFINITIONS ${Caffe_DEFINITIONS})
list(REMOVE_ITEM Caffe_ALL_DEFINITIONS PRIVATE PUBLIC)
list(APPEND CUDA_NVCC_FLAGS ${Caffe_ALL_DEFINITIONS})
endif()
# ---[ Subdirectories
add_subdirectory(src/gtest)
add_subdirectory(src/caffe)
add_subdirectory(tools)
add_subdirectory(examples)
add_subdirectory(python)
add_subdirectory(matlab)
add_subdirectory(docs)
# ---[ Linter target
add_custom_target(lint COMMAND ${CMAKE_COMMAND} -DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake)
# ---[ pytest target
if(BUILD_python)
if(UNIX)
set(python_executable python${python_version})
else()
set(python_executable ${PYTHON_EXECUTABLE})
endif()
add_custom_target(pytest COMMAND ${python_executable} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python )
add_dependencies(pytest pycaffe)
endif()
# ---[ uninstall target
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/Uninstall.cmake
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake/Uninstall.cmake)
# ---[ Configuration summary
caffe_print_configuration_summary()
# ---[ Export configs generation
caffe_generate_export_configs()