Boost Spirit - 项目构建使用 -O1 但不使用 -O2
Boost Spirit - project builds with -O1 but not with -O2
我有一个使用 Boost Spirit X3 的小项目(类似于他们的示例 - https://github.com/cierelabs/x3_fun/),它是在调试中构建的,但不是在发布中构建的。
X3程序结构推荐+另一个例子:https://www.boost.org/doc/libs/1_69_0/libs/spirit/doc/x3/html/spirit_x3/tutorials/minimal.html
编译器:GCC 8.2.0
OS: Windows 10
在 git 对分和一些项目配置实验之后,我将问题缩小到编译器优化标志。
这构建:
g++ -std=c++17 -O1 -pedantic -Wall -Wextra -c -fmessage-length=0
这不会构建:
g++ -std=c++17 -O2 -pedantic -Wall -Wextra -c -fmessage-length=0
我得到的错误:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ./src/main.o:main.cpp:(.text+0x2156): undefined reference to `bool fs::parser::parse_rule<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::spirit::x3::unused_type, boost::spirit::x3::unused_type const>(boost::spirit::x3::rule<fs::parser::whitespace_class, boost::spirit::x3::unused_type, false>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, boost::spirit::x3::unused_type const&, boost::spirit::x3::unused_type const&)'
看起来优化器正在删除实际需要的 parse_rule
定义(这是从 Spirit 生成的核心函数)。
nm -C main.o
转储(左边是-O2
,右边是-O1
):https://www.diffchecker.com/YAFmJkbN
nm -C grammar.o
转储(左边是-O2
,右边是-O1
):https://www.diffchecker.com/6dUgMiEK
nm -C utility.o
dump(左边是-O2
,右边是-O1
):https://www.diffchecker.com/mXGf2Hx7(除了地址没有区别)
最初我认为问题类似于 c++ linker error: undefined references only on optimized build 但我在源文件中没有模板,除了 Boost 可能在其宏中定义的模板。
优化器错误地删除函数定义的原因是什么?
代码:
grammar.hpp
#pragma once
#include <boost/spirit/home/x3.hpp>
#include <tuple>
#include <utility>
namespace fs::parser
{
namespace x3 = boost::spirit::x3;
// symbols that denote filter's language constants
struct booleans_ : x3::symbols<bool>
{
booleans_()
{
add
("True", true)
("False", false)
;
}
};
const booleans_ booleans;
// some constants to aovid code duplication
constexpr auto keyword_boolean = "Boolean";
constexpr auto keyword_number = "Number";
constexpr auto keyword_true = "True";
constexpr auto keyword_false = "False";
constexpr auto assignment_operator = '=';
constexpr auto newline_character = '\n';
auto set_first = [](auto& ctx){ _val(ctx).first += _attr(ctx); };
auto set_second = [](auto& ctx){ _val(ctx).second += _attr(ctx); };
using x3::raw;
using x3::lexeme;
using x3::alpha;
using x3::alnum;
using x3::char_;
using x3::lit;
// comment - a line that starts with #
using comment_type = x3::rule<class comment_class>;
BOOST_SPIRIT_DECLARE(comment_type)
// identifier
using identifier_type = x3::rule<class identifier_class, std::string>;
BOOST_SPIRIT_DECLARE(identifier_type)
// whitespace
// Filter Spirit grammar skips any whitespace except newline character
using whitespace_type = x3::rule<class whitespace_class>;
BOOST_SPIRIT_DECLARE(whitespace_type)
// string
using string_type = x3::rule<class string_class, std::string>;
BOOST_SPIRIT_DECLARE(string_type)
// boolean definition: Boolean b = True
using constant_boolean_definition_type = x3::rule<class constant_boolean_definition_class, std::pair<std::string, bool>>;
BOOST_SPIRIT_DECLARE(constant_boolean_definition_type)
// number definition: Number n = 3
using constant_number_definition_type = x3::rule<class constant_number_definition_class, std::pair<std::string, int>>;
BOOST_SPIRIT_DECLARE(constant_number_definition_type)
// constants
using constant_definition_type = x3::rule<class constant_definition_class, boost::variant<constant_boolean_definition_type::attribute_type, constant_number_definition_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(constant_definition_type)
// filter language consists of lines, of which every is a comment or some code
using code_line_type = x3::rule<class code_line_class, boost::optional<constant_definition_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(code_line_type)
// the entire language grammar
using grammar_type = x3::rule<class grammar_class, std::vector<code_line_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(grammar_type)
using skipper_type = whitespace_type;
// Boost Spirit recommends that this should be in a separate config.hpp file but
// in our case we need skipper type to be visible so we place configuration here
using iterator_type = std::string::const_iterator;
using context_type = x3::phrase_parse_context<skipper_type>::type;
}
namespace fs
{
parser::grammar_type grammar();
parser::skipper_type skipper();
}
grammar_def.hpp
#pragma once
#include "grammar.hpp"
namespace fs::parser
{
const comment_type comment = "comment";
const auto comment_def = lexeme['#' >> *(char_ - newline_character)];
BOOST_SPIRIT_DEFINE(comment)
const identifier_type identifier = "identifier";
const auto identifier_def = lexeme[(alpha | '_') >> *(alnum | '_')];
BOOST_SPIRIT_DEFINE(identifier)
const whitespace_type whitespace = "whitespace";
const auto whitespace_def = x3::space - newline_character;
BOOST_SPIRIT_DEFINE(whitespace)
const string_type string = "string";
const auto string_def = lexeme['"' >> +(char_ - '"') >> '"'];
BOOST_SPIRIT_DEFINE(string)
const constant_boolean_definition_type constant_boolean_definition = "Boolean definition";
const auto constant_boolean_definition_def = lit(keyword_boolean) >> identifier[set_first] >> lit(assignment_operator) >> booleans[set_second];
BOOST_SPIRIT_DEFINE(constant_boolean_definition)
const constant_number_definition_type constant_number_definition = "Number definition";
const auto constant_number_definition_def = lit(keyword_number) >> identifier[set_first] >> lit(assignment_operator) >> x3::int_[set_second];
BOOST_SPIRIT_DEFINE(constant_number_definition)
const constant_definition_type constant_definition = "value definition";
const auto constant_definition_def = constant_boolean_definition | constant_number_definition;
BOOST_SPIRIT_DEFINE(constant_definition)
const code_line_type code_line = "line";
const auto code_line_def = newline_character | comment | constant_definition;
BOOST_SPIRIT_DEFINE(code_line)
const grammar_type grammar = "code";
const auto grammar_def = *code_line;
BOOST_SPIRIT_DEFINE(grammar)
}
namespace fs
{
inline
parser::grammar_type grammar()
{
return parser::grammar;
}
inline
parser::skipper_type skipper()
{
return parser::whitespace;
}
}
grammar.cpp
#include "grammar_def.hpp"
namespace fs::parser
{
BOOST_SPIRIT_INSTANTIATE(grammar_type, iterator_type, context_type)
}
utility.hpp
#pragma once
#include <string>
#include <optional>
namespace fs::utility
{
bool file_exists(const char* path);
inline
bool file_exists(const std::string& path) { return file_exists(path.c_str()); }
std::optional<std::string> load_file(const char* file_path);
inline
std::optional<std::string> load_file(const std::string& file_path) { return load_file(file_path.c_str()); }
}
utility.cpp
#include "utility.hpp"
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
namespace fs::utility
{
bool file_exists(const char* path)
{
struct stat buffer;
return stat(path, &buffer) == 0;
}
std::optional<std::string> load_file(const char* file_path)
{
std::ifstream file(file_path);
if (!file.good())
return {};
return std::string(std::istreambuf_iterator<char>{file}, {});
}
}
main.cpp
#include "utility.hpp"
#include "grammar.hpp"
#include <iostream>
bool parse(std::string const& source)
{
namespace x3 = boost::spirit::x3;
fs::parser::iterator_type it = source.begin();
const fs::parser::iterator_type end = source.end();
fs::parser::grammar_type::attribute_type data;
const bool result = x3::phrase_parse(
it,
end,
fs::grammar(),
fs::skipper(),
data
);
if (it != end) // fail if we did not get a full match
{
std::cout << "parse failure at pos: " << it - source.begin() << "\n";
return false;
}
return result;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "usage: " << "./program filepath\n";
return -1;
}
if (parse(fs::utility::load_file(argv[1]).value()))
{
std::cout << "parse successful\n";
}
else
{
std::cout << "parse failure\n";
}
}
我已经设法自己修复了它。
解决方案:将#include "grammar_def.hpp"
添加到main.cpp
。
发生了什么:BOOST_SPIRIT_DEFINE
声明了 内联 函数模板。如果它们没有立即在任何给定的翻译单元中使用,则所有这些都将被剥离。它们需要在使用 x3::phrase_parse
的同一 TU 中可见,否则它们将被编译器认为不需要 - 因此出现未定义的引用错误。
编辑:问题的根源是 Spirit 中的错误。这个答案只是 TU 机制的一种解决方法。请参阅其他答案以获得正确的解决方案。
parser::grammar_type grammar()
和 parser::skipper_type skipper()
未导出(我遇到了关于此的链接器错误,但您没有在您的问题中提及它)。将它们移动到 grammar.cpp
(当然还要删除它们的 inline
)以解决问题。
缺少船长实例化(您提供的唯一错误消息就是关于此的)。将 BOOST_SPIRIT_INSTANTIATE(whitespace_type, iterator_type, boost::spirit::x3::unused_type)
放入 grammar.cpp
应该可以解决问题,但是...
但是错误消息不会消失,这很可能是因为 Spirit 中的错误。它使用规则的属性类型实例化 parse_rule
,默认值是非常量 unused_type
,但它看起来像是在某处跳过它通过 const ref 获取属性。要解决此问题,请指定具有 const 属性类型 x3::rule<class whitespace_class, boost::spirit::x3::unused_type const>
.
的船长规则
结果https://gist.github.com/Kojoley/f7766422a5b6fd9e34b4b9d434325336.
更新:PR https://github.com/boostorg/spirit/pull/437 将为大家解决问题。
我有一个使用 Boost Spirit X3 的小项目(类似于他们的示例 - https://github.com/cierelabs/x3_fun/),它是在调试中构建的,但不是在发布中构建的。
X3程序结构推荐+另一个例子:https://www.boost.org/doc/libs/1_69_0/libs/spirit/doc/x3/html/spirit_x3/tutorials/minimal.html
编译器:GCC 8.2.0
OS: Windows 10
在 git 对分和一些项目配置实验之后,我将问题缩小到编译器优化标志。
这构建:
g++ -std=c++17 -O1 -pedantic -Wall -Wextra -c -fmessage-length=0
这不会构建:
g++ -std=c++17 -O2 -pedantic -Wall -Wextra -c -fmessage-length=0
我得到的错误:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ./src/main.o:main.cpp:(.text+0x2156): undefined reference to `bool fs::parser::parse_rule<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::spirit::x3::unused_type, boost::spirit::x3::unused_type const>(boost::spirit::x3::rule<fs::parser::whitespace_class, boost::spirit::x3::unused_type, false>, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, __gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, boost::spirit::x3::unused_type const&, boost::spirit::x3::unused_type const&)'
看起来优化器正在删除实际需要的 parse_rule
定义(这是从 Spirit 生成的核心函数)。
nm -C main.o
转储(左边是-O2
,右边是-O1
):https://www.diffchecker.com/YAFmJkbN
nm -C grammar.o
转储(左边是-O2
,右边是-O1
):https://www.diffchecker.com/6dUgMiEK
nm -C utility.o
dump(左边是-O2
,右边是-O1
):https://www.diffchecker.com/mXGf2Hx7(除了地址没有区别)
最初我认为问题类似于 c++ linker error: undefined references only on optimized build 但我在源文件中没有模板,除了 Boost 可能在其宏中定义的模板。
优化器错误地删除函数定义的原因是什么?
代码:
grammar.hpp
#pragma once
#include <boost/spirit/home/x3.hpp>
#include <tuple>
#include <utility>
namespace fs::parser
{
namespace x3 = boost::spirit::x3;
// symbols that denote filter's language constants
struct booleans_ : x3::symbols<bool>
{
booleans_()
{
add
("True", true)
("False", false)
;
}
};
const booleans_ booleans;
// some constants to aovid code duplication
constexpr auto keyword_boolean = "Boolean";
constexpr auto keyword_number = "Number";
constexpr auto keyword_true = "True";
constexpr auto keyword_false = "False";
constexpr auto assignment_operator = '=';
constexpr auto newline_character = '\n';
auto set_first = [](auto& ctx){ _val(ctx).first += _attr(ctx); };
auto set_second = [](auto& ctx){ _val(ctx).second += _attr(ctx); };
using x3::raw;
using x3::lexeme;
using x3::alpha;
using x3::alnum;
using x3::char_;
using x3::lit;
// comment - a line that starts with #
using comment_type = x3::rule<class comment_class>;
BOOST_SPIRIT_DECLARE(comment_type)
// identifier
using identifier_type = x3::rule<class identifier_class, std::string>;
BOOST_SPIRIT_DECLARE(identifier_type)
// whitespace
// Filter Spirit grammar skips any whitespace except newline character
using whitespace_type = x3::rule<class whitespace_class>;
BOOST_SPIRIT_DECLARE(whitespace_type)
// string
using string_type = x3::rule<class string_class, std::string>;
BOOST_SPIRIT_DECLARE(string_type)
// boolean definition: Boolean b = True
using constant_boolean_definition_type = x3::rule<class constant_boolean_definition_class, std::pair<std::string, bool>>;
BOOST_SPIRIT_DECLARE(constant_boolean_definition_type)
// number definition: Number n = 3
using constant_number_definition_type = x3::rule<class constant_number_definition_class, std::pair<std::string, int>>;
BOOST_SPIRIT_DECLARE(constant_number_definition_type)
// constants
using constant_definition_type = x3::rule<class constant_definition_class, boost::variant<constant_boolean_definition_type::attribute_type, constant_number_definition_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(constant_definition_type)
// filter language consists of lines, of which every is a comment or some code
using code_line_type = x3::rule<class code_line_class, boost::optional<constant_definition_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(code_line_type)
// the entire language grammar
using grammar_type = x3::rule<class grammar_class, std::vector<code_line_type::attribute_type>>;
BOOST_SPIRIT_DECLARE(grammar_type)
using skipper_type = whitespace_type;
// Boost Spirit recommends that this should be in a separate config.hpp file but
// in our case we need skipper type to be visible so we place configuration here
using iterator_type = std::string::const_iterator;
using context_type = x3::phrase_parse_context<skipper_type>::type;
}
namespace fs
{
parser::grammar_type grammar();
parser::skipper_type skipper();
}
grammar_def.hpp
#pragma once
#include "grammar.hpp"
namespace fs::parser
{
const comment_type comment = "comment";
const auto comment_def = lexeme['#' >> *(char_ - newline_character)];
BOOST_SPIRIT_DEFINE(comment)
const identifier_type identifier = "identifier";
const auto identifier_def = lexeme[(alpha | '_') >> *(alnum | '_')];
BOOST_SPIRIT_DEFINE(identifier)
const whitespace_type whitespace = "whitespace";
const auto whitespace_def = x3::space - newline_character;
BOOST_SPIRIT_DEFINE(whitespace)
const string_type string = "string";
const auto string_def = lexeme['"' >> +(char_ - '"') >> '"'];
BOOST_SPIRIT_DEFINE(string)
const constant_boolean_definition_type constant_boolean_definition = "Boolean definition";
const auto constant_boolean_definition_def = lit(keyword_boolean) >> identifier[set_first] >> lit(assignment_operator) >> booleans[set_second];
BOOST_SPIRIT_DEFINE(constant_boolean_definition)
const constant_number_definition_type constant_number_definition = "Number definition";
const auto constant_number_definition_def = lit(keyword_number) >> identifier[set_first] >> lit(assignment_operator) >> x3::int_[set_second];
BOOST_SPIRIT_DEFINE(constant_number_definition)
const constant_definition_type constant_definition = "value definition";
const auto constant_definition_def = constant_boolean_definition | constant_number_definition;
BOOST_SPIRIT_DEFINE(constant_definition)
const code_line_type code_line = "line";
const auto code_line_def = newline_character | comment | constant_definition;
BOOST_SPIRIT_DEFINE(code_line)
const grammar_type grammar = "code";
const auto grammar_def = *code_line;
BOOST_SPIRIT_DEFINE(grammar)
}
namespace fs
{
inline
parser::grammar_type grammar()
{
return parser::grammar;
}
inline
parser::skipper_type skipper()
{
return parser::whitespace;
}
}
grammar.cpp
#include "grammar_def.hpp"
namespace fs::parser
{
BOOST_SPIRIT_INSTANTIATE(grammar_type, iterator_type, context_type)
}
utility.hpp
#pragma once
#include <string>
#include <optional>
namespace fs::utility
{
bool file_exists(const char* path);
inline
bool file_exists(const std::string& path) { return file_exists(path.c_str()); }
std::optional<std::string> load_file(const char* file_path);
inline
std::optional<std::string> load_file(const std::string& file_path) { return load_file(file_path.c_str()); }
}
utility.cpp
#include "utility.hpp"
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
namespace fs::utility
{
bool file_exists(const char* path)
{
struct stat buffer;
return stat(path, &buffer) == 0;
}
std::optional<std::string> load_file(const char* file_path)
{
std::ifstream file(file_path);
if (!file.good())
return {};
return std::string(std::istreambuf_iterator<char>{file}, {});
}
}
main.cpp
#include "utility.hpp"
#include "grammar.hpp"
#include <iostream>
bool parse(std::string const& source)
{
namespace x3 = boost::spirit::x3;
fs::parser::iterator_type it = source.begin();
const fs::parser::iterator_type end = source.end();
fs::parser::grammar_type::attribute_type data;
const bool result = x3::phrase_parse(
it,
end,
fs::grammar(),
fs::skipper(),
data
);
if (it != end) // fail if we did not get a full match
{
std::cout << "parse failure at pos: " << it - source.begin() << "\n";
return false;
}
return result;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "usage: " << "./program filepath\n";
return -1;
}
if (parse(fs::utility::load_file(argv[1]).value()))
{
std::cout << "parse successful\n";
}
else
{
std::cout << "parse failure\n";
}
}
我已经设法自己修复了它。
解决方案:将#include "grammar_def.hpp"
添加到main.cpp
。
发生了什么:BOOST_SPIRIT_DEFINE
声明了 内联 函数模板。如果它们没有立即在任何给定的翻译单元中使用,则所有这些都将被剥离。它们需要在使用 x3::phrase_parse
的同一 TU 中可见,否则它们将被编译器认为不需要 - 因此出现未定义的引用错误。
编辑:问题的根源是 Spirit 中的错误。这个答案只是 TU 机制的一种解决方法。请参阅其他答案以获得正确的解决方案。
parser::grammar_type grammar()
和parser::skipper_type skipper()
未导出(我遇到了关于此的链接器错误,但您没有在您的问题中提及它)。将它们移动到grammar.cpp
(当然还要删除它们的inline
)以解决问题。缺少船长实例化(您提供的唯一错误消息就是关于此的)。将
BOOST_SPIRIT_INSTANTIATE(whitespace_type, iterator_type, boost::spirit::x3::unused_type)
放入grammar.cpp
应该可以解决问题,但是...但是错误消息不会消失,这很可能是因为 Spirit 中的错误。它使用规则的属性类型实例化
parse_rule
,默认值是非常量unused_type
,但它看起来像是在某处跳过它通过 const ref 获取属性。要解决此问题,请指定具有 const 属性类型x3::rule<class whitespace_class, boost::spirit::x3::unused_type const>
. 的船长规则
结果https://gist.github.com/Kojoley/f7766422a5b6fd9e34b4b9d434325336.
更新:PR https://github.com/boostorg/spirit/pull/437 将为大家解决问题。