Is there a difference between how LCC compiles a file and how MS visual studo compiles the same file?

6 views (last 30 days)
While running a simulation on MATLAB/SIMULINK, one of the blocks is an s-function written in C++. Compiling the C++ files with two different compilers, namely LCC and MS visual studio 2008 gives 2 different sets of results out of the simulator. Is there any obvious difference between the two compilers which would lead to a difference in the results? and when I say difference, using the visual studio 2008 compiler leads to values of the order of 10^250 which is clearly wrong. Has anyone encountered a similar problem before?

Answers (1)

James Tursa
James Tursa on 9 Aug 2012
LCC is a C compiler, not a C++ compiler. LCC cannot compile C++ code, so I assume that you really have C code and not C++ code. That being said, Visual C is generally vastly superior to LCC as far as optimization is concerned. I have seen cases where the compiled code it generated ran 3x - 10x faster than the code that LCC generated. But for correctly written code and a good algorithm I would not expect the results themselves to differ much. Without seeing your code I would hazard a guess that there is a coding problem, but that is just a guess. Can you post your code?
  2 Comments
Vishal Kulkarni
Vishal Kulkarni on 23 Aug 2012
Thanks a lot for the reply James. Unfortunately I cant post the code but i have figured out what the problem is or rather was. Along with one of the printf commands( and you were right it is a C code and not a C++ code), if I introduce an extra variable of type double and give a print command somewhere in the middle of the code, it compiles just fine but the moment I comment that line out it does not compile. I am assuming this problem arises because of the way Visual C optimizes the code and this leads to memory allocation problems. Introducing an extra and unnecessary variable in the C code fixed the problem though. Thanks again for your help
Vishal
James Tursa
James Tursa on 23 Aug 2012
What you are experiencing is a classic symptom of a missing prototype for a function and/or mismatched macro or typdef between a library function and your code. I suspect you are missing a needed header .h file from your source code. E.g., you might have a function that returns a char but it is missing a prototype, so the compiler assumes it returns an int and grabs garbage bits from a register for the return value (32 bits, 24 of which are garbage). By inserting a printf call, however, the printf function returns an int, which would have the effect of clearing the 24 garbage bits to 0 (typically) in the return register that your other function uses so you don't notice the coding error. I would look for missing header .h files or incorrect DEFINE's or typedef's. If you are not sure which functions are missing prototypes, rename the file to a .cpp file and compile it with Visual C++. Since C++ requires prototypes the compiler will complain each time it encounters such a function.

Sign in to comment.

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!