Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Thursday, May 7, 2026

std::system Windows vs Linux

On Windows the C++ function std::system() is essentially a wrapper around the command processor (cmd.exe). When the process finishes, the exit code is passed directly back to you. If your program exits with 1, the integer returned by std::system is 1. On Linux, it might return 256.

On Linux, a single integer return value isn't just an exit code; it's a status word containing a wealth of information about how the process died. The OS packs different data into specific bit ranges. In most Linux implementations, the exit code is shifted into the high byte. This means a return code of 1 is stored as 1 << 8, which equals 256. To get back the exit code, you have to right shift the status code by 8 bits. The portable way is to use WEXITSTATUS macro.

To write code that works on both platforms, you cannot treat the return value as a raw number. You must use the decoding macros provided in <sys/wait.h> on Linux. You should always check if the process actually finished before asking for the code. Here is the safest pattern for Linux:

#include <sys/wait.h>
int status = std::system("./my_script.sh");
if (WIFEXITED(status)) {
  int exitCode = WEXITSTATUS(status); //
  std::cout << "Success! Code: " << exitCode;
} else if (WIFSIGNALED(status)) {
  int sig = WTERMSIG(status);
  std::cout << "Killed by signal: " << sig;
}

Tuesday, July 30, 2024

LONG_MAX is different for Windows 64 and Linux 64

When you generate code with Simulink (MATLAB R2023b) using ert.tlc, the default OS is Windows 64, see Configuration Parameters - Hardware Implementation - Device type. When you generate C code, the <model name>_private.h file will contain checks for ULONG_MAX and LONG_MAX.

On 64-bit Windows, the long type is typically 32 bits, which causes the LONG_MAX to be 0x7FFFFFFF. On 64-bit Linux systems, the long type is typically 64 bits, i.e. LONG_MAX is 0x7FFFFFFFFFFFFFFF. When you use code generated with the Windows 64 setting and use that on a Linux 64 OS, the check in <model name>_private.h will fail. The solution is to use the Linux 64 setting in Simulink which removes the LONG_MAX check from header file.

This checks seem to have been added after MATLAB R2022b because code generated with R2022b does not have them.

Friday, December 29, 2023

Windows batch file to run an exe and check its result

Windows batch file to run an exe and check its result. If you get the strange ") was unexpected at this time" error when you run it, try adding a comment line before that ")", it usually fixes it (and use "rem" instead of "::"). If you get "3 was unexpected at this time", move inner for loop out of the nested loop:

Monday, December 4, 2023

Sharing files between Windows and Ubuntu virtual machine

I have a Windows 10 PC with Ubuntu 22.04 installed as a VirtualBox virtual machine. There are other ways to share files between Windows and Ubuntu, but the following is the most general way I know:

  1. On Windows, share a folder (e.g. "temp") with your own windows user name.
  2. Find the IP address of VirtualBox ethernet adapter:

  3. On Ubuntu make sure you can ping that IP address.
  4. Open a new Files window, at the bottom left, click on other locations. Then, at the bottom enter smb://<VirtualbBox ethernet adapter IP address>/<Windows folder name>

  5. After clicking Connect button, you should see the temp folder:

Friday, November 3, 2023

Handling left over carriage return

Lines in text files created in Windows end with '\r\n'. If you read that text file in Linux with C++ getline(), your line will have a '\r' at the end because in Linux, getline() only gets rid of '\n'. If you have code that reads a certain number of characters and converts it to floating point using std::stod(), you might get std::invalid_argument exception when trying to read multiple values. You can use the following to take care of this problem:

Wednesday, December 15, 2021

Getting file/folder list in Windows and Linux

Getting file/folder list in Windows and Linux without using libraries is a tricky business. In Windows Visual Studio, there is also a character set setting (Project properties - Advanced - Character Set) which has to be taken into account.

Friday, November 26, 2021

Check if the MATLAB instance you created has exited

When you run MATLAB from a Windows bat file and want to know if it has finished its job and exited (without considering other already open MATLAB instance), you can use the following:

start matlab.exe -nosplash -nodesktop -noFigureWindows
set pid=not found
for /f "tokens=2" %%a in ('tasklist^|find /i "matlab") do (set pid=%%a)
echo matlab pid = %pid%

:waitForMatlabToExit
timeout /t /5 /nobreak
tasklist /fi "pid eq %pid%" | find ":">nul
if errorlevel 1 goto waitForMatlabToExit
echo Matlab exited
exit /b

Thursday, November 25, 2021

Windows batch file: goto vs call

In a Windows batch file, goto label2 command goes to the label2 and continues from the end of label2. call :label2 goes to label2 and after seeing a exit /b (or end of script, eof), returns back right after the call command. In other words, call behaves like a function call in Java, C++ etc. but you have to use exit /b instead of return.

Calling a Matlab function from Windows command line (cmd)

Calling a Matlab function from a Windows cmd batch file is easy if the function has no string arguments. However if the function takes a string argument, you have to use two single quotes around the argument. Example:

start matlab -nosplash -nodesktop -noFigureWindows -r eval("'myFunction(''fileName.txt'')'")

Wednesday, November 10, 2021

Remote connection and file transfer

The easiest way to connect from Windows to a remote Linux computer is to fire up cmd.exe and type ssh user@ip where user is the Linux user name and ip is the IP address of the Linux computer. 

For file transfer, you can use sftp user@ip or scp fileName user@remoteIP/remoteFileName.

ssh, sftp and scp come with Windows 10, you don't have to install anything. Just make sure that SSH is enabled on Linux.