欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ubuntu18.04 vcpkg集成VScode

程序员文章站 2024-02-22 15:29:10
...

Ubuntu18.04 vcpkg集成VScode

vcpkg

微软开源的C++包管理工具

安装vcpkg

下载

git clone https://github.com/microsoft/vcpkg.git

安装:进入vcpkg目录

./bootstrap-vcpkg.sh

执行集成命令

# integrate在Linux上似乎没有用
./vcpkg integrate install
# 命令行补全
./vcpkg integrate bash

集成VScode

需要的插件

  • C/C++ for Visual Studio Code
  • CMake For VisualStudio Code
  • CMake Tools
  • Visual Studio IntelliCode

重要的步骤

在vcpkg/triplets目录创建x86-linux.cmake文件,并添加

set(VCPKG_TARGET_ARCHITECTURE x86)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE static)

set(VCPKG_CMAKE_SYSTEM_NAME Linux)

安装库:指定x86版本

vcpkg install poco:x86-linux

编写项目

//main.cpp
#include "Poco/Net/DNS.h"
#include <iostream>

using Poco::Net::DNS;
using Poco::Net::IPAddress;
using Poco::Net::HostEntry;

int main(int argc, char** argv)
{
    const HostEntry& entry = DNS::hostByName("www.baidu.com");
    std::cout << "Canonical Name: " << entry.name() << std::endl;
    const HostEntry::AliasList& aliases = entry.aliases();
    auto it = aliases.begin();
    for (; it != aliases.end(); ++it)
        std::cout << "Alias: " << *it << std::endl;
    const HostEntry::AddressList& addresses = entry.addresses();
    auto iter = addresses.begin();
    for (; iter != addresses.end(); ++iter)
        std::cout << "Address: " << *iter << std::endl;
    return 0;
}
//CMakeLists.txt
cmake_minimum_required(VERSION 3.10)

project(httptest)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-fsanitize=address")

link_directories(/home/xxx/Softerware/vcpkg/installed/x86-linux/lib ${PROJECT_SOURCE_DIR}/lib)
add_executable(httptest main.cpp)
target_link_libraries(httptest libPocoNet.a libPocoFoundation.a)
target_include_directories(httptest PRIVATE /home/xxx/Softerware/vcpkg/installed/x86-linux/include)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

最后利用cmake构建项目

使用CMake Tools构建项目,手动构建的话需要额外链接c语言的库。

vcpkg虽然不够完美,但也总算是减轻我们的负担了。