際際滷

際際滷Share a Scribd company logo
cmake 襴
伎覓
覈谿
 螳
 覈
 覯
 譯殊 覈, 襷ろ襦, 
 譴 覲
 Example
螳
 蠍一ヾ  蠍磯 蟆曙 autotools 蟆譯殊伎 觜 ろ.
 C/C++ 襦蠏碁 譯朱 .
 Linux 覦  螻, OS X, Windows 讌.
  螻伎 Makefile 螻 蟯 殊 蟆郁骸覓朱 .
 Windows .sln  覦 蟯 殊 蟆郁骸覓朱 .
 OS X Makefile 螻 蟯 殊  蟆朱 豸.(蟆渚 )
覈
 殊企襴 殊 襷.
  襷 殊企襴 襷 ろ 殊 襷.
覯
 cmake 襦語る  螳讌襦 蟲
 蟆 れ
 cmake -S . -Bbuild
 -S source螳 豺 螻褐 讌. C++ sourc螳  CMakeLists.txt
 -B build 蟯 朱れ 焔 螻褐 讌. Source 豺 build 豺 覿襴 蟆 譬.
 觜
 cmake --build build
 覯 cmake --help  れ 危碁ゼ 谿語^
 https://cmake.org/cmake/help/v3.14/manual/cmake.1.html
譯殊 覈, 襷ろ襦, (1)
 cmake_minimum_required (VERSION 2.6)
 cmake script 誤煙  れ.  豕 覯 3.14
 Project (Tutorial)
 襦 覈 蠍. IDE 願朱 襦碁 朱朱 .
 add_executable(programName main.cpp)
 main.cpp襯 貉危 覦 襷伎 programName企朱 ろ殊 襷.
 add_library(libraryName SHARED|STATIC lib.cpp)
 lib.cpp襯 貉危 覦 襷伎 殊企襴 殊 襷.
 SHARED襯 覃 liblibraryName.so( 螻), libraryName.lib/dll() 豢.
 STATIC 覃 liblibrary.a( 螻), libraryName.lib()螳 豢
譯殊 覈, 襷ろ襦, (2)
 include
 : include(OpenSSL)  一 蟆曙, C:Program FilesCMakesharecmake-
3.13Modules 豺  FindOpenSSL.cmake 螳  蟆朱 碁.
 find_package
 : find_package(OpenSSL)  FindOpenSSL.cmake  焔 れ ろ伎, 蟯
覲れ 螳 谿城.
 OPENSSL_INCLUDE_DIR  OPENSSL_LIBRARIES 螳 焔.
 焔 覲螳 伎, target_include_directories, target_link_libraries 煙 .
譯殊 覈, 襷ろ襦, (3)
 target_link_libraries
 : target_link_libraries(Tutorial ${OPENSSL_LIBRARIES})
 Target Tutorial 燕 , OPENSSL_LIBRARIES  れ 螳朱 襷.
谿瑚) Target: add_executable() 轟 add_library() 襯   れ螳
 target_include_directories
 : target_include_directories(Tutorial PRIVATE ${OPENSSL_INCLUDE_DIR})
 Target tutorial Include 蟆 蟆暑襦 豢螳.
谿瑚) private, public: private   target , public   target 訖襷 , 
target  project 豢る 襦 危危.
譯殊 覈, 襷ろ襦, (4)
 add_subdirectory
 : add_subdirectory(moduleTest)
 moduleTest 朱  襴 CMakeLists.txt襯 蟷 伎 ろ.
  襦碁ゼ 覯 觜  
  襴 れ螳 讌 . 讀,  find_package 煙 牛 視 螳 
 襴 蠏碁襦  螳.
 message
 : message("OPENSSL_INCLUDE_DIR="${OPENSSL_INCLUDE_DIR})
 cmake れ 譴 覃伎 覃讌襯 豢.
 cmake れ 譴螳 螳 誤企慨 襦 襷 螻 .
譯殊 覈, 襷ろ襦, (5)
 install
  螻伎 蟆曙, 豕譬朱 make install target 覦 蟆朱 覲伎.
 一 蟆曙, INSTALL.vcxproj 殊 焔, 願 レ 譯朱 蟆朱
覲伎.
 : install(TARGETS first LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib
ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib)
 LIBRARY .so 殊 覩, ARCHIVE .a  .lib 殊 覩
 一 .dll  れ襯 伎 RUNTIME
譴 覲(1)
 CMAKE_MODULE_PATH
 cmake  殊企襴螳 れ 豺手 覲  .
 螳 cmake 覈 襷れ 蟆曙,  螳 豢螳 覈  include 蟆 蟆暑襯 豢螳
 .
 : set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
 襦蟆 襷 Findsecp256k1.cmake 殊 cmake 襴 譟伎蟆 覃,
