0

I am working with a MATLAB optimization platform for black-box cost functions (BBCF).

To make the user free-handed, the utilized BBCF can be any executable file which inputs the input parameters of BBF and must output (return) the cost value of BBCF, so that the MATLAB BBCF optimizer finds the best (least cost) input parameter.

Considering that, from the one hand, my BBCF is implemented in C++, and from the other hand the cost value is a double (real number), I need to compile my code to an EXE file that outputs (returns) double.

But, to the best of my knowledge, when I compile a C++ code to EXE, it "mainly" compiles the main() function and its output is the output of main() function (i.e. 0 if running successful!).

An idea could be using a main function that returns double, and then, compiling such main() to EXE but, unfortunately, it is not possible in C++ (as explained in this link or as claimed in the 3rd answer of this question to be a bug of C++ neither of which are business of this question).

Can anyone provide an idea by which the EXE compiled form of a C++ code, outputs (returns) a double value?

hossayni
  • 465
  • 1
  • 3
  • 16
  • 1
    I would hope that the BBCF handling code takes an answer from the output file descriptor of the executable. Use something like `printf` or `std::cout` form the C++ application to write to that. Use the return value from `main` to signify error conditions, etc. – Rook Jun 13 '17 at 10:16
  • @JLev My question is not how to return a non-int type in C++ to become duplicate of that question (I had seen it). By referring that subject, I was explaining that returning double in main() is not feasible for my problem. – hossayni Jun 13 '17 at 10:17
  • Cant you build a `mex` that directly returns MATLAB types? – Ander Biguri Jun 13 '17 at 10:19
  • @Rook Thanks for hint. I had tested std::cout but not useful. Let me check printf, hope to work. – hossayni Jun 13 '17 at 10:19
  • @AnderBiguri Thanks, I would check if the nearer solutions failed. – hossayni Jun 13 '17 at 10:22

2 Answers2

1

This is not 'a bug in C++' (by the way, the bug might be in some C++ compiler, not in the language itself) - it's described in the standard that main() should return an integer:

http://en.cppreference.com/w/cpp/language/main_function

Regarding how to return a non-int from an executable, there are a couple of ways to do that. Two simplest (in terms of how to implement them) solutions come to my mind:

  1. Save it to a file. Then either monitor that file in Matlab for changes (e.g. compare timestamps) or read after each execution of your EXE file depending on how you're going to use it. Not very efficient solution, but does the job and probably the performance penalty is negligible to that of your other calculations.
  2. If you are fine with your cost value losing some numerical accuracy, you can just multiply the double value by some number (the larger this number, the more decimal places you will retain). Then round it, cast it to an int, have it returned from main(), cast it back to double in matlab and divide by the same number. The number used as the multiplier should be a power of 2 so that it doesn't introduce additional rounding errors. This method might be particularly useful if your cost value takes the values limited to the range [0, 1] or if you can normalize it to these values and you know that variations less than some threshold are not important.
KjMag
  • 2,650
  • 16
  • 16
0

In English, 'shall' is an even stronger imperative than 'must'.

Making a change like this would require changes to the operating system and shell. Such changes are unlikely to happen.

The easiest way to pass a double return would to to write it to standard output. Alternatively there are several methods available for interprocess communication.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43