c++ - 使用CppUTest问题 “expected type-specifier before ‘(’ token 进行Yaml-cpp配置解析器测试”

标签 c++ cmake compiler-errors yaml-cpp cpputest

我正在使用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)) ^
enter image description here
解析器
#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/

相关文章:

带有符号链接(symbolic link)的 CMake 文件(安装文件 DESTINATION 目录)

编译器不给出错误 undefined reference 的行号

c - 尝试在 MacOS 10.7.2 上构建 PROJ.4 库(& rgdal)时出现 "C compiler cannot create executables"

c++ - 如何让模板模板参数取一个数值?

c++ - 字符作为数字打印到 std::cout

c++ - 从源代码构建 CGAL 库的说明

c++ - 为什么scanf_s在OnlineJudge上导致编译器错误?

c++ - 将 Linux system() 调用命令的输出重定向到一个变量

c++ - 改进极小极大算法

java - INSTALL_FAILED_MISSING_SHARED_LIBRARY 导致应用无法安装