c++cmakecompiler-errorsyaml-cppcpputest

Yaml-cpp configuration parser testing with CppUTest issue "expected type-specifier before ‘(’ token"


I am using yaml-cpp library (v 0.6.0) to parse yaml configuration file. Yaml-cpp is installed as system library.

I have made a class Parser which checks for key-value pairs in yaml node. The Parser class is built as ConfigParserLibrary static library.

Now I want to test the Parser class using CppUTest. Building the project with CMake. Example code:

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)

When I use the Parser class in another executable it is compiling without errors. But when I include the config_parser_lib/Parser.hpp file in the ConfigParserTests.cpp file it raises: /usr/include/c++/8/optional:208: error: expected type-specifier before ‘(’ token ::new ((void *) std::__addressof(this->_M_payload)) ^. enter image description here Parser.hpp

#pragma once
#include <yaml-cpp/yaml.h>

class Parser{
public:
    int parseNode(YAML::Node configNode); 
};

Parser.cpp

#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);
}

In CMakeLists.txt: when I change the CMAKE_CXX_STANDARD from 17 to 11, I am getting the following error /usr/include/c++/8/bits/stl_tree.h:636: error: ‘__node’ does not name a type ::new(__node) _Rb_tree_node<_Val>; ^~~~~~.

If I comment out the #include "config_parser_lib/Parser.hpp" line in ConfigParserTests.cpp, there is no error. Thus I think the error comes from including the <yaml-cpp/yaml.h> file. If I include <yaml-cpp/node/node.h> instead of yaml.h>, there is no error, but I need the <yaml-cpp/yaml.h> file which includes other headers I need to use.

gcc version 8.3.1 20190226.


Solution

  • Changing the include order in ConfigParserTests.cpp solved the problem.

    #include "config_parser_lib/Parser.hpp"
    #include <CppUTest/TestHarness.h>
    

    I assume that #include <yaml-cpp/yaml.h> should be at the beginning of every translation unit.