mac build有未定义的符号,其中linux build没有相同的代码

时间:2017-01-27 18:01:17

标签: c++ gcc undefined

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

" MyClassNameSpecific1 :: MyClassNameSpecific1(int,int,int,className :: class1 const&,className :: class2 const&,int,int)",引自:       MyOtherClassName.o中的MyOtherClassName :: 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
}

我不确定是什么原因可能会导致undef在一个而不是另一个。有没有人意识到这种情况的gcc有什么不同?我的makefile包含所有:MyClassName然后是其他类。我发现MyClassNameSpecific2的.h和.cpp文件之间的参数签名没有任何差异。

我尝试确保在.h和.cpp文件中都使用了完整的className :: class1,但它仍然具有相同的undef。此外,我尝试添加类MyClassNameSpecific2,它只在MyClassName.h文件的顶部有类MyClassNameSpecific1,但它没有改变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;

1 个答案:

答案 0 :(得分:0)

修复此问题的方法是更改​​makefile,使其在一个步骤中构建所有内容,而不是为每个类构建单独的步骤,因为他们尝试单独链接以获取每个类的.o文件,然后再将其全部放入最后在一起。我不确定为什么linux版本可以单独执行此操作并且mac版本不是。更改了班级名称以保护无辜者。

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

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

全部:$(appn)

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

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