在 CMake 世界中:
vcpkg
vcpkg 是适用于 Windows、Linux 和 macOS 的 C++ 库管理器的包管理器。它可以与 CMake 无缝集成 - 有关详细信息,请参阅 here。
柯南
Conan 是一个 C/C++ 包管理器。它还有一个 strategy 用于与 CMake 的集成。
带有 ExternalProject_Add 的 CMake
CMakeList.txt.in:
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
CMakeList.txt:
cmake_minimum_required(VERSION 3.8)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
example.cpp
#include <iostream>
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
在 CMake 领域之外:
我建议你不要使用 CMake!使用Bazel!
例如如果你想使用gtest:
工作空间
工作区(name = "GTestDemo")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "googletest",
#tag = "release-1.8.1",
commit = "2fe3bd994b3189899d93f1d5a881e725e046fdc2",
remote = "https://github.com/google/googletest",
shallow_since = "1535728917 -0400",
)
构建
cc_test(
name = "tests",
srcs = ["test.cpp"],
copts = ["-isystem external/gtest/include"],
deps = [
"@googletest//:gtest_main",
],
)
text.cpp
#include <iostream>
#include "gtest/gtest.h"
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
如何运行测试?
bazel test //...
例如如果你想使用boost:
工作空间
workspace(name = "BoostFilesystemDemo")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# Fetch Boost repo
git_repository(
name = "com_github_nelhage_rules_boost",
commit = "49066b7ccafce2609a3d605e3667af3f07e8547c",
remote = "https://github.com/Vertexwahn/rules_boost",
shallow_since = "1559083909 +0200",
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()
构建
cc_binary(
name = "FilesystemTest",
srcs = ["main.cpp"],
defines = ["BOOST_ALL_NO_LIB"],
deps = [
"@boost//:filesystem",
],
)
main.cpp
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
如何构建:
bazel build //...
如何运行:
bazel run //:FilesystemTest
如果要生成 Visual Studio 解决方案,请使用 lavender。不幸的是,薰衣草只是实验性的,需要一些改进。但我认为在这里花费精力而不是让 CMake 处理所有依赖项更有意义。还有some projects 尝试制作 Bazel CMake 互操作。