尊龙凯时人生就是博

ʹÓÃCMake¹¹½¨LinuxÍøÂçÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ

ʹÓÃcmake¹¹½¨linuxÍøÂçÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ

СÐò£º

ÔÚ¿ª·¢LinuxÍøÂçÓ¦ÓóÌÐòʱ £¬Ê¹ÓÃÒ»¸öºÃµÄ¹¹½¨ÏµÍ³ÊǺÜÊÇÖ÷ÒªµÄ¡£CMakeÊÇÒ»¸ö¿çƽ̨µÄ¹¹½¨×Ô¶¯»¯¹¤¾ß £¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÉèÖú͹¹½¨ÎÒÃǵÄÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃCMakeÀ´ÉèÖú͹¹½¨LinuxÍøÂçÓ¦ÓóÌÐò £¬²¢ÌṩһЩÊÊÓõĴúÂëʾÀý¡£

CMakeµÄ×°ÖúÍÉèÖãº

ÔÚ×îÏÈʹÓÃCMake֮ǰ £¬ÎÒÃÇÊ×ÏÈÐèÒªÔÚÎÒÃǵÄϵͳÉÏ×°ÖÃCMake¡£Ê¹ÓÃÒÔÏÂÏÂÁî¿ÉÒÔÔÚ´ó´ó¶¼Linux¿¯ÐаæÉÏ×°ÖÃCMake£º

sudo apt-get install cmake

µÇ¼ºó¸´ÖÆ

Ò»µ©CMake×°ÖÃÍê³É £¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öеÄĿ¼ £¬ÔÚ¸ÃĿ¼Öгõʼ»¯ÎÒÃǵÄCMakeÏîÄ¿¡£¼ÙÉèÎÒÃǵÄÏîÄ¿ÃûΪMyNetworkApp £¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐÒÔÏÂÏÂÁ

mkdir MyNetworkApp && cd MyNetworkApp
cmake .

µÇ¼ºó¸´ÖÆ

Õ⽫ÔÚÄ¿½ñĿ¼ÏÂÌìÉúÒ»¸öCMakeLists.txtÎļþ £¬ÎÒÃÇ¿ÉÒÔÔÚÕâ¸öÎļþÖÐÉèÖÃÎÒÃǵÄÏîÄ¿¡£

ÉèÖÃÍøÂçÓ¦ÓóÌÐò£º

ÔÚCMakeLists.txtÎļþÖÐ £¬ÎÒÃÇ¿ÉÒÔÉèÖÃÎÒÃǵÄÏîÄ¿µÄÏà¹ØÐÅÏ¢ £¬°üÀ¨ÏîÄ¿Ãû³Æ¡¢Ô´Îļþ¡¢ÒÀÀµÏîµÈ¡£Ê×ÏÈ £¬ÈÃÎÒÃÇÉèÖÃÏîÄ¿ÃûºÍ×îµÍ°æ±¾ÒªÇó£º

cmake_minimum_required(VERSION 3.10)
project(MyNetworkApp)

µÇ¼ºó¸´ÖÆ

È»ºó £¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÔ´Îļþµ½ÎÒÃǵÄÏîÄ¿ÖС£¼ÙÉèÎÒÃǵÄÍøÂçÓ¦ÓóÌÐòÐèÒªÒ»¸öÃûΪnetwork.cppµÄÔ´Îļþ £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÆäÌí¼Óµ½ÎÒÃǵÄÏîÄ¿ÖУº

add_executable(MyNetworkApp network.cpp)

µÇ¼ºó¸´ÖÆ

½ÓÏÂÀ´ £¬ÎÒÃÇ¿ÉÒÔÉèÖÃÎÒÃǵÄÏîÄ¿µÄ±àÒëÑ¡ÏîºÍÁ´½ÓÑ¡Ïî¡£ÀýÈç £¬ÈôÊÇÎÒÃÇÐèҪʹÓÃC++ 11±ê×¼±àÒëÎÒÃǵĴúÂë £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ

set(CMAKE_CXX_STANDARD 11)

µÇ¼ºó¸´ÖÆ

ÈôÊÇÎÒÃǵÄÓ¦ÓóÌÐòÒÀÀµÓÚÌض¨µÄ¿â £¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÁ´½ÓÕâЩ¿â£º

target_link_libraries(MyNetworkApp ${SOME_LIBRARY})

µÇ¼ºó¸´ÖÆ

ÔÚ¹¹½¨Àú³ÌÖÐ £¬ÎÒÃÇ»¹¿ÉÒÔÉèÖÃÆäËûÑ¡Ïî £¬ÀýÈç±àÒëÆ÷±ê¼Ç¡¢Ô¤´¦ÀíÆ÷½ç˵µÈ¡£ÏêϸµÄÑ¡Ïî¿ÉÒÔƾ֤ÎÒÃǵÄÐèÇó¾ÙÐÐÉèÖà £¬ÏêÇé¿ÉÒԲο¼CMakeµÄÎĵµ¡£

