除了Conan外,vcpkg也是不错的C++包管理工具。跟conan相比,vcpkg是源码级的包管理,安装包的时候同时在本地编译。
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
#编译
.\bootstrap-vcpkg.bat
#集成
vcpkg integrate install
安装包
默认32位
vcpkg install gtest
vcpkg install benchmark
cmake_minimum_required(VERSION 3.5)
# 设置VCPKG_ROOT所在的位置
set(VCPKG_ROOT "${CMAKE_SOURCE_DIR}/../vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE PATH "")
set(CMAKE_TOOLCHAIN_FILE ${VCPKG_ROOT})
project(TEST_NOW)
find_package(GTest CONFIG REQUIRED)
find_package(benchmark CONFIG REQUIRED)
add_subdirectory(string_test)
增加依赖库
target_link_libraries(string_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main)
target_link_libraries(string_test PRIVATE benchmark::benchmark benchmark::benchmark_main)
代码中可以直接使用benchmark:
#include <string>
#include <benchmark/benchmark.h>
static void BM_STRING(benchmark::State& state)
{
for (auto _ : state) {
for (int ii = 0; ii < 1000000; ++ii)
{
std::string x("https://blog.csdn.net/mythma");
x.c_str();
}
}
}
BENCHMARK(BM_STRING);
BENCHMARK_MAIN();