ʹÓÃCMake¹¹½¨LinuxÇø¿éÁ´Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linuxÇø¿éÁ´Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
СÐò£º
Çø¿éÁ´ÊÖÒÕÕýÈÕÒæ³ÉΪµ±½ñ×îÖËÊÖ¿ÉÈȵÄÊÖÒÕÖ®Ò»£¬Ëü¾ßÓÐÈ¥ÖÐÐÄ»¯¡¢Çå¾²¿É¿¿¡¢Í¸Ã÷µÈÌص㣬±»ÆÕ±éÓ¦ÓÃÓÚ½ðÈÚ¡¢Ò½ÁÆ¡¢¹©Ó¦Á´µÈÁìÓò¡£ÔÚ¹¹½¨Ò»¸ö¸ßЧ¡¢Îȹ̵ÄÇø¿éÁ´Ó¦ÓóÌÐòʱ£¬Ñ¡ÔñºÏÊʵĹ¹½¨¹¤¾ßÊÇÖÁ¹ØÖ÷ÒªµÄ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃCMake¹¹½¨LinuxÉϵÄÇø¿éÁ´Ó¦ÓóÌÐò£¬²¢Ìṩ´úÂëʾÀý¡£
Ò»¡¢CMake¼ò½é
CMakeÊÇÒ»ÖÖ¿ªÔ´µÄ¿çƽ̨¹¹½¨¹¤¾ß£¬¿ÉÒÔ×ÊÖú¿ª·¢Õß¼ò»¯¹¹½¨Àú³Ì£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£CMakeÄܹ»Æ¾Ö¤²î±ðµÄ²Ù×÷ϵͳºÍ±àÒëÆ÷ÌìÉúÏìÓ¦µÄ¹¹½¨¹æÔò£¬Àû±ã¿ª·¢ÕßÔÚ²î±ðµÄƽ̨ÉϹ¹½¨ÏîÄ¿¡£
¶þ¡¢×°ÖÃCMake
ÔÚ×îÏÈʹÓÃCMake֮ǰ£¬ÎÒÃÇÐèÒªÏÈ×°ÖÃËü¡£ÔÚLinuxÉÏ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
Èý¡¢½¨ÉèCMakeLists.txtÎļþ
ÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪCMakeLists.txtµÄÎļþ£¬¸ÃÎļþÓÃÓÚÐÎòÏîÄ¿µÄ¹¹½¨Àú³Ì¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄCMakeLists.txtÎļþ£º
# Ö¸¶¨CMake×îµÍ°æ±¾ cmake_minimum_required(VERSION 3.10) # ÉèÖÃÏîÄ¿Ãû³Æ project(BlockchainApp) # ÉèÖÃÔ´ÎļþÁбí set(SOURCES main.cpp blockchain.cpp transaction.cpp # Ìí¼ÓÆäËûµÄÔ´Îļþ ) # ÉèÖÃÍ·ÎļþËÑË÷·¾¶ include_directories( ${CMAKE_CURRENT_SOURCE_DIR} ) # ÌìÉú¿ÉÖ´ÐÐÎļþ add_executable(${PROJECT_NAME} ${SOURCES})
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇÊ×ÏÈÖ¸¶¨ÁËËùÐèµÄCMake×îµÍ°æ±¾£¬È»ºóÉèÖÃÁËÏîÄ¿µÄÃû³Æ¡£½ÓÏÂÀ´£¬ÎÒÃÇÁгöÁËÏîÄ¿ÖеÄÔ´ÎļþÁÐ±í£¬²¢Í¨¹ýinclude_directories()ÉèÖÃÁËÍ·ÎļþËÑË÷·¾¶£¬¿ÉÒÔƾ֤ÏîÄ¿µÄÏÖÕæÏàÐÎ×÷³öÏìÓ¦µÄÐ޸ġ£
ËÄ¡¢¹¹½¨ÏîÄ¿
ÔÚÏîÄ¿¸ùĿ¼Ï£¬·¿ªÖնˣ¬Ö´ÐÐÒÔÏÂÏÂÁî¹¹½¨ÏîÄ¿£º
mkdir build cd build cmake .. make
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁî»áÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪbuildµÄĿ¼£¬¸ÃÄ¿ÈÎÃüÓÚÉúÑĹ¹½¨Àú³ÌÖÐÌìÉúµÄÔÝʱÎļþºÍ×îÖյĿÉÖ´ÐÐÎļþ¡£cmake ..Ö¸Áî»á¶ÁÈ¡ÉÏÒ»°ì·¨Öн¨ÉèµÄCMakeLists.txtÎļþ£¬²¢ÌìÉúÏìÓ¦µÄ¹¹½¨¹æÔò¡£È»ºó£¬Í¨¹ýmakeÖ¸Áî×îÏȹ¹½¨ÏîÄ¿²¢ÌìÉú¿ÉÖ´ÐÐÎļþ¡£
Îå¡¢ÔËÐгÌÐò
¹¹½¨Íê³Éºó£¬¿ÉÒÔÔÚbuildĿ¼ÏÂÕÒµ½ÌìÉúµÄ¿ÉÖ´ÐÐÎļþ¡£Ö´ÐÐÒÔÏÂÏÂÁîÔËÐгÌÐò£º
./BlockchainApp
µÇ¼ºó¸´ÖÆ
Áù¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃCMake¹¹½¨LinuxÉϵÄÇø¿éÁ´Ó¦ÓóÌÐò£¬²¢¸ø³öÁËÏìÓ¦µÄ´úÂëʾÀý¡£CMake×÷ΪһÖÖ¹¦Ð§Ç¿Ê¢¡¢ÎÞаÐԸߵĹ¹½¨¹¤¾ß£¬Äܹ»×ÊÖú¿ª·¢Õß¼ò»¯¹¹½¨Àú³Ì£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£Ï£Íû±¾ÎĶÔÕýÔÚ¹¹½¨Çø¿éÁ´Ó¦ÓóÌÐòµÄ¿ª·¢ÕßÓÐËù×ÊÖú¡£
´úÂëʾÀý£º
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄÇø¿éÁ´Ó¦ÓóÌÐòʾÀý£º
// main.cpp #include <iostream> #include "blockchain.h" #include "transaction.h" int main() { Blockchain blockchain; Transaction tx1("Alice", "Bob", 10); blockchain.addTransaction(tx1); Transaction tx2("Bob", "Charlie", 5); blockchain.addTransaction(tx2); blockchain.mineBlock(); std::cout << "Chain size: " << blockchain.getBlockchainSize() << std::endl; std::cout << "Balance of Alice: " << blockchain.getBalance("Alice") << std::endl; std::cout << "Balance of Bob: " << blockchain.getBalance("Bob") << std::endl; std::cout << "Balance of Charlie: " << blockchain.getBalance("Charlie") << std::endl; return 0; }
µÇ¼ºó¸´ÖÆ
// blockchain.h #pragma once #include <vector> #include "transaction.h" class Block { public: std::vector<Transaction> transactions; // Ìí¼ÓÆäËûµÄÇø¿éÐÅÏ¢ }; class Blockchain { public: void addTransaction(const Transaction& transaction); void mineBlock(); int getBalance(const std::string& address) const; int getBlockchainSize() const; private: std::vector<Block> blockchain; // Ìí¼ÓÆäËûµÄÇø¿éÁ´ÐÅÏ¢ };
µÇ¼ºó¸´ÖÆ
// transaction.h #pragma once #include <string> class Transaction { public: Transaction(const std::string& from, const std::string& to, int amount) : from(from), to(to), amount(amount) {} std::string from; std::string to; int amount; // Ìí¼ÓÆäËûµÄÉúÒâÓªÒµÐÅÏ¢ };
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÊÇÒ»¸ö¼òÆÓµÄÇø¿éÁ´Ó¦ÓóÌÐòʾÀý£¬ÆäÖаüÀ¨ÁËÇø¿éÁ´µÄ»ù±¾²Ù×÷£¬ÈçÌí¼ÓÉúÒâÓªÒµ¡¢ÍÚ¿óºÍÅÌÎÊÓà¶îµÈ¡£
²Î¿¼×ÊÁÏ£º
CMake¹Ù·½Îĵµ£ºhttps://cmake.org/documentation/
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÇø¿éÁ´Ó¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