0

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.

vg34
  • 91
  • 2
  • 10
  • Such errors normally accompanied with the **filename** and the **line number** which causes the error. Please, show the line which cases the error. Without that info resolving a problem is just a *guessing* game. – Tsyvarev May 16 '19 at 13:47
  • Thank you for responding @Tsyvarev. I have edited the question. – vg34 May 16 '19 at 13:52
  • Interesting.. The error message also should contain an "include-chain" - which header file includes errorneous header, which file includes that header file and so on, up to a source file. Show that chain too. – Tsyvarev May 16 '19 at 14:16
  • @Tsyvarev I have posted an image of the whole include-chain. – vg34 May 16 '19 at 14:32

1 Answers1

0

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.

vg34
  • 91
  • 2
  • 10