c++ - mac build 有 undefined symbol ,而 linux build 没有相同的代码

标签 c++ gcc undefined

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

“MyClassNameSpecific1::MyClassNameSpecific1(int, int, int, className::class1 const&, className::class2 const&, int, int)”,引用自: MyOtherClassName::mContainer() 在 MyOtherClassName.o 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
}

我不确定是什么导致了其中一个而不是另一个的 undef。有没有人知道 gcc 在这种情况下的区别?我的 makefile 包含所有内容:MyClassName,然后是其他类。我没有看到 MyClassNameSpecific2 的 .h 和 .cpp 文件之间的参数签名有任何差异。

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

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

添加的生成文件:

.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;
}

我的其他类名.h:

类名::类1 m_dType; 类名::类2 m_dLayout;

最佳答案

解决这个问题的方法是更改​​ makefile,以便它在一个步骤中构建所有内容,而不是为每个类单独构建步骤,因为他们试图分别链接以获取每个类的 .o 文件,然后再将其全部放入最后在一起。我不确定为什么 linux 构建可以单独执行此操作而 mac 构建却不行。更改类名以保护无辜者。

SRCS=UMetaPlugin.cpp UDataPlugin.cpp UForPlugin.cpp UFacPlugin.cpp UMTransPlugin.cpp UPlugin.cpp

对象=$(patsubst %.cpp,%.o,$(SRCS))

全部:$(appn)

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

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

关于c++ - mac build 有 undefined symbol ,而 linux build 没有相同的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41900254/

相关文章:

c++ - 在 C++ 应用程序中使用 libgrib2c,链接器错误 "Undefined reference to..."

Internet Explorer 中未定义 Javascript 对象

python-3.x - 尽管 xxx 是全局定义的,但预期的 NameError : name 'xxx' is not defined

c++ - 如何从静态方法中获取类名?

c++ - 基于CLRS的合并排序C++上的算法简介(带反转计数)

c - 如何指示可以使用内联 ASM 参数*指向*的内存?

xcode - 未找到 omp.h,OS X Yosemite 未使用最新的 gcc 版本

c++ - 在 .cu 中声明的 .h 中定义的函数

C++:是否可以通过 fork 进程共享指针?

c++ - 如何正确返回带有成员变量的重载运算符++?