Error: LNK2001: unresolved external symbol "private: static class

时光怂恿深爱的人放手 提交于 2019-12-12 02:28:20

问题


This forum contains many examples of such situation, but in my case static variables are defined correctly, however I still get that error. So this issue is not duplicate of previous and above link does not answer the question. Suggested 21 answers post does not have solution Simon gave me here, please unmark this as "duplicate".

Seems I've declared all correctly, check this:

.h file:

class ValueSetsModelsContainer : public QObject
{
  Q_OBJECT

public:
  static void DLLEXPORT loadAllergiesValueSets(MPTDatabase *db);
  static void DLLEXPORT loadProceduresValueSets(MPTDatabase *db);

  // Models access functions
  static QStandardItemModel *drugsModel();
  static QStandardItemModel *substanceModel();
  static QStandardItemModel *reactionsModel();

private:
  static QStandardItemModel *myDrugsModel, *mySubstanceModel, *myReactionsModel;
};

.cpp:

QStandardItemModel *ValueSetsModelsContainer::myDrugsModel = 0;
QStandardItemModel *ValueSetsModelsContainer::mySubstanceModel = 0;
QStandardItemModel *ValueSetsModelsContainer::myReactionsModel = 0;

QStandardItemModel *ValueSetsModelsContainer::drugsModel()
{
  return ValueSetsModelsContainer::myDrugsModel;
}

QStandardItemModel *ValueSetsModelsContainer::substanceModel()
{
  return ValueSetsModelsContainer::mySubstanceModel;
}

QStandardItemModel *ValueSetsModelsContainer::reactionsModel()
{
  return ValueSetsModelsContainer::myReactionsModel;
}

So static variables are defined in cpp, however I still get linking error in another module which calls ValueSetsModelsContainer methods:

  • allergiesdialog.obj:-1: error: LNK2001: unresolved external symbol "private: static class QStandardItemModel * ValueSetsModelsContainer::myDrugsModel" (?myDrugsModel@ValueSetsModelsContainer@@0PAVQStandardItemModel@@A)
  • allergiesdialog.obj:-1: error: LNK2001: unresolved external symbol "private: static class QStandardItemModel *
    ValueSetsModelsContainer::mySubstanceModel"
    (?mySubstanceModel@ValueSetsModelsContainer@@0PAVQStandardItemModel@@A)
  • allergiesdialog.obj:-1: error: LNK2001: unresolved external symbol "private: static class QStandardItemModel *
    ValueSetsModelsContainer::myReactionsModel"
    (?myReactionsModel@ValueSetsModelsContainer@@0PAVQStandardItemModel@@A)

Where the problem could be?


回答1:


From your link commands it turned out that you are linking together objects into a DLL and then in a second step link the DLL with your final binary. This might be caused by a subdirs template in your project settings.

Whenever you want to have a method of a DLL available from outside, you need to make it available via __declspec( dllexport ). I guess this is done in your custom precompiler constant DLLEXPORT.

Now try this in your .h file:

static DLLEXPORT QStandardItemModel *drugsModel();
static DLLEXPORT QStandardItemModel *substanceModel();
static DLLEXPORT QStandardItemModel *reactionsModel();

to make those methods available from outside the DLL.


By the way: I don't think it makes sense here to have an intermediate dynamic library (DLL) if you are just linking stuff from your own project and don't need to make it available to someone else. Consider using a static library instead by setting TEMPLATE = lib and CONFIG += staticlib in the .pro file where ValueSetsModelsContainer is in. But this is another topic and another question.



来源:https://stackoverflow.com/questions/32915615/error-lnk2001-unresolved-external-symbol-private-static-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!