Question

First of all, the function which I am trying to call isn't in an external library. It is part of the following (I am getting back into C++, so I thought I would start by writing my own cron daemon implementation)

Here is my code:

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

using namespace std;

//Global Variables:
    //local time in the form of a tm struct
    struct tm sysclocktime;
    /*
     * tm_sec   int seconds after the minute    0-60*
     * tm_min   int minutes after the hour  0-59
     * tm_hour  int hours since midnight    0-23
     * tm_mday  int day of the month    1-31
     * tm_mon   int months since January    0-11
     * tm_year  int years since 1900    
     * tm_wday  int days since Sunday   0-6
     * tm_yday  int days since January 1    0-365
     * tm_isdst int Daylight Saving Time flag   0-?
     */



//Function Declarations:
    bool fexists(const char *filename);
    char* timetostring(struct tm timeobj);

//Functions:    
int main(int argc, char* argv[]){
    //error counter
    int errors = 0;

    //Set the current time by the system's clock
    time_t     now = time(0);
    sysclocktime = *localtime(&now);
    //Print the current time to cout:
    cout << "Current time is: " << timetostring(sysclocktime) << endl;

    //See if mcron.cfg (mcron config file) exists
    if(fexists("mcron.cfg")){
        //if mcron.cfg exists, read data from it
        cout << "mcron.cfg existiert bereits!" << endl;
    }else{
        //if mcron.cfg doesn't exist, create it
        cout << "mcron.cfg existiert nicht! :(" << endl;
    }
    return errors;
}


/*
 * Function which tells whether a file exists
 */
bool fexists(const char *filename){
    ifstream ifile(filename);
    return ifile;
}

/*
 * Converts the pointer to a time struct into dates and times in a string
 * format like so: YYYY-MM-DD-HH-MM-SS
 * For example: 2013-07-15-16-14-36
 */
char* timetostring(struct tm* timeobj){
//Declare local variable charstring which will be used to create the string of characters
    char* charstring = new char[20];
    //Initialize charstring with relevant time data
sprintf(charstring,"%4d-%2d-%2d-%2d-%2d-%2d",(timeobj->tm_year+1900),    (timeobj->tm_mon+1),timeobj->tm_mday,timeobj->tm_hour,timeobj->tm_min,timeobj->tm_sec);
    //Print the string of characters to the command line:
    cout << "timetostring(): " << charstring;
    return charstring;
}

And here is what I get when I try to compile it:

[user@computer ~/code/mcron]$ g++ mcron.cpp -o mcron
/tmp/cc9M4We8.o: In function `main':
mcron-nostring.cpp:(.text+0xd1): undefined reference to `timetostring(tm)'
collect2: Error: ld returned 1 as it's exit status

What am I doing wrong?

Was it helpful?

Solution

Your declaration is missing a pointer:

char* timetostring(struct tm* timeobj);
                         // ^ here

(plus you need to adapt the call)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top