我正在上Udemy课程,内容涉及用虚幻语言编写游戏以及在学习C ++的过程中。我使用CLion编写代码,但是当我尝试运行它时,出现以下错误;
/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
当我在Xcode中创建项目时,它会使用完全相同的文件进行编译并正常运行。
我的CLion工具链设置;
文件:
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';
}
答案 0 :(得分:1)
这是链接而不是编译错误。错误:
Undefined symbols for architecture x86_64:
"FBullCowGame::GetMaxTries()", referenced from:
PlayGame() in main.cpp.o
告诉您,链接器无法找到您尝试在FBullCowGame::GetMaxTries()
中使用的PlayGame()
的编译代码。
您需要将每个cpp
文件添加到必须编译的源列表中,否则将无法编译:
add_executable(BullCowGame main.cpp FBullCowGame.cpp)
答案 1 :(得分:0)
感谢@ t.niese在评论中我对问题进行了排序。在CMakeLists.txt
中,我需要添加FBullCowGame.cpp
文件。
新的 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(BullCowGame)
set(CMAKE_CXX_STANDARD 17)
add_executable(BullCowGame main.cpp FBullCowGame.cpp)