vs2026远程开发debian12容器的C++程序笔记

CMakeLists.txt

cpp 复制代码
# CMakeList.txt: CMakeProject3 的 CMake 项目,在此处包括源代码并定义
# 项目特定的逻辑。
#
cmake_minimum_required (VERSION 3.10)

set(CMAKE_TOOLCHAIN_FILE "/usr/software/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
set(VCPKG_ROOT "/usr/software/vcpkg")
set(VCPKG_TARGET_TRIPLET x64-linux CACHE STRING "")
set(VCPKG_INSTALL_PREFIX "${VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}")
include_directories(${VCPKG_INSTALL_PREFIX}/include)
link_directories(${VCPKG_INSTALL_PREFIX}/lib)
list(APPEND CMAKE_PREFIX_PATH ${VCPKG_INSTALL_PREFIX})
# 如果支持,请为 MSVC 编译器启用热重载。
if (POLICY CMP0141)
  cmake_policy(SET CMP0141 NEW)
  set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()


project ("CMakeProject3")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 将源代码添加到此项目的可执行文件。
add_executable (CMakeProject3 "CMakeProject3.cpp" "CMakeProject3.h")

find_package(Drogon CONFIG REQUIRED)
target_link_libraries(CMakeProject3 PRIVATE Drogon::Drogon)


if (CMAKE_VERSION VERSION_GREATER 3.12)
  set_property(TARGET CMakeProject3 PROPERTY CXX_STANDARD 20)
endif()

# TODO: 如有需要,请添加测试并安装目标。

CMakePresets.json

bash 复制代码
{
    "version": 3,
    "configurePresets": [
        {
            "name": "windows-base",
            "hidden": true,
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
                "CMAKE_C_COMPILER": "cl.exe",
                "CMAKE_CXX_COMPILER": "cl.exe"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Windows"
            }
        },
        {
            "name": "x64-debug",
            "displayName": "x64 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x64",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x64-release",
            "displayName": "x64 Release",
            "inherits": "x64-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x86-debug",
            "displayName": "x86 Debug",
            "inherits": "windows-base",
            "architecture": {
                "value": "x86",
                "strategy": "external"
            },
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "x86-release",
            "displayName": "x86 Release",
            "inherits": "x86-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_PROJECT_TOP_LEVEL_INCLUDES": "$env{VSINSTALLDIR}Common7/IDE/CommonExtensions/Microsoft/CMake/cmake/Microsoft/SegmentHeap.cmake"
            }
        },
        {
            "name": "linux-debug",
            "displayName": "Linux Debug",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
              "CMAKE_BUILD_TYPE": "Debug",
              "CMAKE_CXX_STANDARD": "17",
              "CMAKE_CXX_STANDARD_REQUIRED": "ON",
              "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
              "CMAKE_TOOLCHAIN_FILE": "/usr/software/vcpkg/scripts/buildsystems/vcpkg.cmake",
              "VCPKG_TARGET_TRIPLET": "x64-linux"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Linux"
            },
            "vendor": {
                "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                    "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
                }
            }
        },
        {
            "name": "macos-debug",
            "displayName": "macOS Debug",
            "generator": "Ninja",
            "binaryDir": "${sourceDir}/out/build/${presetName}",
            "installDir": "${sourceDir}/out/install/${presetName}",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            },
            "condition": {
                "type": "equals",
                "lhs": "${hostSystemName}",
                "rhs": "Darwin"
            },
            "vendor": {
                "microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
                    "sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
                }
            }
        }
    ]
}

主函数

CMakeProjects3.cpp

cpp 复制代码
#include "CMakeProject3.h"
#include <drogon/drogon.h>
#include <cstring> 
#include <cstdio>
using namespace drogon;

int count = 0;
int main()
{
    // 注册接口:GET /getMyName → 返回 helloxiaoming
    app().registerHandler(
        "/getMyName",
        [](const HttpRequestPtr& req,
            std::function<void(const HttpResponsePtr&)>&& callback)
        {
            // 创建响应
            char buf[256] = { 0 };
            char num_str[32];
            strcpy(buf, "hello xiaoming");
            sprintf(num_str, "%d", count);
            strcat(buf, num_str);
            auto resp = HttpResponse::newHttpResponse();
            resp->setBody((const char*)buf);
            resp->setContentTypeCode(CT_TEXT_PLAIN);
            count++;
            // 回调返回
            callback(resp);
        },
        { Get }  // 只允许 GET 请求
    );

    // 启动服务器,端口 19001
    app()
        .addListener("0.0.0.0", 19001)
        .setLogLevel(trantor::Logger::kInfo)
        .run();

    return 0;
}
相关推荐
玉树临风ives1 小时前
atcoder ABC 460 题解
数据结构·c++·算法
水无痕simon1 小时前
9 C语言的基础练习
c语言·开发语言·算法
少司府1 小时前
C++进阶:二叉搜索树
开发语言·数据结构·c++·二叉树·stl·二叉搜索树·tree
江华森1 小时前
Sealos 部署 Kubernetes 高可用集群 — 生产级技术笔记
笔记·容器·kubernetes
Rust研习社1 小时前
从 LaunchBadge 到 transact-rs:SQLx 社区迈出可持续治理的第一步
开发语言·后端·rust
Huangjin007_1 小时前
【C++ STL篇(十四)】哈希表实现:开放定址法与链地址法
c++·哈希算法·散列表
承渊政道1 小时前
【MySQL数据库学习】MySQL表的约束(上)
数据库·c++·学习·mysql·bash·数据库架构·数据库系统
minji...1 小时前
Linux高级IO(六)基于ET模式、单reactor反应堆的epoll版本的TCP计算服务器
linux·服务器·网络·c++·epoll·socket套接字·reactor反应堆模式
程序大视界1 小时前
【C++ 从基础到项目实战】C++(九):友元与设计模式初探——打破封装的艺术
开发语言·c++·cpp