C++ 未定义引用,类没有“std::string”具有的“cxx11”ABI 标记 效果.h效果.cpp

问题描述

对于个人项目,树莓派控制的吉他踏板,我正在创建一个小型图书馆。这个库包含一堆类,指定了几种类型效果功能。为此,我有一个 Effect 基类,每个效果都继承自,以及一个 EffectFactory 基类,它将根据配置文件创建效果序列。我使用的是 C++,这是一种我很陌生的语言,因为我通常使用 JavaScript。

EffectFactory 由映射到其他 EffectFactory 实例的指针和两个函数的字符串映射组成;一个负责将 EffectFactory 实例注册到工厂映射,另一个是可以被 EffectFactory 实例覆盖的创建函数。 当我编译库进行测试时出现了我的问题。我收到一个错误

undefined reference to `EffectFactory::factories[abi:cxx11]'

使用 c++11 ABI 似乎存在问题。我再次运行编译器并添加-Wabi-tag 并得到:

effect-master/effect.h:22:7: warning: 'EffectFactory' does not have the "cxx11" ABI tag that 'std::string' {aka 'std::__cxx11::basic_string<char>'} (used in the type of 'virtual Effect* EffectFactory::create(std::string)') has [-Wabi-tag]
   22 | class EffectFactory
      |       ^~~~~~~~~~~~~
effect-master/effect.h:26:19: note: 'virtual Effect* EffectFactory::create(std::string)' declared here
   26 |   virtual Effect *create(std::string config);
      |                   ^~~~~~
In file included from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\string:55,from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\locale_classes.h:40,from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\ios_base.h:41,from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:42,from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\ostream:38,from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\iostream:39,from effect-master/effect.h:4,from effect-master/effect.cpp:1:
f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\basic_string.h:77:11: note: 'std::string' {aka 'std::__cxx11::basic_string<char>'} declared here
   77 |     class basic_string
      |           ^~~~~~~~~~~~
In file included from effect-master/effect.cpp:1:
effect-master/effect.h:29:49: warning: 'EffectFactory::factories' inherits the "cxx11" ABI tag that 'std::map<std::__cxx11::basic_string<char>,EffectFactory*>' (used in its type) has [-Wabi-tag]
   29 |   static std::map<std::string,EffectFactory *> factories;
      |                                                 ^~~~~~~~~
In file included from f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\map:61,from effect-master/effect.h:6,from effect-master/effect.cpp:1:
f:\programs\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_map.h:100:11: note: 'std::map<std::__cxx11::basic_string<char>,EffectFactory*>' declared here
  100 |     class map
      |           ^~~

据我所知,EffectFactorystd::stringstd::map 之间似乎存在某种不兼容。我花了一些时间谷歌搜索并找到了使用 -D_GLIBCXX_USE_CXX11_ABI=0解决方案,但这并没有解决它。我还发现了 this 问题,但我无法理解它,也不知道它是否适用于我的情况。谁能帮助我理解并解决这个问题?

作为参考,我使用的是 MinGW g++ 编译器。

效果.h

#ifndef EFFECT_H
#define EFFECT_H

#include <iostream>
#include <string>
#include <map>
#include <regex>

class EffectFactory;

class Effect
{
public:
  bool enabled = true;
  void toggleEnabled();
  virtual void triggerAction();
  virtual int eval(int input_signal);

protected:
  int output_signal;
};
class EffectFactory
{
public:
  static void registerType(const std::string &name,EffectFactory *factory);
  virtual Effect *create(std::string config);

private:
  static std::map<std::string,EffectFactory *> factories;
};

#endif

效果.cpp

#include "effect.h"

void Effect::toggleEnabled()
{
  enabled = !enabled;
};
void Effect::triggerAction()
{
  std::cout << "The base triggerAction was triggered\n";
};
int Effect::eval(int input_signal)
{
  std::cout << "The base eval was triggered\n";
  return input_signal;
};

Effect *EffectFactory::create(std::string config)
{
  std::smatch name_match;
  std::regex name_regex("^\\S+(?:\\n?)");
  std::regex replace_regex("^\\S+\\n?|^ {2}");
  std::regex_search(config,name_match,name_regex);
  config = std::regex_replace(config,replace_regex,"");
  return factories[name_match.str()]->create(config);
};
void EffectFactory::registerType(const std::string &name,EffectFactory *factory)
{
  factories[name] = factory;
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)