CLion не компилируется, когда Xcode делает? - PullRequest
0 голосов
/ 29 июня 2018

Я делаю курс удеми по написанию игр на Unreal и изучаю 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, он компилируется и работает нормально, используя те же самые файлы.

Настройка My CLion Toolchains;

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

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

Это ссылка, а не ошибка компиляции. Ошибка:

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)
0 голосов
/ 29 июня 2018

Благодаря @ 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)
...