首页 > 解决方案 > 使用 mingw-32 的 Makefile C++ 问题

问题描述

我对 C++ 很陌生。我在使用 makefile 编译我的 C++ 项目时遇到了一些麻烦。该项目包括:main.cpp、position.cpp、movegen.cpp、move.cpp 和它们各自的头文件。我正在使用 Windows 和 mingw-64。

这是我写的makefile:

CXX = g++
CXXFLAGS = -Wall -g

all: main

main: main.o position.o movegen.o move.o
    $(CXX) $(CXXFLAGS) -o main main.o position.o movegen.o move.o

main.o: main.cpp position.h movegen.h move.h
    $(CXX) $(CXXFLAGS) -c main.cpp

position.o: position.cpp position.h move.h
    $(CXX) $(CXXFLAGS) -c position.cpp

movegen.o: movegen.cpp movegen.h position.h move.h
    $(CXX) $(CXXFLAGS) -c movegen.cpp

move.o: move.cpp move.h
    $(CXX) $(CXXFLAGS) -c move.cpp

是一个非常混乱的依赖关系图,每个文件都依赖于在其下方画一条线的文件,例如 movegen 取决于位置和移动标题。

当我尝试在项目目录中运行 mingw-64_make.exe 命令时,所有目标文件都已成功创建,但 main.exe 没有。这是控制台输出:

