Compilation error - incomplete type:struct timespec

3,125 views
Skip to first unread message

Bogdan Pavkovic

unread,
Jun 10, 2015, 11:05:04 AM6/10/15
to lib...@googlegroups.com

Dear Attie,


First of all, thank you for providing us all this great library.

I managed to make and install it on ArchLinux without any problems.
Provided examples for xbeeZB work like a charm (at, remote_at, data, etc.)

Now I am trying to make use of other libraries in conjunction with libxbee as part of a larger project.

I have included in my project the following things:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h> //I/O system handling
#include <time.h>   //system time
#include <fcntl.h> //open function
#include <unistd.h> //close function
#include "sqlite3.h" 
#include "nmea.h"
#include <xbee.h>
#include <event.h> //even manager - libevent

I issue the following command when compiling with gcc:

gcc STrackU.c -o STrackU -I /usr/local/include/sqlcipher -L /usr/local/lib/libsqlcipher.a -lsqlcipher -I ~/nmealib/include/nmea/ -L ~/nmealib/lib/libnmea.a -lnmea -lxbee -lpthread -lrt 

I am running into a compilation error:

In file included from /usr/include/fcntl.h:68:0,
                 from STrackU.c:82:
/usr/include/bits/stat.h:72:21: error: field 'st_atim' has incomplete type
     struct timespec st_atim;  /* Time of last access.  */
                     ^
/usr/include/bits/stat.h:73:21: error: field 'st_mtim' has incomplete type
     struct timespec st_mtim;  /* Time of last modification.  */
                     ^
/usr/include/bits/stat.h:74:21: error: field 'st_ctim' has incomplete type
     struct timespec st_ctim;  /* Time of last status change.  */
                     ^
In file included from STrackU.c:90:0:
/usr/include/xbee.h:101:2: error: unknown type name 'time_t'
  time_t lastRxTime;
  ^
In file included from STrackU.c:90:0:
/usr/include/xbee.h:142:18: error: field 'timestamp' has incomplete type
  struct timespec timestamp;

I have noticed that it is a common error with struct timespec, basically several re-definitions of the same structure appear across various header files.
Even in xbee.h there is an #if !defined for protection against it.

How could I successfully mitigate such error in my own code?

Best regards,

Bogdan

Attie Grande

unread,
Jun 10, 2015, 11:31:20 AM6/10/15
to Bogdan Pavkovic, libxbee
Hi Bogdan,

Could you try including time.h before termios.h?

Attie

--
You received this message because you are subscribed to the Google Groups "libxbee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to libxbee+u...@googlegroups.com.
To post to this group, send email to lib...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/libxbee/4d02a4af-7a23-4ed3-89a0-dfa2d92f8896%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bogdan Pavkovic

unread,
Jun 10, 2015, 11:50:25 AM6/10/15
to lib...@googlegroups.com, bog...@gmail.com
Hi Attie,

Just tried it - to no avail.
Same compilation errors appear even this time.

Best,
Bogdan

Attie Grande

unread,
Jun 11, 2015, 3:57:06 AM6/11/15
to Bogdan Pavkovic, libxbee
Oh dear...
I've just tried compiling the following, and it worked fine.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>  //I/O system handling
#include <time.h>     //system time
#include <fcntl.h>    //open function
#include <unistd.h>   //close function
//#include "sqlite3.h"
//#include "nmea.h"
#include <xbee.h>
//#include <event.h>    //even manager - libevent

int main(int argc, char *argv[]) {
  struct timespec st_atim;

  return 0;
}

Please could you send me your STrackU.c file?
I'm particularly interested in line 82 (probably a #include <fcntl.h> as per the error in your first email).

Attie

Bogdan Pavkovic

