0
a.c:
#include "a.h"
double GetTimeStamp()
{
            struct timespec start;

            if((clock_gettime( CLOCK_REALTIME, &start)) == -1 )
            {
              perror("clock gettime\n");

            }

/* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt
 * will last while it's processing the background task.*/
            //micro seconds output
        return (1e3 * start.tv_sec + start.tv_nsec * 1e-3);

}

int a()
{
intr_rx_time = GetTimeStamp();
IPLatency = intr_rx_time;

}

a.h:
a();
double intr_rx_time, IPLatency;

b.c:
#include "a.h"
extern double IPLatency;
uint32 ipLatency;

int main()
{
  ipLatency = (uint32)IPLatency;
  printf("Current IP Latency microseconds: %ld\n", ipLatency);
}

In the above : I am calculating a timestamp from a.c program. Later I am reading the calcualted timestamp value in b.c program as shown above. But I am getting the output as 0 in b.c program. what is the error in the above program ?? could someone please help .

2 Answers2

1

The intr_rx_time and IPLatency in a.c and b.c are different objects.

Add extern to the header file (change from "definition" to "declaration") and define the variables (without extern) in a single one of the .c files.

// a.h
int a(void);
extern double intr_rx_time, IPLatency;

// a.c
#include <stdio.h>
#include <time.h>
#include "a.h"
double GetTimeStamp(void)
{
    struct timespec start;
    if ((clock_gettime(CLOCK_REALTIME, &start)) == -1) {
        perror("clock gettime");
    }
    /* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt
     * will last while it's processing the background task. */
    //micro seconds output
    return (1e3 * start.tv_sec + start.tv_nsec * 1e-3);
}

int a(void)
{
    intr_rx_time = GetTimeStamp();
    IPLatency = intr_rx_time;
}

// b.c
#include <inttypes.h>
#include <stdio.h>
#include "a.h"
double IPLatency;
uint32 ipLatency;

int main(void)
{
    ipLatency = IPLatency;
    printf("Current IP Latency microseconds: %" PRIu32 "\n", ipLatency);
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • could you modify that for me please!! i did not understand!! – user3635707 May 14 '14 at 11:02
  • Note that your `main()` function does not call any function from "a.c" – pmg May 14 '14 at 11:08
  • warning : format '%u' expects type 'unsigned int', but argument 2 has type 'uint32' – user3635707 May 14 '14 at 11:16
  • @user3635707: your compiler is being helpful. You have a good compiler. The warning means that using the specifier `"%u"` to print values of type `uint_32` is wrong: either change the specifier or change the type of the variable (or cast, ugh). – pmg May 14 '14 at 11:25
  • I used your code as above but it is showing waning as specified – user3635707 May 14 '14 at 11:29
  • I modified : ipLatency = (uint32) IPLatency; I specified %ld in printf. If i specify %d then it will show warning. – user3635707 May 14 '14 at 11:30
  • My code does never used `"%u"`. It always used `"%" PRIu32` – pmg May 14 '14 at 11:31
0

1) move double intr_rx_time, IPLatency; from a.h to a.c

2) prototype in a.h

a();

should be

int a(void);

3) add below lines to a.h

#include<time.h> 
#include<stdio.h>
typedef unsigned int uint32;

4) change %ld to %d in b.c

user207064
  • 665
  • 5
  • 14