gpt4 book ai didi

c++ - Xcode 编译时 CLion 不编译?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:05:29 25 4
gpt4 key购买 nike

我正在开设 Udemy 类(class),内容是关于在虚幻引擎中编写游戏并在您学习 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 工具链设置;

enter image description here

文件:

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';
}

最佳答案

这是链接错误,不是编译错误。错误:

Undefined symbols for architecture x86_64:
"FBullCowGame::GetMaxTries()", referenced from:
PlayGame() in main.cpp.o

告诉您链接器无法找到您尝试在 PlayGame() 中使用的 FBullCowGame::GetMaxTries() 的编译代码。

您需要将您拥有的每个cpp 文件添加到必须编译的源列表中,否则将无法编译:

add_executable(BullCowGame main.cpp FBullCowGame.cpp)

关于c++ - Xcode 编译时 CLion 不编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51104191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com