C:\Users\Hutch\pepegaengine>mingw32-make.exe
g++ -Wall -g -c main.cpp
main.cpp: In static member function 'static void TestEngine::displayBB(U64)':
main.cpp:22:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'unsigned int'} [-Wsign-compare]
                 for (int j = 0; j < eightBit.length(); j++) {
                                 ~~^~~~~~~~~~~~~~~~~~~
g++ -Wall -g -c position.cpp
position.cpp: In member function 'void Position::unmakeMove(cmove::Move)':
position.cpp:771:30: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
             } else if (flags = capture) {
                        ~~~~~~^~~~~~~~~
position.cpp:766:17: warning: unused variable 'toPiece' [-Wunused-variable]
             int toPiece = move.getToPiece();
                 ^~~~~~~
g++ -Wall -g -c movegen.cpp
movegen.cpp: In function 'cmove::Move* cmovegen::legalMoveGenerator(cposition::Position&, cposition::enumColour, cmove::Move*)':
movegen.cpp:9:13: warning: unused variable 'enemyPawns' [-Wunused-variable]
         U64 enemyPawns = pos.getPieceSet(blackPawns - colourMove);
             ^~~~~~~~~~
g++ -Wall -g -c move.cpp
g++ -Wall -g -o main main.o position.o movegen.o move.o
main.o: In function `main':
C:\Users\Hutch\pepegaengine/main.cpp:34: undefined reference to `cposition::Position::Position(unsigned long long*)'
C:\Users\Hutch\pepegaengine/main.cpp:35: undefined reference to `cposition::Position::initializeStartPosition()'
C:\Users\Hutch\pepegaengine/main.cpp:39: undefined reference to `cposition::Position::getEmptyBB()'
C:\Users\Hutch\pepegaengine/main.cpp:42: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/main.cpp:108: undefined reference to `cmove::Move::Move()'
C:\Users\Hutch\pepegaengine/main.cpp:109: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/main.cpp:110: undefined reference to `cposition::Position::makeMove(cmove::Move)'
C:\Users\Hutch\pepegaengine/main.cpp:112: undefined reference to `cmovegen::legalMoveGenerator(cposition::enumColour, cmove::Move*)'
C:\Users\Hutch\pepegaengine/main.cpp:115: undefined reference to `cmove::Move::getFrom()'
C:\Users\Hutch\pepegaengine/main.cpp:116: undefined reference to `cmove::Move::getTo()'
C:\Users\Hutch\pepegaengine/main.cpp:121: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/main.cpp:122: undefined reference to `cposition::Position::makeMove(cmove::Move)'
C:\Users\Hutch\pepegaengine/main.cpp:124: undefined reference to `cmovegen::legalMoveGenerator(cposition::enumColour, cmove::Move*)'
C:\Users\Hutch\pepegaengine/main.cpp:127: undefined reference to `cmove::Move::getFrom()'
C:\Users\Hutch\pepegaengine/main.cpp:128: undefined reference to `cmove::Move::getTo()'
movegen.o: In function `ZN8cmovegen18legalMoveGeneratorERN9cposition8PositionENS0_10enumColourEPN5cmove4MoveE':
C:\Users\Hutch\pepegaengine/movegen.cpp:8: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:9: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:22: undefined reference to `cposition::Position::getEmptyBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:22: undefined reference to `cposition::Position::singlePushTargets(unsigned long long, unsigned long long, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:23: undefined reference to `cposition::Position::getEmptyBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:23: undefined reference to `cposition::Position::doublePushTargets(unsigned long long, unsigned long long, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:24: undefined reference to `cposition::Position::pawnCaptures(unsigned long long, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:25: undefined reference to `cposition::Position::pawnAllAttacks(unsigned long long, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:25: undefined reference to `cposition::Position::getEnPassantTarget()'
C:\Users\Hutch\pepegaengine/movegen.cpp:28: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:47: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:48: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:49: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:50: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
movegen.o:C:\Users\Hutch\pepegaengine/movegen.cpp:56: more undefined references to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)' follow
movegen.o: In function `ZN8cmovegen18legalMoveGeneratorERN9cposition8PositionENS0_10enumColourEPN5cmove4MoveE':
C:\Users\Hutch\pepegaengine/movegen.cpp:72: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:76: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:77: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:78: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:79: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:85: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:95: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:102: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:102: undefined reference to `cposition::Position::rookCaptures(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:103: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:103: undefined reference to `cposition::Position::rookQuietMoves(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:108: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:114: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:114: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:121: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:126: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:126: undefined reference to `cposition::Position::queenCaptures(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:127: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:127: undefined reference to `cposition::Position::queenQuietMoves(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:130: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:136: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:136: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:144: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:149: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:149: undefined reference to `cposition::Position::bishopCaptures(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:150: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:150: undefined reference to `cposition::Position::bishopQuietMoves(unsigned long long, int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:153: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:159: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:159: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:167: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:173: undefined reference to `cposition::Position::singleKnightQuietMoves(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:174: undefined reference to `cposition::Position::singleKnightCaptures(int, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:177: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:183: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:183: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:191: undefined reference to `cposition::Position::getPieceSet(int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:192: undefined reference to `cposition::Position::kingQuietMoves(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:193: undefined reference to `cposition::Position::kingCaptures(unsigned long long, cposition::enumColour)'
C:\Users\Hutch\pepegaengine/movegen.cpp:196: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:197: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:197: undefined reference to `cposition::Position::isAttackedSquare(unsigned long long, int, int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:205: undefined reference to `cposition::Position::pieceOnSquare(unsigned long long)'
C:\Users\Hutch\pepegaengine/movegen.cpp:205: undefined reference to `cmove::Move::Move(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)'
C:\Users\Hutch\pepegaengine/movegen.cpp:206: undefined reference to `cposition::Position::getOccupiedBB()'
C:\Users\Hutch\pepegaengine/movegen.cpp:206: undefined reference to `cposition::Position::isAttackedSquare(unsigned long long, int, int)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [makefile:12: main] Error 1

主.cpp:

#include <iostream>
#include <iostream>
#include <bitset>
#include "position.h"
#include "movegen.h"
#include "move.h"
using namespace cmove;
using namespace cposition;
using namespace cmovegen;
using namespace std;

int main() {
    
    U64 pieceBB[14];
    
    Position pos(pieceBB);
    pos.initializeStartPosition();
    
    cout << "empty: " << endl;
    TestEngine::displayBB(pos.getEmptyBB());

    cout << "occupied: " << endl;
    TestEngine::displayBB(pos.getOccupiedBB());

    Move moves[150];
    Move d4(11,27, quietMove, whitePawns, 0);
    pos.makeMove(d4);

    legalMoveGenerator(white, moves);
    ...

位置.cpp:

#include<string>
#include "position.h"
#define C64(constantU64) constantU64##ULL
typedef unsigned long long U64;
using namespace std;
using namespace cmove;
using namespace cposition;

class Position {

    U64 pieceBB[14];
    U64 emptyBB;
    U64 occupiedBB;
    U64 enPassantTarget;
    bool castleRights[4];

    U64 arrPawnAttacks[2][64];
    U64 arrKnightAttacks[64];
    U64 arrKingAttacks[64];

    public:    
        U64 getEmptyBB() {
            return emptyBB;
        }

        U64 getOccupiedBB() {
            return occupiedBB;
        }

        U64 getPieceSet(int piece) {
            return pieceBB[piece];
        }
        ...

位置.h:

#ifndef POSITION
#define POSITION

#define C64(constantU64) constantU64##ULL

typedef unsigned long long U64;
#include "move.h"
namespace cposition {
    typedef unsigned long long U64;

    enum enumPiece {
        whitePieces,
        blackPieces,
        whitePawns,
        blackPawns,
        whiteRooks,
        blackRooks,
        whiteKnights,
        blackKnights,
        whiteBishops,
        blackBishops,
        whiteQueens,
        blackQueens,
        whiteKing,
        blackKing,                
    };

    enum enumSquare {
        a1, b1, c1, d1, e1, f1, g1, h1,
        a2, b2, c2, d2, e2, f2, g2, h2,
        a3, b3, c3, d3, e3, f3, g3, h3,
        a4, b4, c4, d4, e4, f4, g4, h4,
        a5, b5, c5, d5, e5, f5, g5, h5,
        a6, b6, c6, d6, e6, f6, g6, h6,
        a7, b7, c7, d7, e7, f7, g7, h7,
        a8, b8, c8, d8, e8, f8, g8, h8
    };

    enum enumDirection {
        n,
        ne,
        e,
        se,
        s,
        sw,
        w,
        nw
    };

    enum enumColour {
        white,
        black
    };

    enum enumFlag {
        quietMove,
        doublePawnPush,
        kingCastle,
        queenCastle,
        capture,
        epCapture,
        knightPromotion,
        bishopPromotion,
        rookPromotion,
        queenPromotion,
        knightPromoCapture,
        bishopPromoCapture,
        rookPromoCapture,
        queenPromoCapture
    };

    enum enumCastleRights {
        whiteKingsideCastleRights,
        blackKingsideCastleRights,
        whiteQueensideCastleRights,
        blackQueensideCastleRights
    };

    class Position {
        U64 pieceBB[14];
        U64 emptyBB;
        U64 occupiedBB;
        U64 enPassantTarget;
        bool castleRights[4];

        U64 arrPawnAttacks[2][64];
        U64 arrKnightAttacks[64];
        U64 arrKingAttacks[64];

        

        public:    

            U64 getEmptyBB();

            U64 getOccupiedBB();

            U64 getPieceSet(int piece);

            U64 getEnPassantTarget();
            ...

movegen.cpp:

#include <math.h> 
#include "movegen.h"
using namespace cmove;
using namespace cposition;

namespace cmovegen {
    cmove::Move * legalMoveGenerator(Position& pos, enumColour colourMove, Move * moves) {
        U64 pawns = pos.getPieceSet(whitePawns + colourMove);
        U64 enemyPawns = pos.getPieceSet(blackPawns - colourMove);
        U64 pawnPush = 0;
        U64 pawnDoublePush = 0;
        U64 pawnCap = 0;
        U64 pawnEP = 0;

        //TestEngine::displayBB(pawns);
        //TestEngine::displayBB(enemyPawns);
        

        while (pawns) {
            U64 currentPawn = pawns & -pawns;
            
            pawnPush = pos.singlePushTargets(currentPawn, pos.getEmptyBB(), colourMove);
            pawnDoublePush = pos.doublePushTargets(currentPawn, pos.getEmptyBB(), colourMove);
            pawnCap = pos.pawnCaptures(currentPawn, colourMove);
            pawnEP = pos.pawnAllAttacks(currentPawn, colourMove) & pos.getEnPassantTarget();
            
            if (pawnEP) {
                Move newMove (log2(currentPawn), log2(pawnEP), epCapture, (whitePawns + colourMove), (blackPawns - colourMove));
                *moves++ = newMove;
            }
            ...

movegen.h:

#ifndef MOVEGEN
#define MOVEGEN
#include "position.h"

namespace cmovegen {
    cmove::Move * legalMoveGenerator(cposition::enumColour colourMove, cmove::Move * moves);
}
#endif

移动.cpp:

#include "move.h"
using namespace cmove;

class Move {
    
    unsigned int move24 = 0;

    public:
        Move() {
            move24 = 0;
        }

        Move(unsigned int from, unsigned int to, unsigned int flags, unsigned int fromPiece, unsigned int toPiece) {
            move24 = ((flags & 0xf) << 20) | ((from & 0x3f) << 14) | ((to & 0x3f) << 8) | ((fromPiece & 0xf) << 4) | (toPiece & 0xf);
        }

        unsigned int getFrom() {
            return (move24 >> 14) & 0x3f;
        }

        unsigned int getTo() {
            return (move24 >> 8) & 0x3f;
        }

        unsigned int getFlags() {
            return (move24 >> 20) & 0xf;
        }

        unsigned int getFromPiece() {
            return (move24 >> 4) & 0xf;
        }

        unsigned int getToPiece() {
            return move24 & 0xf;
        }

        void setFrom(unsigned int from) {
            move24 &= 0xf03fff;
            move24 |= ((from & 0x3f) << 14);
        }

        void setTo(unsigned int to) {
            move24 &= 0xffc0ff;
            move24 |= ((to & 0x3f) << 8);
        }

        void setFlags(unsigned int flags) {
            move24 &= 0x0fffff;
            move24 |= ((flags & 0xf) << 20);
        }

        void setFromPiece(unsigned int fromPiece) {
            move24 &= 0xffff0f;
            move24 |= ((fromPiece & 0xf) << 4); 
        }

        void setToPiece(unsigned int toPiece) {
            move24 &= 0xfffff0;
            move24 |= (toPiece & 0xf); 
        }
};

移动.h:

#ifndef MOVE
#define MOVE
namespace cmove {
    class Move {
        
        unsigned int move24;

        public:
            Move();

            Move(unsigned int from, unsigned int to, unsigned int flags, unsigned int fromPiece, unsigned int toPiece);

            unsigned int getFrom();

            unsigned int getTo();

            unsigned int getFlags();

            unsigned int getFromPiece();

            unsigned int getToPiece();

            void setFrom(unsigned int from);

            void setTo(unsigned int to);

            void setFlags(unsigned int flags);

            void setFromPiece(unsigned int fromPiece);

            void setToPiece(unsigned int toPiece);
    };
}
#endif

标签: c++windowsvisual-studio-codemakefilemingw32

解决方案


推荐阅读