¹¹½¨ºÍÔËÐÐÓ¦ÓóÌÐò£º

Ò»µ©ÎÒÃÇÉèÖÃÍêÎÒÃǵÄÏîÄ¿ £¬ÎÒÃÇ¿ÉÒÔʹÓÃCMakeÀ´¹¹½¨ÎÒÃǵÄÓ¦ÓóÌÐò¡£Ö»ÐèÔÚÏîĿĿ¼ÖÐÖ´ÐÐÒÔÏÂÏÂÁ

cmake --build .

µÇ¼ºó¸´ÖÆ

Õ⽫±àÒëÎÒÃǵĴúÂë £¬²¢ÔÚÄ¿½ñĿ¼ÖÐÌìÉú¿ÉÖ´ÐÐÎļþ¡£¼ÙÉèÎÒÃǵĿÉÖ´ÐÐÎļþÃûΪMyNetworkApp £¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐÒÔÏÂÏÂÁîÀ´ÔËÐÐÎÒÃǵÄÓ¦ÓóÌÐò£º

./MyNetworkApp

µÇ¼ºó¸´ÖÆ

ʾÀý´úÂ룺

¼ÙÉèÎÒÃÇÒª¿ª·¢Ò»¸ö¼òÆÓµÄÍøÂçЧÀÍÆ÷Ó¦ÓóÌÐò £¬¼àÌýij¸öÌض¨¶Ë¿Ú £¬²¢ÔÚÊÕµ½ÇëÇóʱ·¢ËÍÒ»Ìõ½Ó´ýÐÂÎÅ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂë×÷Ϊnetwork.cpp£º

#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};
    const char* welcomeMessage = "Welcome to My Network App
";

    // ½¨ÉèÌ×½Ó×Ö
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    // ÉèÖÃÌ×½Ó×ÖÑ¡Ïî
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                                                  &opt, sizeof(opt))) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);

    // °ó¶¨Ì×½Ó×Öµ½Ìض¨¶Ë¿Ú
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    // ¼àÌý¶Ë¿Ú£¬×î¶àÅþÁ¬10¸ö¿Í»§¶Ë
    if (listen(server_fd, 10) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    // ÎüÊÕ¿Í»§¶ËÅþÁ¬ÇëÇó£¬²¢·¢ËͽӴýÐÂÎÅ
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
                     (socklen_t*)&addrlen))<0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    send(new_socket, welcomeMessage, strlen(welcomeMessage), 0);
    printf("Welcome message sent
");

    return 0;
}

µÇ¼ºó¸´ÖÆ

Õâ¸ö¼òÆÓµÄÍøÂçÓ¦ÓóÌÐò½¨ÉèÁËÒ»¸öЧÀÍÆ÷Ì×½Ó×Ö £¬°ó¶¨µ½8080¶Ë¿Ú £¬²¢¼àÌý¿Í»§¶ËÅþÁ¬¡£µ±Óпͻ§¶ËÅþÁ¬Ê± £¬Ëü»á·¢ËÍÒ»Ìõ½Ó´ýÐÂÎÅ¡£ÎÒÃÇ¿ÉÒÔ½«Õâ¸ö´úÂëÉúÑĵ½network.cppÎļþÖÐ £¬²¢½«ÉÏÊö´úÂëÌí¼Óµ½ÎÒÃǵÄCMakeLists.txtÎļþÖС£

½áÂÛ£º

CMakeÊÇÒ»¸öºÜÊÇÇ¿Ê¢µÄ¹¹½¨×Ô¶¯»¯¹¤¾ß £¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÉèÖú͹¹½¨ÎÒÃǵÄLinuxÍøÂçÓ¦ÓóÌÐò¡£Í¨¹ýºÏÀíÉèÖÃCMakeLists.txtÎļþ £¬ÎÒÃÇ¿ÉÒÔÀû±ãµØÌí¼ÓÔ´Îļþ¡¢ÉèÖñàÒëÑ¡Ïî¡¢Á´½Ó¿âµÈ¡£Ï£Íû±¾ÎÄÄÜ×ÊÖú¶ÁÕßÉîÈëÏàʶÔõÑùʹÓÃCMakeÀ´¹¹½¨ËûÃǵÄÍøÂçÓ¦ÓóÌÐò¡£

²Î¿¼ÎÄÏ×£º

CMake¹Ù·½Îĵµ: https://cmake.org/documentation/

ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÍøÂçÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