2012-02-14 2 views
53

Mac에 C++ 코드를 작성하고 있습니다. 내 코드가 잘못 또는 내가 엑스 코드에 추가 플래그를 추가해야하는 경우 확실하지정적 변수 링크 오류

Undefined symbols for architecture i386: "Log::theString", referenced from: Log::method(std::string) in libTest.a(Log.o) ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)

을? 컴파일 할 때이 오류 이유는 무엇입니까. 내 현재 XCode 구성은 '정적 라이브러리'프로젝트의 기본 구성입니다.

내 코드 :

Log.h ------------

#include <iostream> 
#include <string> 

using namespace std; 

class Log{ 
public: 
    static void method(string arg); 
private: 
    static string theString ; 
}; 

Log.cpp ----

#include "Log.h" 
#include <ostream> 

void Log::method(string arg){ 
    theString = "hola"; 
    cout << theString << endl; 
} 

난 이 방법으로 테스트 코드에서 '메소드'를 호출하십시오. 'Log :: method ("asd") :'

감사합니다.

+4

string Log::theString; 

를 포함합니다. 참조 된 다른 질문은 본질적으로 매우 일반적이며 내 Mac 특정 문제를 해결하는 데 도움이되지 않았을 것입니다. – Adam

답변

65

cpp 파일에 통계를 정의해야합니다.

Log.cpp는

#include "Log.h" 
#include <ostream> 

string Log::theString; // <---- define static here 

void Log::method(string arg){ 
    theString = "hola"; 
    cout << theString << endl; 
} 

또한 헤더에서 using namespace std;을 제거해야합니다. 아직도 할 수있는 동안 습관에 빠져라. 그러면 머리말을 포함하여 어디서나 std으로 전역 네임 스페이스가 오염됩니다.

+0

* 정의 * 대신 *를 초기화합니다 *, 아니오 (그냥 묻는 중)? – Vyktor

+0

@Vyktor 나는 둘 다 동의한다고 생각합니다. –

+9

아마도 더 나은 용어는 문자열에 공간을 할당하는 것입니다. – btown

12

static string theString;을 신고했지만 정의하지 않았습니다.

내가이 중복 질문입니다 동의하여 cpp 파일