include(Findsecp256k1)朱 覈   .
 CMAKE_CXX_STANDARD
 C++ standard襯 讌  .
 : set (CMAKE_CXX_STANDARD 11)  c++11 standard 襦 貉危狩.
譴 覲(2)
 CMAKE_SOURCE_DIR
 cmake襯 豕豐襦 ろ  讌 source 豺
  襴襦 れ願蟆 朱 讌.
 CMAKE_CURRENT_SOURCE_DIR
  source 豺
  襴襦 れ願蟆 覃 伎 襷豢 覲蟆暑.
Example: 螳覦 蟆
CMakeLists.txt
+---firstexec
| CMakeLists.txt
| main.cpp
|
---firstlib
CMakeLists.txt
firstlib.cpp
firstlib.h
cmake_minimum_required (VERSION 2.6)
project(cmake-tutorial)
add_subdirectory(firstlib)
add_subdirectory(firstexec)
CMakeLists.txt in root
襴 蟲譟
Example: firstlib
cmake_minimum_required (VERSION 2.6)
project(firstlib)
add_library(first STATIC firstlib.cpp)
install(TARGETS first
LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib
ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib
)
install(FILES firstlib.h DESTINATION
${CMAKE_SOURCE_DIR}/include)
CMakeLists.txt in firstlib
#include "firstlib.h"
int add(int a, int b) {
return a+b;
}
firstlib.cpp in firstlib
#ifndef FIRSTLIB_H
#define FIRSTLIB_H
int add(int a, int b);
#endif //FIRSTLIB_H
firstlib.h in firstlib
Example: firstexec
cmake_minimum_required (VERSION 2.6)
project(firstexec)
add_executable(firstexec main.cpp)
target_link_libraries(firstexec first)
target_include_directories(firstexec
PRIVATE
${CMAKE_SOURCE_DIR}/firstlib)
install(TARGETS firstexec
RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/bin)
CMakeLists.txt in firstexec
#include <iostream>
#include <firstlib.h>
int main()
{
using namespace std;
cout << "from firstlib, " << add(10, 20) << endl;
return 0;
}
main.cpp in firstexec
Example 覯
 觜 蟾讌襷
 cmake -S . -B build
 cmake --build build
 れ  
 cmake --build build --target install
 Example source code 豺?
 https://github.com/moonyl/cmake-tutorial.git

More Related Content

