我在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
答案 0 :(得分:1)
您的程序需要两个目标文件:
assignment_operator_main : assignment_operator_main.o assignment_operator.o
g++ -v -o assignment_operator_main assignment_operator_main.o assignment_operator.o