External c/c++

Community Forums/Monkey2 Talk/External c/c++

impixi(Posted 2016) [#1]
I'm trying to code an example for Extern and I can't seem to make it work.. The following compiles in Monkey2 but I see the following error in the console:


/exte1.buildv002/build_cache/desktop_debug_windows/exte1_0exte1.cpp.o: In function `Z6bbMainv':
/exte1.buildv002/build_cache/desktop_debug_windows/exte1_exte1.cpp:17: undefined reference to `GetSysSeed()'



I've examined the generated c++ files and they don't seem to incorporate some of my c code. Leads me to believe I'm missing something obvious, though I can't see what it is. Comparing my own source files to those of the monkey2 module source, they *seem* to follow the same format...

Here are my source files:

utils.h



utils.c



utils.monkey2



exte1.monkey2



Any ideas?


Danilo(Posted 2016) [#2]
Try renaming utils.c to utils.cpp - it finds the function here with .cpp file.

Maybe you need to use ' extern "C“ ' for C functions in .c files (because MX2 is C++, not C):
extern "C" unsigned long GetSysSeed(void);


On Mac OS X I get another error (but compiles when out-commenting that part):
utils.cpp:9:17: error: use of undeclared identifier 'CLOCK_MONOTONIC'
                clock_gettime(CLOCK_MONOTONIC, &tm);
                              ^
1 error generated.


EDIT:
Verified: With .c file, change utils.h to the following:
#if _WIN32
	#include <windows.h>
#else
	#include <time.h>
#endif


#ifdef __cplusplus
  extern "C" {
#endif

unsigned long GetSysSeed(void);

#ifdef __cplusplus
  } // end extern "C"
#endif



impixi(Posted 2016) [#3]
Ah, thanks!

And it looks like I need to write an additional branch for __APPLE__, since clock_gettime() is not implemented in Mac OS...