×ðÁú¿­Ê±

ʹÓÃ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ÍøÂçÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

13452372176

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

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

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