在OS X WTIH G 上编译:架构X86_64的未定义符号



我正在尝试从OS X上使用G 的Visual Studio编译我的C 项目,但我仍然会收到以下错误:

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

我确实定义了Match::start()强硬。这就是为什么我不明白错误的原因。:(

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

此处的完整代码:https://github.com/saduras/chessplusplus

感谢您的帮助!

大卫

我无法完全从您的输出中分辨出来,但是您是否将所有源文件编译在一起?

您本身不能只运行" G -G Chessplusplus.cpp",因为那只是一个源文件。尝试列出所有源文件,例如:

g -g chessplusplus.cpp match.cpp等...

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

您已经告诉GCC从文件ChessPlusPlus.cpp创建程序ChessPlusPLus,因此它抱怨未定义Match::start。没错。您在文件Match.cpp中定义了它,但是您尚未告诉编译器有关该文件的任何内容,那么它应该如何找到该函数?它不会简单地打开硬盘驱动器上的每个文件,以查找该功能的定义。

您需要将所有文件链接在一起:

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

最新更新