2
$\begingroup$

I create a C-library and compile it using the tools in LibraryLink. When I create a code binary like this

CreateLibrary[src, "lib_name", "Debug" -> True]

I can specify the option "Debug" -> True or False. Is it possible to check the value of this option inside the C-code? I just want to add debug information if the library is compiled with Debug option and not use it if this option is not present. For example

//c-code

int is_debug(){
#ifdef DEBUG_MODE
  return 1; //when Debug -> True
#else
  return 0; //when Debug -> False
#endif
}

//rest code  
$\endgroup$
5
  • 1
    $\begingroup$ The precise ifdef to use would depend on the compiler that Mathematica is using, so check docs for whatever compiler you have set in $CCompiler. Usually NDEBUG or DEBUG is a good bet. $\endgroup$
    – flinty
    Commented Feb 19 at 13:19
  • 1
    $\begingroup$ There are other ways of course: you could check for sections in the binary (on linux where DWARF info is embedded into the ELF), or for the presence of a PDB file next to it (if on windows), or you could assert(false) and drop to a signal handler immediately after you load the library and set a flag. $\endgroup$
    – flinty
    Commented Feb 19 at 13:30
  • $\begingroup$ @flinty thanks! I tried both options and unfortunately there are no definitions for those names DEBUG and NDEBUG $\endgroup$ Commented Feb 19 at 20:03
  • $\begingroup$ I use visual studio compiler $\endgroup$ Commented Feb 19 at 20:22
  • 1
    $\begingroup$ "_DEBUG Defined as 1 when the /LDd, /MDd, or /MTd compiler option is set. Otherwise, undefined." learn.microsoft.com/en-us/cpp/preprocessor/… $\endgroup$
    – ihojnicki
    Commented Feb 19 at 20:38

0

Browse other questions tagged or ask your own question.