-2

My project is compiling and opening up on the web but the main loop is not working. The error seems to be that usleep is undefined. I'm not sure how I can correct this. As per the error message, I seem to be dividing by zero (undefined). I tried removing 'fps' but no luck.

#define emscripten_set_main_loop(func,fps,simulateInfiniteLoop) 
while (1) { func(); usleep(1000000/fps); }

Expands to:

while (1) { loop(); usleep(1000000/0); }
identifier "usleep" is undefined C/C++(20)
genpfault
  • 51,148
  • 11
  • 85
  • 139
Adib0y360
  • 1
  • 5
  • Where are you expecting `usleep` to be defined? What header are you including to declare it? – Brian61354270 Feb 04 '23 at 20:55
  • 1
    `usleep` requires `#include ` on POSIX machines and does not exist on most other machines. If you want your code to generally _work on machines_, use C++'s [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for). – Drew Dormann Feb 04 '23 at 20:55
  • @Brian: I'm using the standard emscripten.h file, which is identical to the following: https://chromium.googlesource.com/external/github.com/kripken/emscripten/+/1.35.20/system/include/emscripten/emscripten.h – Adib0y360 Feb 04 '23 at 20:58
  • 1
    It is also important to understand what it means for a symbol to be "undefined". It has nothing to do with dividing by zero. – Drew Dormann Feb 04 '23 at 20:59
  • @DrewDormann: I replaced usleep with sleep_for and added chrono and thread as header files. Still having the same error message, but now with sleep_for instead of usleep. :( – Adib0y360 Feb 04 '23 at 21:03
  • 1
    Please post your solution as an answer to the question instead of an update. – Bill the Lizard Feb 04 '23 at 22:31

1 Answers1

0

Problem solved! I switched over to using emscripten_set_main_loop_arg, put all my variables in a struct, and rewrote my functions to accept pointers to the struct and deal appropriately.

Adib0y360
  • 1
  • 5