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

标签 c++ xcode macos clion

我正在开设 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/

相关文章:

macos - 无法连接到 MongoDB 错误 :61

Swift OSX - Parse.com - 保存新用户而不进行更改

c++ - 什么是最适合 C++ 源代码的编码

iphone - NSHTTPCookieStorage 是否跨应用程序持久存在?

ios - 将 .sks 连接到 skscene.h

ios - 将cocoapods添加到现有项目中

macos - dtrace 中的 "dynamic variable drops with non-empty dirty list"是什么意思?

c++ - 轮廓字体上下颠倒 - glOrtho() 设置不正确

c++ - 如何让Halide使用滑动窗口优化?

c++ - 建立时间: Visual Studio 2015-2017 build very slow [closed]