I am trying to write the time in format of 21-02-2016 00:50:00 but there are some mistakes that I could not solve. Also in main function I called PrintTime()
and I got an undefined reference error in main function which is probably because of this piece of code.
void PrintTime()
{
struct timeval tv;
time_t nowtime;
struct tm *nowtm;
char tmbuf[64]
gettimeofday(&tv, NULL);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
free(tmbuf);
}
I tried to execute this here are the list of errors I get:
Q3.c:67:7: warning: assignment makes pointer from integer without a cast
nowtm = localtime(&nowtime);
^
Q3.c:68:1: warning: incompatible implicit declaration of built-in function ‘strftime’
strftime(tmbuf, sizeof tmbuf, "%d-%d-%d %d:%d:%d", nowtm);
^
/tmp/ccwQwMjE.o: In function `main':
Q3.c:(.text+0x102): undefined reference to `PrintTime'
Your code should have #include <time.h>
included in it
/* PrintTime.h */
#ifndef PRINTTIME_H
#define PRINTTIME_H
...
#include <time.h>
...
void PrintTime(){
}
And the main.c
/* main.c */
...
#include <PrintTime.h>
...
int main() {
...
return 0;
}
Compiling
% cc main.c -o main -I_path_to_PrintTime_h_file
You forgot ;
after char tmbuf[64]
. free(tmbuf);
is wrong because tmbuff
is declared as Stack not Heap. Revise and compile again. Be sure that you are already included
#include <sys/time.h>
#include <time.h>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.