Reputation: 163
I am doing a Udemy course on writing games in Unreal and learning C++ while you do. I was using CLion to write the code but when I try run it I get the following error;
/usr/local/bin/cmake --build /Users/penkin/Sandbox/penkin-unreal/Section_02/BullCowGame/cmake-build-debug --target BullCowGame -- -j 4
[ 50%] Linking CXX executable BullCowGame
Undefined symbols for architecture x86_64:
"FBullCowGame::GetMaxTries()", referenced from:
PlayGame() in main.cpp.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[3]: *** [BullCowGame] Error 1
make[2]: *** [CMakeFiles/BullCowGame.dir/all] Error 2
make[1]: *** [CMakeFiles/BullCowGame.dir/rule] Error 2
make: *** [BullCowGame] Error 2
When I create the project in Xcode it compiles and runs fine using the exact same files.
My CLion Toolchains setup;
Files:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(BullCowGame)
set(CMAKE_CXX_STANDARD 17)
add_executable(BullCowGame main.cpp)
FBullCowGame.h
#include <string>
class FBullCowGame {
public:
void Reset();
int GetMaxTries();
int GetCurrentTry();
bool IsGameWon();
bool CheckGuessValidity(std::string);
private:
int MyCurrentTry = 1;
int MyMaxTries = 5;
};
FBullCowGame.cpp
#include "FBullCowGame.h"
void FBullCowGame::Reset() {}
int FBullCowGame::GetMaxTries() {
return MyMaxTries;
}
int FBullCowGame::GetCurrentTry() {
return MyCurrentTry;
}
bool FBullCowGame::IsGameWon() {
return false;
}
bool FBullCowGame::CheckGuessValidity(std::string) {
return false;
}
main.cpp
#include <iostream>
#include <string>
#include "FBullCowGame.h"
void PrintIntro();
std::string GetGuess();
void PlayGame();
bool AskToPlayAgain();
// --
// Application's entry point.
// --
int main() {
do {
PrintIntro();
PlayGame();
}
while (AskToPlayAgain());
return 0; // Exit application.
}
// --
// Prints the game's intro text.
// --
void PrintIntro() {
constexpr int WORD_LENGTH = 5;
std::cout << "Welcome to Bulls & Cows, a fun word game." << std::endl;
std::cout << "Can you guess the " << WORD_LENGTH << " letter isogram I'm thinking of?" << std::endl;
}
// --
// Gets the string guessed by the user and returns that string.
// --
std::string GetGuess() {
std::string Guess;
std::cout << std::endl << "Enter your guess: ";
getline(std::cin, Guess);
return Guess;
}
// --
// Runs through the game logic.
// --
void PlayGame() {
std::string Guess;
FBullCowGame BCGame;
int MaxTries = BCGame.GetMaxTries();
for (int count = 1; count <= MaxTries; count++) {
Guess = GetGuess();
std::cout << "You guessed \"" << Guess << "\"" << std::endl;
}
}
// --
// Asks the user if they would like to play the game again.
// --
bool AskToPlayAgain() {
std::string Response;
std::cout << std::endl << "Would you like to play again (y/n)? ";
getline(std::cin, Response);
return std::tolower(Response[0]) == 'y';
}
Upvotes: 2
Views: 395
Reputation: 40842
It is a linking and not a comiling error. The error:
Undefined symbols for architecture x86_64:
"FBullCowGame::GetMaxTries()", referenced from:
PlayGame() in main.cpp.o
Tells you that the linker was not able to find compiled code for FBullCowGame::GetMaxTries()
that you try to use in PlayGame()
.
You need to add each cpp
file you have to the list of source that has to be compiled, otherwise it won't be compiled:
add_executable(BullCowGame main.cpp FBullCowGame.cpp)
Upvotes: 1
Reputation: 163
Thanks to @t.niese in the comments I've sorted the problem. In the CMakeLists.txt
I need to add my FBullCowGame.cpp
file.
New CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(BullCowGame)
set(CMAKE_CXX_STANDARD 17)
add_executable(BullCowGame main.cpp FBullCowGame.cpp)
Upvotes: 0