• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

[클래스 및 분할 컴파일 질문]undefined reference to

23.08.01 09:37 작성 조회수 761

0

클래스를 헤더 파일(stock.h)와 func.cpp 파일에 분할해서 작성 후 컴파일 했는데

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x74): undefined reference to `Stock::Stock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, float)'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0xac): undefined reference to `Stock::Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0xfa): undefined reference to `Stock::show()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x107): undefined reference to `Stock::show()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x162): undefined reference to `Stock::Stock(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, float)'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x185): undefined reference to `Stock::~Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x1ba): undefined reference to `Stock::show()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x1cc): undefined reference to `Stock::~Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x1d9): undefined reference to `Stock::~Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x20d): undefined reference to `Stock::~Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x23a): undefined reference to `Stock::~Stock()'

C:\Users\PC-202~1\AppData\Local\Temp\ccEPMb0b.o:practice.cpp:(.text+0x24b): undefined reference to `Stock::~Stock()'

collect2.exe: error: ld returned 1 exit status

이런 오류가 발생했습니다.

인터넷을 찾아봐도 잘 모르겠어서 원인이 무엇인지 질문드립니다.

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.08.03

안녕하세요, 인프런 AI 인턴이에요. 클래스를 헤더 파일과 cpp 파일에 분할해서 작성한 후 컴파일하였을 때 “undefined reference to” 에러가 발생한 것 같네요.

이 에러는 일반적으로 링크 과정에서 발생하는 오류로, 컴파일러가 함수/변수의 선언을 찾을 수 없다는 의미입니다. 링크 과정에서 호출한 함수 또는 변수의 정의가 없는 경우에 발생할 수 있습니다.

이러한 경우, 일반적으로 다음과 같은 해결 방법을 시도해볼 수 있습니다:
1. 클래스의 메서드 정의를 포함하는 cpp 파일을 컴파일에 포함시키는지 확인해보세요. 컴파일 명령어에 모든 cpp 파일을 추가하거나, 필요한 cpp 파일만 컴파일하는지 확인해보세요.
2. 클래스의 메서드 정의를 제대로 작성했는지 확인해보세요. 함수 이름, 매개변수, 반환값 등이 정확하게 일치하는지 확인해보세요.
3. 헤더 파일의 include 가드와 클래스의 선언/정의가 잘 되어있는지 확인해보세요. 중복된 include, 잘못된 헤더 파일의 사용, 오타 등으로 인해 이러한 오류가 발생할 수 있습니다.

만약 위의 해결 방법으로 문제가 해결되지 않는다면, 보다 자세한 정보가 필요할 것 같습니다. 코드와 컴파일 명령어를 함께 공유해주시면 더욱 도움이 될 수 있을 것 같아요. 감사합니다.