1

Attempting to build netcode server.c example using visual studio for linux. During compilation I am receiving the following error: netcode.c(5194,24): error : ‘CLOCK_MONOTONIC_RAW’ undeclared (first use in this function)

Which is coming from the following source:

// linux

#include <unistd.h>

void netcode_sleep( double time )
{
    struct timespec ts;
    ts.tv_sec = (time_t) time;
    ts.tv_nsec = (long) ((time - (double) ( ts.tv_sec )) * 1000000000.0);
    nanosleep( &ts, NULL );
}

double netcode_time()
{
    static double start = -1;
    if ( start == -1 )
    {
        struct timespec ts;
        clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
        start = ts.tv_sec + ( (double) ( ts.tv_nsec ) ) / 1000000000.0;
        return 0.0;
    }
    struct timespec ts;
    clock_gettime( CLOCK_MONOTONIC_RAW, &ts );
    double current = ts.tv_sec + ( (double) ( ts.tv_nsec ) ) / 1000000000.0;
    return current - start;
}

I have successfully built this project on windows, as well as DIRECTLY on a linux machine before with no issue. I'm a little perplexed as I wasn't expecting to run into any issues since from what I can tell visual studio for linux is really just syncing sources with a linux host and sending a g++ command for everything to be compiled on the host...so not real sure why this is happening. Any ideas?

EDIT

Solution was to include #define _POSIX_C_SOURCE 199309L in the source file of the library. For some reason this is not required when building directly on the linux machine, but visual studio for linux is requiring it. I'm guessing that maybe the compile command generated by visual studio for linux is possibly doing something to ommit the build from using _POSIX_C_SOURCE? Maybe? (making this assumption based on the fact the project built successfully directly on the linux machine using a basic g++ command: g++ server.c netcode.a -lsodium)

Can anyone identify such a switch/option that would be causing that from the below (which is generated by visual studio)

"g++" -W"switch" 
-W"no-deprecated-declarations" 
-W"empty-body" 
-W"conversion" 
-W"return-type" 
-W"parentheses" 
-W"no-pointer-sign" 
-W"no-format" 
-W"uninitialized" 
-W"unreachable-code" 
-W"unused-function" 
-W"unused-value" 
-W"unused-variable" 
-std=c++11 
-Wall -fno-strict-aliasing 
-g2 -gdwarf-2 "g++" -O0 "3600000" -fthreadsafe-statics 
-W"switch" 
-W"no-deprecated-declarations" 
-W"empty-body" 
-W"conversion" 
-W"return-type" 
-W"parentheses" 
-W"no-format" 
-W"uninitialized" 
-W"unreachable-code" 
-W"unused-function" 
-W"unused-value" 
-W"unused-variable" 
-frtti -fno-omit-frame-pointer -std=c11 -fexceptions -o "C:\dev\project<different options>
whitwhoa
  • 2,389
  • 4
  • 30
  • 61
  • *visual studio for linux* Was not aware such a thing existed. Do you mean Visual Studio Code? – user4581301 Aug 19 '20 at 22:43
  • Oh it's a thing. You can install the linux toolchain into visual studio, then link a linux host and use visual studio as ide and debugger while code executes on linux host: https://learn.microsoft.com/en-us/cpp/linux/?view=vs-2019 – whitwhoa Aug 19 '20 at 22:49
  • Ah ha! So VS is still running on a Windows box. That makes more sense. GCC Toolchain underneath? I wonder if it's targeting a more-general POSIX environment. `CLOCK_MONOTONIC_RAW` is Linux only and I can see it being left out if MS has broader support aspirations. – user4581301 Aug 19 '20 at 23:19

0 Answers0