unread,
Jun 11, 2015, 9:05:00 AM6/11/15
to lib...@googlegroups.com, bog...@gmail.com
Unfortunately I am not in a position to send you entire STrackU.c. :(
Line 82 is as simple as:
#include <fcntl.h>

So I guess it is collision between libxbee and some of these:
  • nmea.h
  • sqlite3.h
  • event.h
Any ideas how to resolve it?
I saw some solutions trying to use #define timespec linux_timespec
before including colliding headers and #undefine timespec just after it.

Thanks again for dedicating time to help me.

Attie Grande

unread,
Jun 11, 2015, 9:57:55 AM6/11/15
to Bogdan Pavkovic, libxbee
Ah okay - no problem.

I've just installed sqlite3 and libevent, and grabbed nmealib (http://nmea.sourceforge.net/- hopefully the right one).
I've managed to re-create your error, and it appears to be caused by the way you're including nmea.h.

Your compile line:
gcc STrackU.c -o STrackU -I /usr/local/include/sqlcipher -L /usr/local/lib/libsqlcipher.a -lsqlcipher -I ~/nmealib/include/nmea/ -L ~/nmealib/lib/libnmea.a -lnmea -lxbee -lpthread -lrt

Is adding ~/nmealib/include/nmea/ to the include path... and it adds this at the _front_ of the list, not the end.
As ~/nmealib/include/nmea/ contains a time.h, you're effectively masking out the system's time.h...

What you should be doing is as follows (note the angle braces for any/all 'system-level' includes):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>  //I/O system handling
#include <time.h>     //system time
#include <fcntl.h>    //open function
#include <unistd.h>   //close function
#include <sqlite3.h>
#include <nmea/nmea.h>
#include <xbee.h>
#include <event.h>    //even manager - libevent

And the compiling with this (don't go deeper than the 'include' directory)
gcc STrackU.c -o STrackU -L /usr/local/lib/libsqlcipher.a -lsqlcipher -I ~/nmealib/include -L ~/nmealib/lib/libnmea.a -lnmea -lxbee -lpthread -lrt

Note that I also removed the -I /usr/local/include/sqlcipher as these files should be included in the same way - using #include <sqlcipher/___.h>

Attie

Bogdan Pavkovic

unread,
Jun 11, 2015, 11:13:45 AM6/11/15
to lib...@googlegroups.com, bog...@gmail.com
Amazing! Works like a charm!

Million thanks for dedicating time and properly and pedagogically explaining the small tricks of the gcc compiler.

To complete your answer with a final gcc command (event lib should be linked as well):

gcc STrackU.c -o STrackU -L /usr/local/lib/libsqlcipher.a -lsqlcipher -I ~/nmealib/include -L ~/nmealib/lib/libnmea.a -lnmea -lxbee -lpthread -lrt -levent

SQLCipher include should be:
#include <sqlcipher/sqlite3.h>

Finally, environment should be configured before running an example with SQLCipher:

export LD_LIBRARY_PATH=/usr/local/lib/

according to the tutorial I have written for SQLCipher. :)

Bogdan

Attie Grande

unread,
Jun 11, 2015, 12:14:34 PM6/11/15
to Bogdan Pavkovic, libxbee
No problem! I'm pleased it works :-)

With regards to LD_LIBRARY_PATH... this is a bit hacky too... It'll also place the specified directories ahead of any system paths(!)
If you're developing a proper application, it might be worth either:
  1. Installing the libraries in a system library path (this only works if you're installing the library by hand, and not using a library that is handled by your package manager)
  2. Using rpath... see below
See my attached archive, and the session below - this creates libmy.so, and compiles the application twice with different options - test and test2.
$ make
gcc libmy/my.c -o libmy/libmy.o -c -fPIC
gcc -shared -Wl,-soname,libmy.so libmy/libmy.o -o libmy/libmy.so
gcc -Llibmy -Ilibmy/include test.c -lmy -o test
gcc -Llibmy -Ilibmy/include test.c -lmy -o test2 -Wl,-rpath,/home/attie/t/rpath_demo/libmy
$ ./test
./test: error while loading shared libraries: libmy.so: cannot open shared object file: No such file or directory
$ LD_LIBRARY_PATH=./libmy ./test
Hello World!
$ ./test2
Hello World!

Above, we have:
  • RED - Bad - The application (test) doesn't run, because it doesn't know where libmy.so is...
  • ORANGE - Better - We are now hinting as to where test may find libmy.so, but this can be tricky and unwieldy for 'real' applications...
  • GREEN - Good! - We can now just run test2 (compiled with the rpath set), and it works 'just like that' - Be aware though, if you move libmy out of (in this case) /home/attie/t/rpath_demo/libmy , then it will stop working again... So pick a place for the library, and leave it there.
Attie

rpath_demo.tgz
Reply all
Reply to author
Forward
0 new messages