【发布时间】:2012-03-03 18:35:13
【问题描述】:
我正在努力在 linux 中构建一个库。这在 Windows 中构建和工作,但在 Linux 上,当使用静态库时,我在我们的库中得到一个未解析的符号。代码如下:
class MyClass : public AnotherClassRefCounded
{
public:
static bool queryInstance(MyClass **ppmyClass);
};
在这个类的 .cpp 文件中,我有:
#include "stdafx.h"
#include "MyClass.h"
MyClass* MyClass::m_pInstance = NULL;
bool MyClass::queryInstance(MyClass **myClass)
{
if(m_pInstance == NULL)
{
m_pInstance = new MyClass();
m_pInstance->incRef();
}
m_pInstance->incRef();
*myClass = m_pInstance;
return true;
}
现在在 libMyLib.a 上运行 nm -Cu 时,我得到以下输出:
[matt6809@hogganz400 libDebug]$ nm -Cu libMappingd.a | grep queryInstance
U AFewMoreScopes::MyClass::queryInstance(AFewMoreScopes::MyClass**)
我的系统信息是:
[matt6809@hogganz400 libDebug]$ cat /etc/redhat-release ; gcc --version
Red Hat Enterprise Linux Server release 6.1 (Santiago)
gcc (GCC) 4.4.5 20110214 (Red Hat 4.4.5-6)
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
出于版权原因,我不能发布相同的代码。我已经尽我所能复制了代码。如果您觉得我遗漏了任何信息,请随时询问。
更新
构建示例:
...
g++ -c -include Mappingd -pipe -w -g -fPIC -Wall -W <DEFINE FLAGS> <INCLUDE FLAGS> -o MyClass.o MyClass.cpp
...
ar cqs libMappingd.a <all object files>
更新 0
这很有趣。在“MyClass”的目标文件中,该符号不是未定义的:
MyClass.o:
<My Symbol Not Undefined>
但是,如果您转到另一个目标文件:
<Other Object>.o:
...
U Scope::MyClass::queryInstance(Socpe::MyClass**)
更新1
g++ -c -include Mappingd -pipe -w -g -fPIC -Wall -W -DLINUXx86 <DEFINE FLAGS> <INCLUDE FLAGS> -o MyClass.o MyClass.cpp
g++ -c -include Mappingd -pipe -w -g -fPIC -Wall -W -DLINUXx86 <DEFINE FLAGS> <INCLUDE FLAGS> -o OtherClass.o OtherClass.cpp
rm -f libMappingd.a
ar cqs libMappingd.a <Other Objects> MyClass.o OtherClass.o
和
g++ -c -include Mappingd -pipe -w -g -fPIC -Wall -W -DLINUXx86 <DEFINE FLAGS> <INCLUDE FLAGS> -o OtherClass.o OtherClass.cpp
g++ -c -include Mappingd -pipe -w -g -fPIC -Wall -W -DLINUXx86 <DEFINE FLAGS> <INCLUDE FLAGS> -o MyClass.o MyClass.cpp
rm -f libMappingd.a
ar cqs libMappingd.a <Other Objects> OtherClass.o MyClass.o
没关系,我仍然在其他库中得到未定义的符号。
【问题讨论】:
-
你是如何制作
libMappingd.a的?发布命令及其输出。 -
为什么'MyClass'在你的cpp文件中变成了'RuntimeEnvironment'?
-
我正在使用 Qt 构建静态库。给我一点时间从编译语句中取出一行。
-
版权原因?我认为这是合理使用,但无论如何。您发布的代码是否出现同样的错误?
-
我有一个使用静态实例方法和属性的单例类,它在库中编译得很好。我担心我们正在构建的图书馆可能有更深层次的问题,只是想在深入挖掘之前看看是否有一个简单的答案。