ʹÓÃCMake¹¹½¨LinuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ͻ񻣼
CMakeÊÇÒ»ÖÖ¿çƽ̨µÄ¹¹½¨¹¤¾ß£¬¿ÉÒÔÓÃÓÚ×Ô¶¯»¯¹¹½¨ºÍÖÎÀíÏîÄ¿¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃCMakeÉèÖú͹¹½¨Ò»¸öLinuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐò¡£ÎÒÃǽ«ÖصãÏÈÈÝCMakeµÄ»ù±¾ÉèÖúͳ£Óù¦Ð§£¬ÒÔ¼°ÔõÑùͨ¹ýʾÀý´úÂëչʾÆäÓ÷¨¡£
ÏÈÈÝCMake
CMakeÊÇÒ»¸ö¿ªÔ´µÄ¿çƽ̨¹¹½¨¹¤¾ß£¬¿ÉÒÔÓÃÓÚ×Ô¶¯»¯µØÌìÉúÏîÄ¿¹¹½¨Îļþ¡£ËüÖ§³Ö²î±ðµÄ¹¹½¨ÏµÍ³£¬ÈçGNU Make¡¢NinjaºÍVisual StudioµÈ¡£CMakeʹÓÃCMakeLists.txtÎļþÀ´ÐÎòÏîÄ¿µÄ¹¹½¨Àú³ÌºÍÒÀÀµ¹Øϵ£¬²¢Æ¾Ö¤¸ÃÎļþÌìÉúÏìÓ¦µÄ¹¹½¨Îļþ¡£
×°ÖÃCMake
ÔÚLinuxϵͳÖÐ×°ÖÃCMakeºÜÊǼòÆÓ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
½¨ÉèCMakeLists.txtÎļþ
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öCMakeLists.txtÎļþ¡£¸ÃÎļþ½«ÓÃÓÚÐÎòÏîÄ¿µÄÉèÖú͹¹½¨Àú³Ì¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄCMakeLists.txtÎļþʾÀý£º
cmake_minimum_required(VERSION 3.10) project(SmartLogisticsApp) # Ìí¼Ó¿ÉÖ´ÐÐÎļþ add_executable(smart_logistics_app main.cpp) # Ìí¼Ó¿âÎļþ target_link_libraries(smart_logistics_app lib1 lib2) # Ìí¼ÓÍ·Îļþ target_include_directories(smart_logistics_app PUBLIC include)
µÇ¼ºó¸´ÖÆ
Ìí¼ÓÔ´ÎļþºÍ¿âÎļþ
ÔÚCMakeLists.txtÎļþÖÐʹÓÃadd_executableÏÂÁîÌí¼ÓÔ´Îļþ£¬Ê¹ÓÃtarget_link_librariesÏÂÁîÌí¼Ó¿âÎļþ¡£ÔÚʾÀýÖУ¬ÎÒÃǽ«main.cppÎļþÌí¼ÓΪԴÎļþ£¬²¢Á´½Ólib1ºÍlib2¿âÎļþ¡£
Ìí¼ÓÍ·ÎļþĿ¼
ʹÓÃtarget_include_directoriesÏÂÁîÌí¼ÓÍ·ÎļþĿ¼¡£ÔÚʾÀýÖУ¬ÎÒÃǽ«includeĿ¼Ìí¼ÓΪͷÎļþĿ¼¡£
¹¹½¨ÏîÄ¿
ʹÓÃÒÔÏÂÏÂÁî¹¹½¨ÏîÄ¿£º
mkdir build cd build cmake .. make
µÇ¼ºó¸´ÖÆ
ʾÀý´úÂë˵Ã÷
ÏÂÃæÊǹØÓÚLinuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòµÄʾÀý´úÂ룺
// main.cpp #include <iostream> #include "vehicle.h" int main() { Vehicle vehicle("ABC123", "Truck"); std::cout << "Vehicle Type: " << vehicle.getType() << std::endl; std::cout << "License Plate: " << vehicle.getLicensePlate() << std::endl; return 0; } // vehicle.h #ifndef VEHICLE_H #define VEHICLE_H #include <string> class Vehicle { public: Vehicle(const std::string& licensePlate, const std::string& type); std::string getType() const; std::string getLicensePlate() const; private: std::string m_licensePlate; std::string m_type; }; #endif // vehicle.cpp #include "vehicle.h" Vehicle::Vehicle(const std::string& licensePlate, const std::string& type) : m_licensePlate(licensePlate), m_type(type) {} std::string Vehicle::getType() const { return m_type; } std::string Vehicle::getLicensePlate() const { return m_licensePlate; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀý´úÂëչʾÁËÒ»¸öÖÇÄÜÎïÁ÷Ó¦ÓóÌÐò£¬ÆäÖаüÀ¨Ò»¸ö³µÁ¾ÀàVehicle¡£main.cppÎļþÖн¨ÉèÁËÒ»¸öVehicle¹¤¾ß²¢´òÓ¡Ïà¹ØÐÅÏ¢¡£
½áÂÛ£º
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃCMakeÉèÖú͹¹½¨Ò»¸öLinuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòµÄ»ù±¾¼¼ÇÉ¡£ÎÒÃÇÌÖÂÛÁËCMakeµÄ×°ÖÃÀú³Ì£¬²¢ÌṩÁËÒ»¸öCMakeLists.txtÎļþµÄʾÀý¡£±ðµÄ£¬ÎÒÃÇ»¹ÌṩÁËÒ»¸öʹÓÃC++±àдµÄʾÀýÓ¦ÓóÌÐòµÄ´úÂ롣ͨ¹ýÕâƪÎÄÕ£¬¶ÁÕß¿ÉÒÔ¸üºÃµØÃ÷È·CMakeµÄÓ÷¨ºÍÆäÔÚÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòÖеÄÓ¦Óá£
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÖÇÄÜÎïÁ÷Ó¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