Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand why compiler is giving me error with this code

I have the following C code, which looks very correct to me. However, the clang compiler (infact gcc or any other C compiler too) thinks otherwise.

typedef struct
{
    struct timeval td_start;
    struct timeval td_end;
} Timer;

void startTimer( struct Timer* ptimer ) 
{
    gettimeofday( &(ptimer->td_start), NULL );
}

void stopTimer( struct Timer* ptimer ) 
{
    gettimeofday( &(ptimer->td_end), NULL );
}

The compiler gives the following waring & error messages. Any idea what is wrong here?

./timing.h:14:25: warning: declaration of 'struct Timer' will not be visible
      outside of this function [-Wvisibility]
void startTimer( struct Timer* ptimer )
                        ^
./timing.h:16:27: error: incomplete definition of type 'struct Timer'
    gettimeofday( &(ptimer->td_start), NULL );
                    ~~~~~~^
./timing.h:14:25: note: forward declaration of 'struct Timer'
void startTimer( struct Timer* ptimer )
                        ^
./timing.h:19:24: warning: declaration of 'struct Timer' will not be visible
      outside of this function [-Wvisibility]
void stopTimer( struct Timer* ptimer )
                       ^
./timing.h:21:27: error: incomplete definition of type 'struct Timer'
    gettimeofday( &(ptimer->td_end), NULL );
                    ~~~~~~^
./timing.h:19:24: note: forward declaration of 'struct Timer'
void stopTimer( struct Timer* ptimer )
like image 964
pythonic Avatar asked May 15 '12 14:05

pythonic


People also ask

What causes compiler error?

A compile error happens when the compiler reports something wrong with your program, and does not produce a machine-language translation. You will get compile errors. Everybody does. Don't let them bother you.

How do you resolve a linking error?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.

What are compiler warnings?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

What causes linker errors?

Linker errors occur when the linker is trying to put all the pieces of a program together to create an executable, and one or more pieces are missing. Typically, this can happen when an object file or libraries can't be found by the linker. Fixing linker errors can be tricky.


2 Answers

Remove the struct keyword (it's not needed since you've already typedefed the struct):

void startTimer( Timer* ptimer ) 
{
  ...

void stopTimer( Timer* ptimer ) 
{
  ...

Alternatively, remove the typedef:

struct Timer
{
    struct timeval td_start;
    struct timeval td_end;
};

void startTimer( struct Timer* ptimer ) 
{
  ...

void stopTimer( struct Timer* ptimer ) 
{
  ...

For more information, see Why should we typedef a struct so often in C?

like image 79
NPE Avatar answered Oct 15 '22 03:10

NPE


Either you

struct Timer
{
    struct timeval td_start;
    struct timeval td_end;
};

void startTimer( struct Timer* ptimer ) 
{
    gettimeofday( &(ptimer->td_start), NULL );
}

Or you

typedef struct
{
    struct timeval td_start;
    struct timeval td_end;
} Timer;

void startTimer( Timer* ptimer ) 
{
    gettimeofday( &(ptimer->td_start), NULL );
}

But don't mixup.

like image 45
Didier Trosset Avatar answered Oct 15 '22 02:10

Didier Trosset