我正在使用yaml-cpp库(v 0.6.0)来解析yaml配置文件。 Yaml-cpp作为系统库安装。
我做了一个Parser
类,它检查yaml节点中的键值对。 Parser
类被构建为ConfigParserLibrary
静态库。
现在,我想使用Parser
测试CppUTest
类。使用CMake
构建项目。示例代码:
CMakeLists:
cmake_minimum_required(VERSION 3.9)
project(yaml-parser VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ConfigParserLibrary lib
include_directories(${PROJECT_SOURCE_DIR}/include/config_parser_lib)
file(GLOB_RECURSE LIB_CONFIG_PARSER_SRC "config_parser_lib/Parser.cpp")
add_library(ConfigParserLibrary STATIC ${LIB_CONFIG_PARSER_SRC})
target_link_libraries(ConfigParserLibrary yaml-cpp)
# Executable that uses ConfigParserLibrary
add_executable(conf_reader "config_reader.cpp")
target_link_libraries(conf_reader yaml-cpp ConfigParserLibrary)
message(STATUS "Library tests will be built.")
add_library(ConfigParserTests STATIC tests/ConfigParserTests.cpp)
target_link_libraries(ConfigParserTests ConfigParserLibrary yaml-cpp)
add_executable(RunLibTests tests/RunTests.cpp)
target_link_libraries(RunLibTests CppUTest CppUTestExt
ConfigParserTests ConfigParserLibrary yaml-cpp)
add_test(NAME test_library COMMAND RunLibTests)
当我在另一个可执行文件中使用Parser类时,它正在编译而没有错误。但是,当我在ConfigParserTests.cpp文件中包含config_parser_lib/Parser.hpp文件时,它将引发:
/usr/include/c++/8/optional:208: error: expected type-specifier before ‘(’ token
::new ((void *) std::__addressof(this->_M_payload))
^
。解析器
#pragma once
#include <yaml-cpp/yaml.h>
class Parser{
public:
int parseNode(YAML::Node configNode);
};
解析器
#include "config_parser_lib/Parser.hpp"
int Parser::parseNode(YAML::Node configNode){
return valueA = configNode["keyA"].as<int>();
}
ConfigParserTests.cpp
#include <CppUTest/TestHarness.h>
#include "config_parser_lib/Parser.hpp"
TEST_GROUP(ConfigParserTests) {
};
TEST(ConfigParserTests, ParseConfigNode){
}
RunTests.cpp
#include <CppUTest/CommandLineTestRunner.h>
IMPORT_TEST_GROUP(ConfigParserTests);
int main(int ac, const char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}
在
CMakeLists.txt
中:当我将CMAKE_CXX_STANDARD
从17更改为11时,出现以下错误/usr/include/c++/8/bits/stl_tree.h:636: error: ‘__node’ does not name a type
::new(__node) _Rb_tree_node<_Val>;
^~~~~~
。如果我注释掉
#include "config_parser_lib/Parser.hpp"
中的ConfigParserTests.cpp
行,则没有错误。因此,我认为错误来自于包括<yaml-cpp/yaml.h>
文件。如果我包括<yaml-cpp/node/node.h>
而不是yaml.h>
,则没有错误,但是我需要<yaml-cpp/yaml.h>
文件,其中包含我需要使用的其他 header 。gcc版本8.3.1 20190226。
最佳答案
更改ConfigParserTests.cpp
中的包含顺序可以解决此问题。
#include "config_parser_lib/Parser.hpp"
#include <CppUTest/TestHarness.h>
我假设
#include <yaml-cpp/yaml.h>
应该在每个翻译单元的开头。
关于c++ - 使用CppUTest问题 “expected type-specifier before ‘(’ token 进行Yaml-cpp配置解析器测试”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169986/