0

I'm trying to create a simple test to time some functions. I'm using the following files main.cpp and testTiming.hpp. I am not able to compile, I get the following error: main.cpp:16: undefined reference to `startProfile(TIME_ID)'. Is there anything incorrect about my function definitions?

testTiming.hpp

#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>

enum TIME_ID
{
    TIME1 = 0,
    TIME2 = 1,
    NUM_TIMES = 2
};

typedef struct timeTable
{
    timespec lastStartTime;
    timespec totalTime;
} timeTable;

timeTable mTimeTable[NUM_TIMES];

void inline stopProfile( TIME_ID timeId );

timespec inline diff(timespec start, timespec end)
{
    timespec temp;
    if ((end.tv_nsec-start.tv_nsec) < 0) {
        temp.tv_sec = end.tv_sec - start.tv_sec - 1;
        temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
     } else {
        temp.tv_sec = end.tv_sec - start.tv_sec;
        temp.tv_nsec = end.tv_nsec - start.tv_nsec;
     }
     return temp;
}

void inline startprofile( TIME_ID timeId)
{
    if (timeId = TIME1)
    {
        stopProfile(TIME1);
    }

    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(mTimeTable[timeId].lastStartTime));
    //clock_gettime(CLOCK_REALTIME, &(mTimeTable[timeId].lastStartTime));
};

void inline stopProfile( TIME_ID timeId)
{
    timespec stopTime;
    timespec diffTime;
    clock_gettime(CLOCK_THREAD_CPUTIME_ID, &(stopTime));
    //clock_gettime(CLOCK_REALTIME, &(stopTime));
    diffTime = diff(mTimeTable[timeId].lastStartTime, stopTime);
    mTimeTable[timeId].totalTime.tv_sec += diffTime.tv_sec;
    mTimeTable[timeId].totalTime.tv_nsec += diffTime.tv_nsec;

    if (timeId != TIME1)
    {
        startprofile(TIME1);
    }
};

void printprofile(void)
{
    int i;
    for(i = 0; i < NUM_TIMES; i++)
    {
        switch( i)
        {
        case TIME1:
        printf("PROFILE : TIME1: %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float                  
        (mTimeTable[i].totalTime.tv_nsec / 1000000000.0f) );
        break;
        case TIME2:
        printf("PR0FILE : TIME2P: %d %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float 
        (mTimeTable[i].totalTime.tv_nsec / 1000000000.0f) );
        printf("PR0FILE : TIME2W: %d %f s\n", (float) mTimeTable[i].totalTime.tv_sec + float   
        (mTimeTable[i].totalTime.tv_nsec) );
        break;
        default:
        break;
        } 
    }
};

main.cpp

#include "testTiming.hpp"
#include <time.h>

using namespace std;

void inline startProfile( TIME_ID timeId );
void inline printProfile( void );

int main(int argc, char** argv)
{
int sec = 5;

startProfile(TIME1);
sleep(sec);
stopProfile(TIME1);
//printProfile();

return 0;
}

Makefile

ls SHELL = /bin/csh

all :
    g++ main.cpp -g -o timeTest

clean:
    rm -f timeTest
iheartcpp
  • 371
  • 1
  • 5
  • 14
  • Don't mark them as inline! They won't be visible outside of that compile unit if you do. I [wrote a micro library for exactly this](https://github.com/cameron314/microbench), by the way. – Cameron Dec 03 '14 at 23:00
  • Oops, I [stand corrected about `inline`](http://stackoverflow.com/a/7767858/21475) -- it's fine to use as long as they're not also `static` (since they're more-or-less `extern` by default). But the definitions still shouldn't be `inline` here, I think. In any case, the problem is `clock_gettime`, which is in the `rt` library. Add `-lrt` to the end of your `g++` line in the makefile. – Cameron Dec 03 '14 at 23:02

0 Answers0