Cmake tutorial

  • 2. 覈谿 螳 覈 覯 譯殊 覈, 襷ろ襦, 譴 覲 Example
  • 3. 螳 蠍一ヾ 蠍磯 蟆曙 autotools 蟆譯殊伎 觜 ろ. C/C++ 襦蠏碁 譯朱 . Linux 覦 螻, OS X, Windows 讌. 螻伎 Makefile 螻 蟯 殊 蟆郁骸覓朱 . Windows .sln 覦 蟯 殊 蟆郁骸覓朱 . OS X Makefile 螻 蟯 殊 蟆朱 豸.(蟆渚 )
  • 4. 覈 殊企襴 殊 襷. 襷 殊企襴 襷 ろ 殊 襷.
  • 5. 覯 cmake 襦語る 螳讌襦 蟲 蟆 れ cmake -S . -Bbuild -S source螳 豺 螻褐 讌. C++ sourc螳 CMakeLists.txt -B build 蟯 朱れ 焔 螻褐 讌. Source 豺 build 豺 覿襴 蟆 譬. 觜 cmake --build build 覯 cmake --help れ 危碁ゼ 谿語^ https://cmake.org/cmake/help/v3.14/manual/cmake.1.html
  • 6. 譯殊 覈, 襷ろ襦, (1) cmake_minimum_required (VERSION 2.6) cmake script 誤煙 れ. 豕 覯 3.14 Project (Tutorial) 襦 覈 蠍. IDE 願朱 襦碁 朱朱 . add_executable(programName main.cpp) main.cpp襯 貉危 覦 襷伎 programName企朱 ろ殊 襷. add_library(libraryName SHARED|STATIC lib.cpp) lib.cpp襯 貉危 覦 襷伎 殊企襴 殊 襷. SHARED襯 覃 liblibraryName.so( 螻), libraryName.lib/dll() 豢. STATIC 覃 liblibrary.a( 螻), libraryName.lib()螳 豢
  • 7. 譯殊 覈, 襷ろ襦, (2) include : include(OpenSSL) 一 蟆曙, C:Program FilesCMakesharecmake- 3.13Modules 豺 FindOpenSSL.cmake 螳 蟆朱 碁. find_package : find_package(OpenSSL) FindOpenSSL.cmake 焔 れ ろ伎, 蟯 覲れ 螳 谿城. OPENSSL_INCLUDE_DIR OPENSSL_LIBRARIES 螳 焔. 焔 覲螳 伎, target_include_directories, target_link_libraries 煙 .
  • 8. 譯殊 覈, 襷ろ襦, (3) target_link_libraries : target_link_libraries(Tutorial ${OPENSSL_LIBRARIES}) Target Tutorial 燕 , OPENSSL_LIBRARIES れ 螳朱 襷. 谿瑚) Target: add_executable() 轟 add_library() 襯 れ螳 target_include_directories : target_include_directories(Tutorial PRIVATE ${OPENSSL_INCLUDE_DIR}) Target tutorial Include 蟆 蟆暑襦 豢螳. 谿瑚) private, public: private target , public target 訖襷 , target project 豢る 襦 危危.
  • 9. 譯殊 覈, 襷ろ襦, (4) add_subdirectory : add_subdirectory(moduleTest) moduleTest 朱 襴 CMakeLists.txt襯 蟷 伎 ろ. 襦碁ゼ 覯 觜 襴 れ螳 讌 . 讀, find_package 煙 牛 視 螳 襴 蠏碁襦 螳. message : message("OPENSSL_INCLUDE_DIR="${OPENSSL_INCLUDE_DIR}) cmake れ 譴 覃伎 覃讌襯 豢. cmake れ 譴螳 螳 誤企慨 襦 襷 螻 .
  • 10. 譯殊 覈, 襷ろ襦, (5) install 螻伎 蟆曙, 豕譬朱 make install target 覦 蟆朱 覲伎. 一 蟆曙, INSTALL.vcxproj 殊 焔, 願 レ 譯朱 蟆朱 覲伎. : install(TARGETS first LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib) LIBRARY .so 殊 覩, ARCHIVE .a .lib 殊 覩 一 .dll れ襯 伎 RUNTIME
  • 11. 譴 覲(1) CMAKE_MODULE_PATH cmake 殊企襴螳 れ 豺手 覲 . 螳 cmake 覈 襷れ 蟆曙, 螳 豢螳 覈 include 蟆 蟆暑襯 豢螳 . : set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) 襦蟆 襷 Findsecp256k1.cmake 殊 cmake 襴 譟伎蟆 覃, include(Findsecp256k1)朱 覈 . CMAKE_CXX_STANDARD C++ standard襯 讌 . : set (CMAKE_CXX_STANDARD 11) c++11 standard 襦 貉危狩.
  • 12. 譴 覲(2) CMAKE_SOURCE_DIR cmake襯 豕豐襦 ろ 讌 source 豺 襴襦 れ願蟆 朱 讌. CMAKE_CURRENT_SOURCE_DIR source 豺 襴襦 れ願蟆 覃 伎 襷豢 覲蟆暑.
  • 13. Example: 螳覦 蟆 CMakeLists.txt +---firstexec | CMakeLists.txt | main.cpp | ---firstlib CMakeLists.txt firstlib.cpp firstlib.h cmake_minimum_required (VERSION 2.6) project(cmake-tutorial) add_subdirectory(firstlib) add_subdirectory(firstexec) CMakeLists.txt in root 襴 蟲譟
  • 14. Example: firstlib cmake_minimum_required (VERSION 2.6) project(firstlib) add_library(first STATIC firstlib.cpp) install(TARGETS first LIBRARY DESTINATION ${CMAKE_SOURCE_DIR}/lib ARCHIVE DESTINATION ${CMAKE_SOURCE_DIR}/lib ) install(FILES firstlib.h DESTINATION ${CMAKE_SOURCE_DIR}/include) CMakeLists.txt in firstlib #include "firstlib.h" int add(int a, int b) { return a+b; } firstlib.cpp in firstlib #ifndef FIRSTLIB_H #define FIRSTLIB_H int add(int a, int b); #endif //FIRSTLIB_H firstlib.h in firstlib
  • 15. Example: firstexec cmake_minimum_required (VERSION 2.6) project(firstexec) add_executable(firstexec main.cpp) target_link_libraries(firstexec first) target_include_directories(firstexec PRIVATE ${CMAKE_SOURCE_DIR}/firstlib) install(TARGETS firstexec RUNTIME DESTINATION ${CMAKE_SOURCE_DIR}/bin) CMakeLists.txt in firstexec #include <iostream> #include <firstlib.h> int main() { using namespace std; cout << "from firstlib, " << add(10, 20) << endl; return 0; } main.cpp in firstexec
  • 16. Example 覯 觜 蟾讌襷 cmake -S . -B build cmake --build build れ cmake --build build --target install Example source code 豺? https://github.com/moonyl/cmake-tutorial.git