我在Mac上编写了一个玩具程序,在构建编译器时,出现以下错误ld: symbol(s) not found for architecture x86_64。在Mac上这似乎是一个常见错误,但始终是由于使用gcc构建cpp程序引起的。

g++ -v -o assignment_operator_main assignment_operator_main.o
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -o assignment_operator_main assignment_operator_main.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "CMyString::CMyString(char*)", referenced from:
      _main in assignment_operator_main.o
  "CMyString::CMyString(CMyString const&)", referenced from:
      _main in assignment_operator_main.o
  "CMyString::~CMyString()", referenced from:
      _main in assignment_operator_main.o
  "CMyString::operator=(CMyString const&)", referenced from:
      _main in assignment_operator_main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [assignment_operator_main] Error 1

下面是我的程序。
assignment_operator.h
#ifndef ASSIGNMENT_OPERATOR_H
#define ASSIGNMENT_OPERATOR_H
class CMyString{
    public:
        CMyString( char* pData = NULL );
        CMyString(const CMyString& str );
        CMyString& operator=(const CMyString& str);
        char* get_m_pData();
        ~CMyString(void);

    private:
        char * m_pData;
};
#endif

assignment_operator.cpp
#include "assignment_operator.h"

CMyString::CMyString(char * pData = NULL ){
    m_pData= pData
}
CMyString::CMyString(const CMyString& str){
    m_pData=new char[strlen(str.get_m_pData())+1]
    if( m_pData != NULL )
        strcpy(m_pData, str.get_m_pData())
}

CMyString& CMyString::operator=(const CMyString& str){
    if( this != &str ){
        char* tmp_m_pData;
        CMyString tmp(str);
        tmp_m_pData=tmp.m_pData;
        tmp.m_pData=m_pData;
        m_pData=tmp_m_pData;
    }
    return *this;
}

char* CMyString::get_m_pData(){
    return m_pData;
}
CMyString::~CMyString(void){
    if( m_pData != NULL )
        delete [] m_pData
}

生成文件
assignment_operator_main : assignment_operator_main.o
        g++ -v -o assignment_operator_main assignment_operator_main.o
assignment_operator_main.o : assignment_operator_main.cpp assignment_operator.h
        g++  -v -c assignment_operator_main.cpp
assignment_operator.o : assignment_operator.cpp assignment_operator.h
        g++  -v -c assignment_operator.cpp
clean:
        rm assignment_operator_main.o assignment_operator.o

最佳答案

您的程序需要两个目标文件:

assignment_operator_main : assignment_operator_main.o assignment_operator.o
    g++ -v -o assignment_operator_main assignment_operator_main.o assignment_operator.o

关于c++ - ld:在Mac上找不到架构x86_64的符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32631349/

10-13 06:34