Skip to content

[MSVC][x86] Compile error in box2d\src\core.c and box2d\samples\main.cpp #815

@NEIL-smtg

Description

@NEIL-smtg

Hi, I work on MSVC compiler testing, and we regularly build popular open-source project including box2d, with development builds of MSVC in order to find and fix regressions before they ship and cause problems for you.

Recently, as I build box2d with x86 as the target architecture from source with MSVC, I encountered the error as following:

C:\gitP\erincatto\box2d\src\core.c(118,8): error C2220: the following warning is treated as an error [C:\gitP\erincatto\box2d\build_x86\src\box2d.vcxproj]
C:\gitP\erincatto\box2d\src\core.c(118,8): warning C4047: 'initializing': 'void *' differs in levels of indirection from 'int' [C:\gitP\erincatto\box2d\build_x86\src\box2d.vcxproj]

I have encountered this error at https://github.com/erincatto/box2d/blob/main/src/core.c#L118 and https://github.com/erincatto/box2d/blob/main/samples/main.cpp#L74.

#ifdef B2_PLATFORM_WINDOWS
	void* ptr = _aligned_malloc( size32, B2_ALIGNMENT );
#elif defined( B2_PLATFORM_ANDROID )
    ....
#else
	void* ptr = aligned_alloc( B2_ALIGNMENT, size32 ); // error c2220
#endif

The compiler somehow is entering into the else block, but MSVC does not supporting aligned_alloc but they have a _aligned_malloc instead. For more details, refer to the official documentation: aligned-malloc.

After I have applied this msvc_suport.patch, the build was successful, but it introduced a new issue when running test.exe. Although the test runs successfully, the exit code is incorrect. This suggests that the patch I used is likely not the correct solution."

image

Steps to reproduce:

  1. Open x86 native tools command prompt for vs2022
  2. git clone https://github.com/erincatto/box2d.git
  3. cd erincatto/box2d
  4. mkdir build_x86
  5. cd build_x86
  6. set VSCMD_SKIP_SENDTELEMETRY=1 & "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat" -host_arch=x86 -arch=x86
  7. cmake -G "Visual Studio 17 2022" -A Win32 -DCMAKE_SYSTEM_VERSION=10.0.22621.0 .. 2>&1
  8. msbuild /m /p:Platform=Win32 /p:Configuration=Release box2d.sln /t:Rebuild 2>&1
  9. .\bin\Release\test.exe 2>&1

Build log: box2d.log

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions