orangeduck / Corange

Pure C Game Engine
http://www.youtube.com/watch?v=482GxqTWXtA
Other
1.78k stars 199 forks source link

Doesn't build on linux #76

Closed mulle-nat closed 2 years ago

mulle-nat commented 2 years ago

You get a lot of linker errors like

/usr/bin/ld: obj/cengine.o:/home/src/src/unix/Corange/./include/cengine.h:80: multiple definition of `debug_str'; obj/casset.o:/home/src/src/unix/Corange/./include/cengine.h:80: first defined here
/usr/bin/ld: obj/cengine.o:/home/src/src/unix/Corange/./include/cengine.h:79: multiple definition of `debug_buf'; obj/casset.o:/home/src/src/unix/Corange/./include/cengine.h:79: first defined here

The problem is that the cengine.h header defines global buffer variables, that are compiled into each file:

#define ERROR_BUFFER_SIZE   2048*4
#define DEBUG_BUFFER_SIZE   2048*4
#define WARNING_BUFFER_SIZE 2048*4

...

char error_buf[ERROR_BUFFER_SIZE];
char error_str[ERROR_BUFFER_SIZE];

char warning_buf[WARNING_BUFFER_SIZE];
char warning_str[WARNING_BUFFER_SIZE];

char debug_buf[DEBUG_BUFFER_SIZE];
char debug_str[DEBUG_BUFFER_SIZE];

There are two quick solutions

#define error(MSG, ...) { \  
  char error_buf[ERROR_BUFFER_SIZE]; \
  char error_str[ERROR_BUFFER_SIZE]; \
  snprintf(error_str,(ERROR_BUFFER_SIZE-1), "[ERROR] (%s:%s:%i) ", __FILE__, __func__, __LINE__); \
  snprintf(error_buf,(ERROR_BUFFER_SIZE-1), MSG, ##__VA_ARGS__); strcat(error_str, error_buf);    \
  error_(error_str); }

This is thread-safer as a bonus, but putting 16K on the stack seems somewhat excessive.

blogdron commented 2 years ago

This error is caused by a change in the default behavior of the compiler. If you have an idea how to make it beautiful, we are waiting for the patch =)

mulle-nat commented 2 years ago

I outlined two solutions. I am not sure though, which one is preferable.

orangeduck commented 2 years ago

I think marking these as extern makes the most sense to me.