0

I'm trying to compile my C++ project from Visual Studio on OS X with g++ but I still get the following error:

Davids-MacBook-Pro-4:ChessPlusPlus davidspeck$ /usr/local/bin/g++-7 -g ChessPlusPlus.cpp -v -o ChessPlusPLus
Using built-in specs.
COLLECT_GCC=/usr/local/bin/g++-7
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin16.7.0
Configured with: ../configure --build=x86_64-apple-darwin16.7.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-7 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-checking=release --with-pkgversion='Homebrew GCC 7.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 7.2.0 (Homebrew GCC 7.2.0) 
COLLECT_GCC_OPTIONS='-g' '-v' '-o' 'ChessPlusPLus' '-mmacosx-version-min=10.12.7' '-asm_macosx_version_min=10.12' '-shared-libgcc' '-mtune=core2'
 /usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/cc1plus -quiet -v -D__DYNAMIC__ ChessPlusPlus.cpp -fPIC -feliminate-unused-debug-symbols -quiet -dumpbase ChessPlusPlus.cpp -mmacosx-version-min=10.12.7 -mtune=core2 -auxbase ChessPlusPlus -g -version -o /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//cc3fi6Qs.s
GNU C++14 (Homebrew GCC 7.2.0) version 7.2.0 (x86_64-apple-darwin16.7.0)
    compiled by GNU C version 7.2.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

warning: MPFR header version 3.1.5 differs from library version 3.1.6.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../../../../../x86_64-apple-darwin16.7.0/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../../../../../include/c++/7.2.0
 /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../../../../../include/c++/7.2.0/x86_64-apple-darwin16.7.0
 /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../../../../../include/c++/7.2.0/backward
 /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/include
 /usr/local/include
 /usr/local/Cellar/gcc/7.2.0/include
 /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/include-fixed
 /usr/include
 /System/Library/Frameworks
 /Library/Frameworks
End of search list.
GNU C++14 (Homebrew GCC 7.2.0) version 7.2.0 (x86_64-apple-darwin16.7.0)
    compiled by GNU C version 7.2.0, GMP version 6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version isl-0.18-GMP

warning: MPFR header version 3.1.5 differs from library version 3.1.6.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 3812dd5842fb4d0e2ade017d806f14bb
COLLECT_GCC_OPTIONS='-g' '-v' '-o' 'ChessPlusPLus' '-mmacosx-version-min=10.12.7'  '-shared-libgcc' '-mtune=core2'
 as -arch x86_64 -v -force_cpusubtype_ALL -mmacosx-version-min=10.12 -o /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//ccGyS8bJ.o /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//cc3fi6Qs.s
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1as -triple x86_64-apple-macosx10.12.0 -filetype obj -main-file-name cc3fi6Qs.s -target-cpu penryn -fdebug-compilation-dir /Users/davidspeck/Documents/projects/ChessPlusPlus -dwarf-debug-producer Apple LLVM version 9.0.0 (clang-900.0.38) -dwarf-version=4 -mrelocation-model pic -o /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//ccGyS8bJ.o /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//cc3fi6Qs.s
COMPILER_PATH=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/:/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/:/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/:/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/:/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/
LIBRARY_PATH=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/:/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../../
COLLECT_GCC_OPTIONS='-g' '-v' '-o' 'ChessPlusPLus' '-mmacosx-version-min=10.12.7'  '-shared-libgcc' '-mtune=core2'
 /usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin16.7.0/7.2.0/collect2 -dynamic -arch x86_64 -macosx_version_min 10.12.7 -weak_reference_mismatches non-weak -o ChessPlusPLus -L/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0 -L/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../.. /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//ccGyS8bJ.o -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v -idsym -dsym
collect2 version 7.2.0
/usr/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.12.7 -weak_reference_mismatches non-weak -o ChessPlusPLus -L/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0 -L/usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0/../../.. /var/folders/zd/ptm77qjd59n3bffmh3dtk5h00000gn/T//ccGyS8bJ.o -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
@(#)PROGRAM:ld  PROJECT:ld64-302.3.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
    /usr/local/Cellar/gcc/7.2.0/lib/gcc/7/gcc/x86_64-apple-darwin16.7.0/7.2.0
    /usr/local/Cellar/gcc/7.2.0/lib/gcc/7
    /usr/lib
    /usr/local/lib
Framework search paths:
    /Library/Frameworks/
    /System/Library/Frameworks/
Undefined symbols for architecture x86_64:
  "Match::start()", referenced from:
      _main in ccGyS8bJ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I did define Match::start() tough. That's why I don't understand the error. :(

ChessPlusPlus.cpp:

#include "stdafx.h"
#include "ChessPlusPlus.h"
#include "Match.h"

int main()
{
    Match match;
    match.start();

    system("pause");
    return 0;
}

Match.h:

#pragma once

enum class MatchState
{
    UNSTARTED,
    ONGOING,
    WON_WHITE,
    WON_BLACK,
};

class Match
{
public:
    void start();
    inline MatchState getState() { return state; }

private:
    MatchState state = MatchState::UNSTARTED;

    bool parseInput(const std::string input, int* positions);
};

Match.cpp:

#include "stdafx.h"
#include "Match.h"
#include "Board.h"
#include "ChessConsoleView.h"
#include "Piece.h"
#include <iostream>
#include <string>

void Match::start()
{
    if (state != MatchState::UNSTARTED)
    {
        return;
    }

    state = MatchState::ONGOING;
    Board board{};
    ChessConsoleView view{ board };

    Color whosTurn = Color::WHITE;

    while (state == MatchState::ONGOING)
    {
        view.print();

        int positions[4]{ 0, 0, 0, 0 };
        bool success = false;
        do
        {
            if (whosTurn == Color::WHITE)
                std::cout << "WHITEs turn:";
            else
                std::cout << "BLACKs turn:";

            std::string input;
            std::getline(std::cin, input);
            success = parseInput(input, positions);

        } while (!success);

        // positons stores start (x,y) in 0,1 and end (x,y) in 2,3
        board.movePiece(positions[0], positions[1], positions[2], positions[3]);
        whosTurn = whosTurn == Color::WHITE ? Color::BLACK : Color::WHITE;
    }
}

bool Match::parseInput(const std::string input, int* positions)
{
    if (input.length() < 5)
        return false;

    positions[0] = input[0] - 'a';
    positions[1] = input[1] - '1';
    positions[2] = input[3] - 'a';
    positions[3] = input[4] - '1';

    for (int i = 0; i < 4; i++)
    {
        if (positions[i] < 0 || positions[i] > 7)
        {
            return false;
        }
    }

    return true;
}

Full code here: https://github.com/Saduras/ChessPlusPlus

Thanks for your help!

David

David Speck
  • 438
  • 4
  • 12

2 Answers2

2

I can't tell completely from your output, but did you compile all the source files together?

You can't just run "g++ -g ChessPlusPlus.cpp" by itself, because that is only one source file. Try listing all of the source files like so:

g++ -g ChessPlusPlus.cpp Match.cpp etc...

1

/usr/local/bin/g++-7 -g ChessPlusPlus.cpp -v -o ChessPlusPLus

You've told GCC to create the program ChessPlusPLus from the file ChessPlusPlus.cpp, so it complains that Match::start is not defined. That's correct. You defined it in the file Match.cpp but you haven't told the compiler anything about that file, so how is it supposed to find the function? It's not going to simply open every file on your hard drive looking for the definition of that function.

You need to link all the files together:

/usr/local/bin/g++-7 -g ChessPlusPlus.cpp Match.cpp o ChessPlusPlus

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521