0

Hello and thanks in advance for all the problems this platform has solved for me in the past. Unfortunately I've found a problem, which I couldn't solve.

I'm pretty new to cmake and expanded a demo project with a new executable and some library files. I have no problems compiling the demo project. However, my new project needs to be compiled with c99 standard and suddenly, I get errors implementing the timespec struct from time.h. This is also used in the demo project, so I compiled the demo again with c99 and I've got the same problem.

Running this on Ubuntu, using the gcc compiler and cmake version 2.8.7

Hope I've got all necessary details covered. If not, please let me know and thanks in advance for your efforts!

Best regards

Edit#1: Error messages I get:
- >CLOCK_MONOTONIC< not declared (first use in this function)
- field 'tv_nsec' could not be resolved
- field 'tv_sec' could not be resolved
- Symbol 'CLOCK_MONOTONIC' could not be resolved
- Warnings for implicit declaration of functions 'clock_gettime', 'nanosleep', 'timeradd', 'timercmp'

Edit#2: error output with make VERBOSE=1

/usr/bin/gcc  -D_XOPEN_SOURCE=600 -I/home/localadmin/Eclipse_Workspace/SOEM_master/soem -I/home/localadmin/Eclipse_Workspace/SOEM_master/osal -I/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux -I/home/localadmin/Eclipse_Workspace/SOEM_master/oshw/linux    -std=c99 -o CMakeFiles/soem.dir/osal/linux/osal.c.o   -c /home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:60:50: Warning: »struct timezone« declared in parameter list [activated by default]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:60:50: Warning: range of validity includes only this definition or declaration [activated by default]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c: In function »osal_timer_start«:  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:105:4: Warning: Implicit function »timeradd« [-Wimplicit-function-declaration]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c: In function »osal_timer_is_expired«:  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:120:4: Warning: Implicit declaration of function »timercmp« [-Wimplicit-function-declaration]  
/home/localadmin/Eclipse_Workspace/SOEM_master/osal/linux/osal.c:120:61: Error: expected expression before »<« token  
make[2]: *** [CMakeFiles/soem.dir/osal/linux/osal.c.o] Error 1  
make[2]: Leaving directory '/home/localadmin/Eclipse_Workspace/SOEM_master/build'  
make[1]: *** [CMakeFiles/soem.dir/all] Error 2  
make[1]: Leaving directory '/home/localadmin/Eclipse_Workspace/SOEM_master/build'  
make: *** [all] Error 2

This was the output after definining _XOPEN_SOURCE=600, what was suggested in the other thread that got posted below. So the timespec struct is available, but the functions aren't.

Edit #3: minimal, complete and verifiable example

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main()
{
struct timespec test;
test.tv_sec = 0;
struct timeval start_time;
struct timeval timeout;
struct timeval stop_time;
timeradd(&start_time, &timeout, &stop_time);
return 0;
}  

Compiles without problems. If I use gcc mcv_example.c -std=c99 I get:

mcv_example.c: In function 'main':
mcv_example.c:24:18: error: storage size of 'test' isn't known
mcv_example.c:29:2: warning: implicit declaration of function 'timeradd' [-Wimplicit-function-declaration]

Edit#4: The solution for me was using gnu99 instead of c99. Now I can create the UNIX Makefiles with cmake, but still can't create a working Eclipse project.
Since that is a different problem, I guess this case is closed and thank you all for your help and efforts!

jckaos
  • 21
  • 5
  • Please share the errors you get. – Jabberwocky Sep 14 '16 at 09:46
  • Sorry, didn't think about that. Added it to the original post so that more people will see it. – jckaos Sep 14 '16 at 10:00
  • There is no point in mentioning `cmake` at all. Simply copy/paste the error reported by gcc in the shell after you typed: `make VERBOSE=1` – malat Sep 14 '16 at 10:02
  • That solved part of the problem. I tried it with the demo project, which is functional without running it with c99. Still got errors of functions not being declared. – jckaos Sep 14 '16 at 12:04
  • 1
    Produce a [mcve]. – n. m. could be an AI Sep 14 '16 at 12:10
  • Use `gcc -std=gnu11` (or `gcc -std=gnu99` if you must), or try `-D_XOPEN_SOURCE=700` (request POSIX 2008/2013 support, more or less). Note that the cross-referenced question mentions `_XOPEN_SOURCE` and `700` — the answer dates back to 2010, when support for POSIX 2008 was not as widespread as it is now. – Jonathan Leffler Sep 15 '16 at 06:38
  • Thanks, I stated in my edit#4 that gnu99 did work. Working on a solution to generate a Eclipse project with cmake now, but timespec is missing, which is no problem if I use the generator for UNIX Makefiles. I read somewhere that the project generator for eclipse has quite a lot of bugs, but there is supposed to be a manual workaround. – jckaos Sep 15 '16 at 06:56

1 Answers1

1

As man timeradd says, definition of function timeradd is available only when _DEFAULT_SOURCE feature-test-macro is defined:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

   All functions shown above:
       Since glibc 2.19:
           _DEFAULT_SOURCE
       Glibc 2.19 and earlier:
           _BSD_SOURCE

Description for _DEFAULT_SOURCE macro in man feature_test_macros says:

This macro can be defined to ensure that the "default" definitions are provided even when the defaults would otherwise be disabled, as happens when individual macros are explicitly defined, or the compiler is invoked in one of its "standard" modes (e.g., cc -std=c99).

So you need to explicitely define _DEFAULT_SOURCE macro for make function timeradd being available in -std=c99 mode:

#define _DEFAULT_SOURCE
#include <sys/time.h>
...
timeradd(...);
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thanks for your suggestion. I solved that by using gnu99 instead of c99. Everything works fine now , when I generate the UNIX Makefiles with cmake. I'm still getting the error that timespec can't be resolved, when I try to generate a Eclipse project, but that's a different story I guess. – jckaos Sep 15 '16 at 06:30