mac构建有一个未定义的符号,而linux构建并没有用于相同的代码

mac build has undefined symbol where linux build did not for same code

本文关键字:构建 用于 并没有 代码 linux 有一个 未定义 符号 mac      更新时间:2023-10-16

我们有适用于mac的gcc 4.2和适用于linux的gcc 4.4。当我构建相同的代码时,我得到以下未定义的符号:

"MyClassNameSpecific1::MyClassNameSpecific1(int,int,int、className::class1 const&,className:;class2 const&&、int,int)",引用自:MyOtherClassName::MyOtherClassName.o中的mContainer()ld:找不到体系结构x86_64 的符号

MyClassName.h:的代码如下

class MyClassNameSpecific1;
class MyClassNameSpecific2;
class MyClassNameSpecific1
{
public: 
MyClassNameSpecific1(const string &param1);
virtual ~MyClassNameSpecific1() {}
}
class MyClassNameSpecific2: public classU::UData 
{
public:
MyClassNameSpecific2(int width, int height, int breadth, const className::class1 &dType, const className::class2 &layout, int tWidth, int tHeight);
};

MyClassName.cpp有这个:

#include "MyClassName.h"
MyClassNameSpecific1::MyClassNameSpecific1(const string &param1) : classU::UData()
{
//does things here
}
MyClassNameSpecific2::MyClassNameSpecific2(int width, int height, int breadth, const className::class1 &dType, const className::class2 &layout, int tWidth, int tHeight) : classU::UData()
{
//does things here
}

我不确定是什么原因导致了一场不败,但另一场却没有。有人知道gcc在这种情况下有什么不同吗?我的makefile包含所有类:MyClassName,然后是其他类。我看不出MyClassNameSpecific2的.h和.cpp文件之间的参数签名有任何差异。

我试着确保在.h和.cpp文件中都使用了完整的className::class1,但它仍然有相同的undef。此外,我尝试添加类MyClassNameSpecific2,其中它在MyClassName.h文件的顶部只有类MyClassName Specific1,但它没有更改undef。我试着在谷歌上搜索这个问题,但没有找到任何相关的信息。也许还有其他东西我可以搜索,但我不确定。我在.h和.cpp文件中省略了小类定义的#includes。

如果有人可以尝试,那就太好了,即使这是我没有想到的参数的不同命名法。

添加了makefile:

.SUFFIXES: .cpp
DEP_DIR = ../dependencies
CC = g++
OS := $(shell uname -s)
ifeq ($(OS),Darwin)
#set LIB_DIR
LIB_DIR1=darwin64_gcc42/lib
LIB_DIR2=darwin64_gcc44/lib
else
LIB_DIR=linux64_gcc44/lib
endif
INCDIRS = -I. -I../include 
-I$(DEP_DIR)/className/include 
-I$(DEP_DIR)/classNameOther/include
#C++FLAGS = -c -fPIC -g -O2 -DLINUX -D_DEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall
C++FLAGS = -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall
ifeq ($(OS),Darwin)
LDFLAGS = -m64 -pthread -ldl -shared -L../$(LIB_DIR1)/release 
-L$(DEP_DIR)/className/$(LIB_DIR1) 
-L$(DEP_DIR)/classNameOther/$(LIB_DIR2)/release
else
LDFLAGS = -m64 -pthread -ldl -shared -L../$(LIB_DIR)/release 
-L$(DEP_DIR)/className/$(LIB_DIR) 
-L$(DEP_DIR)/classNameOther/$(LIB_DIR)/release
endif
LDLIBS = -llittleClass -lclassName -lclassNameOther -lclassNameOthermalloc
all:    MyClassName MyOtherClassName AnotherClass2 AnotherClass3 AnotherClass4 AMoreOverallClass
AMoreOverallClass: AMoreOverallClass.o
$(CC) AMoreOverallClass.o $(LDFLAGS) -o $@ $(LDLIBS)
...

.cpp.o:
$(CC) $(C++FLAGS) $(INCDIRS) $< -o $@
clean:
rm -rf *.o all

以下是在命令行进行输出:

mcle@engmacvi01(577)% make
g++ -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall -I. -I../include -I../dependencies/className/include -I../dependencies/classNameOther/include MyClassName.cpp -o MyClassName.o
g++ MyClassName.o -m64 -pthread -ldl -shared -L../darwin64_gcc42/lib/release -L../dependencies/className/darwin64_gcc42/lib -L../dependencies/classNameOther/darwin64_gcc44/lib/release -o MyClassName-ludm -lclassName -lclassNameOther -lclassNameOthermalloc
g++ -c -fPIC -O2 -DLINUX -DNDEBUG -D_FILE_OFFSET_BITS=64 -m64 -Wall -I. -I../include -I../dependencies/className/include -I../dependencies/classNameOther/include AnotherClass.cpp -o AnotherClass.o
g++ AnotherClass.o -m64 -pthread -ldl -shared -L../darwin64_gcc42/lib/release -L../dependencies/className/darwin64_gcc42/lib -L../dependencies/classNameOther/darwin64_gcc44/lib/release -o AnotherClass -llittleClass -lclassName -lclassNameOther -lclassNameOthermalloc
Undefined symbols for architecture x86_64:
"MyClassNameSpecific1::MyClassNameSpecific1(int, int, int, className::class1 const&, className::class2 const&, int, int)", referenced from:
AnotherClass::mContainer()       in AnotherClass.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [AnotherClass] Error 1

此外,MyOtherClassName.cpp:

UReturnClass &MyOtherClassName::mContainer()
{
if (!m_mContainerPtr)
{
m_mContainerPtr = new UReturnClass();
UMPtr md = new MyClassNameSpecific2(m_width, m_height, m_bands, m_dataType, m_dataLayout, m_tileWidth, m_tileHeight);
m_mContainerPtr->setMdata(md);
}
return *m_mContainerPtr;
}

MyOtherClassName.h:

className::class1 m_dType;className::class2 m_dLayout;

修复方法是更改makefile,使其在一个步骤中构建所有文件,而不是为每个类单独执行步骤,因为他们试图分别链接以获取每个类的.o文件,然后再将其全部放在一起。我不知道为什么linux版本可以单独做这件事,而mac版本则不然。更改类名以保护无辜者。

SRCS=UMetaPlugin.cpp UDataPlugin..cpp UForPlugin.cppp UFacPlugin.cppUMTransPlugin.cpp-UPlugin.cpp

objects=$(patsubst%.cpp,%.o,$(SRCS))

全部:$(appn)

$(appn):$(对象)$(CC)$(LDFLAGS)-o$@$(对象)$(LDIBS)

.cpp.o:$(CC)$(C++FLAGS)$(INCDIRS)$&lt-o$@