Same code works fine on Win but not on Mac: TYPES PROBLEM [still need to unstnd] lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- tom.tom · 2013-06-05 16:51 Hi, In our app we communicate with an external device throgh the SDK provided by the vendor. The sample app has several simple calls to the SDK external functions (in a framework). This code works fine on Win but does not work on Mac - the is no error, but calling one of the external funtions seems to have no effect even though the funtion returns OK status. two additional interesting points: 1) we tested the similar app written in C using XCode and it works fine (which indicates the MAC version of the SDK is fine) 2) we managed to get the code working only few times when debugging by setting the breakpoint and using GDB to check the content of local variables which are the fields of a record being passed to the external function - not sure if there was some other factor that helped but we could not reproduce it (the code working) later... Any ideas how to approach that problem, what to egzamine, where to look, what may differ...? Thx, Tom ---------------------------------------------------------------------- Jonas Maebe · 2013-06-05 20:23 How do you declare the external SDK function on the Pascal side on Mac OS X? ---------------------------------------------------------------------- Leledumbo · 2013-06-06 00:58 In my experience, some SDK uses different calling convention for different OS. It's usually stdcall on windows and cdecl on others. ---------------------------------------------------------------------- tom.tom · 2013-06-06 12:04 OK, we've found out the source od the problem but still need to understand that. What we did was writting the same pascal code under XCode using FPC templates. What pointed us into the right direction was the compiler error under XCode complaining about the range checking. Here is the sample code: Code: [Select]var test : Integer; ... ... test := $7FFFFFFF; This compiles fine under Lazarus (with range checking on) on Mac and on Win. This value gets properly passed to the external library on Win but as I wrote before it didn't work on Mac. However this was not compiling under XCode because of the range check error! Whan we changed the type to LongWord it started working fine in all cases. Now, can someone explain why is that? This is the FP definition of ordinary types: Code: [Select]Integer either smallint or longint size 2 or 4 Cardinal longword 4 Longint -2147483648 .. 2147483647 4 Longword 0 .. 4294967295 4 the $7FFFFFFF = 2147483647   - why then it was not working under Mac but compiling under Lazarus and not compiling under XCode???? The SizeOf() was returning 4 on both platforms for both Integer and Longword. Is it possible that Integer is somehow managed in different way in FPC compiler under Mac? ---------------------------------------------------------------------- Jonas Maebe · 2013-06-06 12:15 The default syntax mode for the Xcode templates is MacPas. This means that if your source code does not contain any {$mode xxx} switch, it will be compiled in mode MacPas. In that mode, integer = smallint. I assume that your sizeof() test was not in the unit that contained that declaration, but in another unit. The reason you got a range check error in Xcode but not in Lazarus could be because the debug configuration of the FPC Xcode templates globablly enables range checking. Maybe Lazarus doesn't. In that case you would still have gotten a range check warning in Lazarus though. ---------------------------------------------------------------------- User137 · 2013-06-06 12:18 If you want to use signed 32-bit integer, you should use Longint. Integer is flexible type. Longword/Cardinal (4 billion) cannot handle negative values, and Integer/Longint is half smaller in positive side (2 billion). ---------------------------------------------------------------------- Leledumbo · 2013-06-06 13:18 Integer unfortunately is mapped to SmallInt by default for Turbo Pascal compatibility. Better use the specific size types for interoperability. ---------------------------------------------------------------------- tom.tom · 2013-06-06 21:44 @Jonas - Lazarus didn't complain about the range - even the GDB returned the corect value when inspecting this variable @User137 - for LongInt it didn't work either - only for LongWord - why? @Leledumbo - on what level it is mapped to SmallInt? How come the compiller and runtime allowed assigning $7FFFFFFF without any errors? ..and does anybody know why it worked fine on Windows but started working fine on Mac only when changed Integer to LongWord? Another question is what is really a difference between Integer and LongWord if they both returned SIzeOf = 4 and the $7FFFFFFF value should be stored fine in both cases and either on signed or unsigned type it sets exactly the same bits? ---------------------------------------------------------------------- Jonas Maebe · 2013-06-07 04:10 Maybe your Lazarus project has already a global setting for mode Delphi or mode objfpc, in which case integer would be mapped to longint already. And maybe your problem is completely unrelated to that particular assignment, but to a later use of that variable. Without the complete actual code, the SDK routine declaration and command line parameters used we can keep guessing for a very long time what the problem might or might not be.