Issues with Makefile C++ using mingw-32 - c++
I'm very new to C++. I am having some trouble with using a makefile to compile my C++ project. The project consists of: main.cpp, position.cpp, movegen.cpp, move.cpp and their respective headers. I am using Windows and mingw-64.
Here is the makefile I have written:
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
Here is a very messy dependency diagram, each file is dependent on the one with a line drawn below it downwards e.g. movegen depends on position and move headers.
When I try running the mingw-64_make.exe command in the directory of the project, all of the object files are created successfully, but main.exe is not.
This is the console output:
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
main.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);
...
position.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];
}
...
position.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
move.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);
}
};
move.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
Related
Return dim3 variable from function in CUDA C++
I would like to return a dim3 object from a function. The specific code is: dim3 getGridBasedOnBlockSize(int width, int height, int block_size) { int gridX = (int)ceil((float)width / block_size); int gridY = (int)ceil((float)height / block_size); return dim3(gridX, gridY); But while compiling an error occurs: error: expected expression before ‘dim3’ return dim3(gridX, gridY); ^ So I changed it a little bit to this: dim3 getGridBasedOnBlockSize(int width, int height, int block_size) { int gridX = (int)ceil((float)width / block_size); int gridY = (int)ceil((float)height / block_size); dim3 gridXY(gridX, gridY); return gridXY; But now it says: error: incompatible types when returning type ‘dim3 (*)() {aka struct dim3 (*)()}’ but ‘dim3 {aka struct dim3}’ was expected return gridXY; ^ Could you help me? What does this mean, how can I fix this in order to be compiled properly with nvcc? Thank you! The complete code for this .c file is: #include <unistd.h> #include <stdio.h> #include <math.h> #include <cuda.h> #include <cuda_runtime_api.h> #define WARP_SIZE 16 #define DEBUG false float *_copyHostDevice(float *src, int src_size) { float *src_d; cudaMalloc((void**)&src_d, sizeof(float) * src_size); cudaMemcpy(src_d, src, sizeof(float) * src_size, cudaMemcpyHostToDevice); return src_d; } float *_copyDeviceHost(float *src, int src_size, float *dst) { float *target; if (dst == NULL) { target = (float*)malloc(sizeof(float) * src_size); } else { target = dst; } cudaMemcpy(target, src, sizeof(float) * src_size, cudaMemcpyDeviceToHost); return target; } typedef struct { int x; int y; } GlobalDim; __device__ GlobalDim getGlobalDim(dim3 blockDim, dim3 blockIdx, dim3 threadIdx) { GlobalDim gd; gd.x = blockDim.x * blockIdx.x + threadIdx.x; gd.y = blockDim.y * blockIdx.y + threadIdx.y; return gd; } dim3 getGridBasedOnBlockSize(int width, int height, int block_size) { int gridX = (int)ceil((float)width / block_size); int gridY = (int)ceil((float)height / block_size); dim3 gridXY(gridX, gridY); return gridXY; } void _sleep(int n) { usleep(n*1000000); } void drawMatrix(float *m, int width, int height) { for (int i=0; i < height; i++) { for (int j=0; j < width; j++) { printf("%f ", m[i * width + j]); } printf("\n"); } } And the compile command and result: $ nvcc -Wno-deprecated-gpu-targets -o project4 nn.cu parallel.cu utils.c utils.c: In function ‘getGridBasedOnBlockSize’: utils.c:48:5: warning: parameter names (without types) in function declaration dim3 gridXY(gridX, gridY); ^ utils.c:49:12: error: incompatible types when returning type ‘dim3 (*)() {aka struct dim3 (*)()}’ but ‘dim3 {aka struct dim3}’ was expected return gridXY; ^ And for the dim3(...) case, it shows: nvcc -Wno-deprecated-gpu-targets -o project4 nn.cu parallel.cu utils.c utils.c: In function ‘getGridBasedOnBlockSize’: utils.c:48:12: error: expected expression before ‘dim3’ return dim3(gridX, gridY); ^ Edit1: #Zindarod Using dim3 gridXY; gridXY.x = gridX; gridXY.y = gridY; return gridXY; instead of dim3 gridXY(gridX, gridY); return gridXY; as you suggested, unfortunately throws this error: $ nvcc -Wno-deprecated-gpu-targets -o project4 nn.cu parallel.cu utils.c /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `_copyHostDevice': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x16): multiple definition of `_copyHostDevice' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x16): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `_copyDeviceHost': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x8c): multiple definition of `_copyDeviceHost' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x8c): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `getGlobalDim': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0xed): multiple definition of `getGlobalDim' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0xed): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `getGridBasedOnBlockSize': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x120): multiple definition of `getGridBasedOnBlockSize' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x120): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `_sleep': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x1de): multiple definition of `_sleep' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x1de): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `drawMatrix': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x1fc): multiple definition of `drawMatrix' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x1fc): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `setWeightsForLayers(float*, float*, float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x27b): multiple definition of `setWeightsForLayers(float*, float*, float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x27b): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `updateWeightsCUDA(float*, float*, float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x885): multiple definition of `updateWeightsCUDA(float*, float*, float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x12b2): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `update_layer(float*, float*, int, int, float*)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x3fa): multiple definition of `update_layer(float*, float*, int, int, float*)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x3fa): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `mapStepCUDA(float*, float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x9ad): multiple definition of `mapStepCUDA(float*, float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x13da): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `reduceStepCUDA(float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0xa9d): multiple definition of `reduceStepCUDA(float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x14ca): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `__device_stub__Z17updateWeightsCUDAPfS_S_S_ii(float*, float*, float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x77a): multiple definition of `__device_stub__Z17updateWeightsCUDAPfS_S_S_ii(float*, float*, float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x11a7): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `__device_stub__Z11mapStepCUDAPfS_S_ii(float*, float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x8cd): multiple definition of `__device_stub__Z11mapStepCUDAPfS_S_ii(float*, float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x12fa): first defined here /tmp/tmpxft_00007384_00000000-29_parallel.o: In function `__device_stub__Z14reduceStepCUDAPfS_ii(float*, float*, int, int)': tmpxft_00007384_00000000-9_parallel.cudafe1.cpp:(.text+0x9e8): multiple definition of `__device_stub__Z14reduceStepCUDAPfS_ii(float*, float*, int, int)' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x1415): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `_copyHostDevice': utils.c:(.text+0x0): multiple definition of `_copyHostDevice' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x16): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `_copyDeviceHost': utils.c:(.text+0x76): multiple definition of `_copyDeviceHost' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x8c): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `getGlobalDim': utils.c:(.text+0xd7): multiple definition of `getGlobalDim' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0xed): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `getGridBasedOnBlockSize': utils.c:(.text+0x11d): multiple definition of `getGridBasedOnBlockSize' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x120): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `_sleep': utils.c:(.text+0x1a5): multiple definition of `_sleep' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x1de): first defined here /tmp/tmpxft_00007384_00000000-30_utils.o: In function `drawMatrix': utils.c:(.text+0x1c3): multiple definition of `drawMatrix' /tmp/tmpxft_00007384_00000000-21_nn.o:tmpxft_00007384_00000000-4_nn.cudafe1.cpp:(.text+0x1fc): first defined here collect2: error: ld returned 1 exit status
Replace this line: dim3 gridXY(gridX, gridY); with: dim3 gridXY; gridXY.x = gridX, gridXY.y = gridY;
C++ template construction for class member lists
So I have made a little bit of C++ templates before: some quite basic examples of the curiously recurring template pattern for classes of mathematical numbers. This time I tried using the same pattern to create a list or "network" for objects of a class which they could add themselves to and look around for other objects. I thought I could use a static std::list to do this. So my attempt goes like this: template<class C> class HasNetwork{ public: HasNetwork(){} static list<C*> m; }; template<class C> list<C*> HasNetwork<C>::m = list<C*>(); class IHaveNetwork: public HasNetwork<IHaveNetwork>{ public: IHaveNetwork(){ m.push_back(this); } }; int main(){ IHaveNetwork lHF, lHF2; //for(list<IHaveNetwork*>::iterator it = lHF.m->begin(); ; it++); return 0; } It seems to compile, but I get nasty link errors ( even with the for loop iterator commented out ). Maybe I need to do some cast or maybe "this" is not defined until after the constructor finishes? Here is the link error: /tmp/templatest-912860.o: In function `__clang_call_terminate': templatest.cpp: (.text.__clang_call_terminate[__clang_call_terminate]+0x9): undefined reference to `__cxa_begin_catch' templatest.cpp:(.text.__clang_call_terminate[__clang_call_terminate]+0x12): undefined reference to `std::terminate()' /tmp/templatest-912860.o: In function `__gnu_cxx::new_allocator<std::_List_node<IHaveNetwork*> >::deallocate(std::_List_node<IHaveNetwork*>*, unsigned long)': templatest.cpp: (.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE10deallocateEPS4_m[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE10deallocateEPS4_m]+0x1c): undefined reference to `operator delete(void*)' /tmp/templatest-912860.o: In function `std::list<IHaveNetwork*, std::allocator<IHaveNetwork*> >::_M_insert(std::_List_iterator<IHaveNetwork*>, IHaveNetwork* const&)': templatest.cpp: (.text._ZNSt4listIP12IHaveNetworkSaIS1_EE9_M_insertESt14_List_iteratorIS1_ERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE9_M_insertESt14_List_iteratorIS1_ERKS1_]+0x31): undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)' /tmp/templatest-912860.o: In function `std::list >::_M_create_node(IHaveNetwork* const&)': templatest.cpp: (.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xa0): undefined reference to `__cxa_begin_catch' templatest.cpp:(.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xbb): undefined reference to `__cxa_rethrow' templatest.cpp: (.text._ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_[_ZNSt4listIP12IHaveNetworkSaIS1_EE14_M_create_nodeERKS1_]+0xce): undefined reference to `__cxa_end_catch' /tmp/templatest-912860.o: In function `__gnu_cxx::new_allocator<std::_List_node<IHaveNetwork*> >::allocate(unsigned long, void const*)': templatest.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv]+0x33): undefined reference to `std::__throw_bad_alloc()' templatest.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorISt10_List_nodeIP12IHaveNetworkEE8allocateEmPKv]+0x40): undefined reference to `operator new(unsigned long)' /tmp/templatest-912860.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
I cannot reproduce. Here is a slightly modified version of you code that compiles links and runs without even a warning (I only had to fix some indirection level errors): #include <iostream> #include <list> #include <string> template<class C> class HasNetwork{ public: HasNetwork(){} static std::list<C*> m; }; template<class C> std::list<C*> HasNetwork<C>::m = std::list<C*>(); class IHaveNetwork: public HasNetwork<IHaveNetwork>{ std::string name; public: IHaveNetwork(const std::string &name): name(name) { m.push_back(this); } std::string getName() const { return name; } }; int main(){ IHaveNetwork lHF("foo"), lHF2("bar"); for(std::list<IHaveNetwork*>::iterator it = lHF.m.begin(); it != lHF.m.end(); it++) { std::cout << (*it)->getName() << std::endl; } return 0; }
Cannot get min_element to work in C++
I'm writing a small recursive algorithm. Here is the code: #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> coins; int checkchange(int left) { vector<int> choices (coins.size()); if (left == 0) return 0; else { int min; for (int i=0;i<coins.size();i++) { choices.at(i) = (1 + checkchange(left - coins.at(i))); } return min_element(choices.front(),choices.back()); } } int main() { int N; cin >> N; for (int i=0;i<N;i++) { int c,m,temp,change; cin >> c >> m; for (int j=0;j<c;j++) { cin >> temp; coins.push_back(temp); } for (int j=0;j<m;j++) { cin >> temp; change = checkchange(temp); cout << change; } } return 0; } I get the following error: In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/algorithm:62, from burningcoins.cpp:3: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h: In function ‘_FIter std::min_element(_FIter, _FIter) [with _FIter = int]’: burningcoins.cpp:19: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998: error: invalid type argument of ‘unary *’ /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_algo.h:5998: error: invalid type argument of ‘unary *’ I've tried compiling with both g++ and gcc, both give me the same error. What am I doing wrong? Edit: New Code: int checkchange(int left) { vector<int> choices (coins.size()); if (left == 0) return 0; else { for (int i=0;i<coins.size();i++) { choices[i] = (1 + checkchange(left - coins.at(i))); } return *min_element(choices.begin(), choices.end()); } } New error message: /tmp/ccV3VLsK.o: In function main': <br/> burningcoins.cpp:(.text+0x16a): undefined reference tostd::cin' burningcoins.cpp:(.text+0x16f): undefined reference to std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x187): undefined reference tostd::cin' burningcoins.cpp:(.text+0x18c): undefined reference to std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' <br/> burningcoins.cpp:(.text+0x19b): undefined reference tostd::basic_istream >::operator>>(int&)' burningcoins.cpp:(.text+0x1b0): undefined reference to std::cin' <br/> burningcoins.cpp:(.text+0x1b5): undefined reference tostd::basic_istream >::operator>>(int&)' burningcoins.cpp:(.text+0x1ec): undefined reference to std::cin' <br/> burningcoins.cpp:(.text+0x1f1): undefined reference tostd::basic_istream >::operator>>(int&)' burningcoins.cpp:(.text+0x208): undefined reference to std::cout' <br/> burningcoins.cpp:(.text+0x20d): undefined reference tostd::basic_ostream >::operator<<(int)' /tmp/ccV3VLsK.o: In function __static_initialization_and_destruction_0(int, int)': <br/> burningcoins.cpp:(.text+0x261): undefined reference tostd::ios_base::Init::Init()' burningcoins.cpp:(.text+0x266): undefined reference to std::ios_base::Init::~Init()' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_range_check(unsigned long) const': burningcoins.cpp:(.text._ZNKSt6vectorIiSaIiEE14_M_range_checkEm[std::vector >::_M_range_check(unsigned long) const]+0x2d): undefined reference to std::__throw_out_of_range(char const*)' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)': burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)]+0x259): undefined reference to __cxa_begin_catch' <br/> burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)]+0x2be): undefined reference to__cxa_rethrow' burningcoins.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[std::vector >::_M_insert_aux(__gnu_cxx::__normal_iterator > >, int const&)]+0x2c8): undefined reference to __cxa_end_catch' <br/> /tmp/ccV3VLsK.o: In functionstd::vector >::_M_check_len(unsigned long, char const*) const': burningcoins.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[std::vector >::_M_check_len(unsigned long, char const*) const]+0x4c): undefined reference to std::__throw_length_error(char const*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::deallocate(int*, unsigned long)': burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[__gnu_cxx::new_allocator::deallocate(int*, unsigned long)]+0x1c): undefined reference to operator delete(void*)' <br/> /tmp/ccV3VLsK.o: In function__gnu_cxx::new_allocator::allocate(unsigned long, void const*)': burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator::allocate(unsigned long, void const*)]+0x35): undefined reference to std::__throw_bad_alloc()' <br/> burningcoins.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)]+0x45): undefined reference tooperator new(unsigned long)' /tmp/ccV3VLsK.o:(.eh_frame+0x12): undefined reference to __gxx_personality_v0' <br/> /tmp/ccV3VLsK.o:(.eh_frame+0x4f): undefined reference to__gxx_personality_v0' collect2: ld returned 1 exit status
std::min_element takes a range. front and back return references to actual values. You should be using the begin and end methods to return an iterator to the corresponding positions in the vector: min_element(choices.begin(), choices.end()); // ^^^^^ ^^^ If you find this tedious, you can create a function which wraps around the standard min_element: template <class Container> auto min_element(Container c) -> decltype(std::min_element(c.begin(), c.end())) { return std::min_element(c.begin(), c.end()); } And use it as: min_element(choices);
C++: Undefined reference to function in namespace
Here I am, trying to figure out what's wrong with my code without success :( I'm writing a resampler but I guess that's of no interest at all, I'm just trying yo make this stupid warning go away. Anyway, here's my code: ddc.hpp #ifndef __DIGITAL_DOWN_CONVERTER_H__ #define __DIGITAL_DOWN_CONVERTER_H__ #include <vector> #include "interpolator.h" namespace ddc { void decimate(std::vector<float> &, unsigned int); void expand(std::vector<float> &, unsigned int); void perform_resampling(std::vector<float>, unsigned int, unsigned int); void generate_filter(std::vector<float> &, unsigned int, unsigned int); float Sinc(float); unsigned int mcd(unsigned int, unsigned int); } #endif ddc.cpp #include "ddc.hpp" namespace ddc { void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) { unsigned int i, gcd = mcd(freq_1, freq_2); unsigned int downFactor, upFactor; std::vector<float> filter; downFactor = freq_1/gcd; upFactor = freq_2/gcd; generate_filter(filter, 1024 /* lobi della semi-sinc */, upFactor); decimate(data, downFactor); expand(data, upFactor); interpolate_fft(data, filter); } } main.cpp #include <vector> #include "ddc.hpp" using namespace std; int main() { vector<float> data; // bla bla ddc::perform_resampling(data, 1000000, 60000); return 0; } Compiling with g++ (linux) I get the following error: $ make all g++ -c ddc.cpp -o ddc.o -Wall -O3 -lm -m64 g++ -c main.cpp -o main.o -Wall -O3 -lm -m64 g++ ddc.o main.o -o ../bin/resampler main.o: In function `main': main.cpp:(.text.startup+0x255): undefine d reference to `ddc::perform_resampling(std::vector<float, std::allocator<float> >, unsigned int, unsigned int)' collect2: ld returned 1 exit status make: *** [../bin/resampler] Error 1 I'm going out of my mind, please help me! What am I doing wrong? Besides, If I remove ddc:: from the main function, gcc suggests me this: main.cpp:59:49: note: suggested alternative: ddc.hpp:24:7: note: ‘ddc::perform_resampling’
You declare a function taking a vector by value as its first argument, then define it taking the vector by reference. This produces a separate overload, and the declared function has no definition. Presumably it should be a reference, so add & to the declaration in the header. You would get a more useful compiler error if you defined functions outside their namespace: void ddc::perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) { // ^^^^^ // blah blah } since it's an error to define a function with a qualified name if it hasn't been declared.
These two are different: void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) void perform_resampling(std::vector<float> data, unsigned int freq_1, unsigned int freq_2) P.S. This shows one good reason to put parameter names in your prototypes, even though they aren't strictly required. With parameter names, you can compare a prototype directly to a definition, and they should match character for character
In your prototype, you are missing & void perform_resampling(std::vector<float>, unsigned int, unsigned int); It appears in the definition void perform_resampling(std::vector<float> &data, unsigned int freq_1, unsigned int freq_2) { unsigned int i, gcd = mcd(freq_1, freq_2); unsigned int downFactor, upFactor; std::vector<float> filter; downFactor = freq_1/gcd; upFactor = freq_2/gcd; generate_filter(filter, 1024 /* lobi della semi-sinc */, upFactor); decimate(data, downFactor); expand(data, upFactor); interpolate_fft(data, filter); }
Using gcc/g++ instrument functions with MinGW?
i try to use the gcc instrument functions with g++ compiler of MinGW, but i always got linker issues. Is it possible to use the instrument functions with MinGW/MSYS? The linker failure output: $ g++ instrumentFunctions.cpp -o iftest -finstrument-functions >> iflog.txt C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x4e): undefined reference to `__cyg_prof ile_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x158): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x179): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x18c): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1a7): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1bf): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1db): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x1f3): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x22f): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x27a): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x29b): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x2e4): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x2ff): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x326): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x341): undefined reference to `__cyg_pro file_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text+0x368): undefined reference to `__cyg_pro file_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$printf[_printf]+0x16): undefined referenc e to `__cyg_profile_func_enter' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$printf[_printf]+0x43): undefined referenc e to `__cyg_profile_func_exit' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$_ZSt3minIjERKT_S2_S2_[unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int const&)]+0x15): undefined reference to `__cyg_profile_func_ent er' C:/Users/HELLHO~1/AppData/Local/Temp/ccUwxrxt.o:instrumentFunctions.cpp:(.text$_ZSt3minIjERKT_S2_S2_[unsigned int const& std::min<unsigned int>(unsigned int const&, unsigned int const&)]+0x42): undefined reference to `__cyg_profile_func_exi t' collect2: ld returned 1 exit status g++ command i use: g++ iftest.cpp -o iftest -finstruction-functions Listing of my testsource: #include <stdio.h> void __cyg_profile_func_enter( void *, void * ) __attribute__ ((no_instrument_function)); void __cyg_profile_func_enter(void *func, void *callsite) { printf("%p\n", (int)func); } void func_c( void ) { return; } void func_b( void ) { func_c(); return; } void func_a( void ) { func_b(); return; } int main() { func_a(); func_c(); }
You need to give the __cyg* functions "C" linkage. A function defined in C++ normally gets a mangled name which can't be used from libraries not written in C++. You can ask the compiler to give a function the name C would give it by using an `extern "C"{} block. The following should compile fine. #include <stdio.h> extern "C" { void __cyg_profile_func_enter( void *, void * ) __attribute__ ((no_instrument_function)); void __cyg_profile_func_enter(void *func, void *callsite) { printf("enter %p\n", func); } void __cyg_profile_func_exit( void *, void * ) __attribute__ ((no_instrument_function)); void __cyg_profile_func_exit(void *func, void *callsite) { printf("exit %p\n", func); } } void func_c( void ) { return; } void func_b( void ) { func_c(); return; } void func_a( void ) { func_b(); return; } int main() { func_a(); func_c(); }