Skip to content

Check _WIN32 instead of WIN32 definition in C++ code #7546

@ekilmer

Description

@ekilmer

Bug Description:

The C++ API code is checking WIN32 instead of _WIN32, which causes compilation failure and some confusion when the user uses their own CMAKE_CXX_FLAGS (without redefining WIN32) during CMake configuration on Windows systems.

Steps To Reproduce:

Use your own CMAKE_CXX_FLAGS without defining /DWIN32

cmake -B build -S . -DBN_ALLOW_STUBS=ON -DCMAKE_CXX_FLAGS="/permissive-"
cmake --build build --config Release

and you'll eventually see an error from function.cpp

  function.cpp
C:\Users\ekilmer\src\binaryninja-api\function.cpp(2500,2): error C3861: '__sync_fetch_and_add': identifier not found [C:\Users\ekil
mer\src\binaryninja-api\build\binaryninjaapi.vcxproj]
C:\Users\ekilmer\src\binaryninja-api\function.cpp(2511,2): error C3861: '__sync_fetch_and_add': identifier not found [C:\Users\ekil
mer\src\binaryninja-api\build\binaryninjaapi.vcxproj]

which is this snippet

binaryninja-api/function.cpp

Lines 2497 to 2501 in 5d9fa65

#ifdef WIN32
InterlockedIncrement((LONG*)&m_advancedAnalysisRequests);
#else
__sync_fetch_and_add(&m_advancedAnalysisRequests, 1);
#endif

and when compiling for Windows, I would expect line 2500 to be removed, except the #ifdef WIN32 doesn't work because I've overwritten CMAKE_CXX_FLAGS.

Expected Behavior:

Compilation to succeed by using internally defined compiler definitions like _WIN32 or others here https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170

Additional Information:

CMake is to blame here for defining WIN32 by default in the CMAKE_CXX_FLAGS (check your CMakeCache.txt in the build directory with a default cmake -B build -S .).

While this is easy to resolve on the user's end (just add /DWIN32 to your flags), it was a little confusing to track down at first.

Also, in the case of developing third-party plugins, not using the built-in _WIN32 can force some additional code when using the API as a submodule and wanting to enable more warnings on only the plugin's code instead of the entire API. Maybe there's a better way (I know this isn't perfect) to do this for C++ plugins, but I've done the following in a external/CMakeLists.txt file:

# Clear all of our warning flags for external dependencies
set(CMAKE_CXX_FLAGS "")

# Make the binaryninja-api code available
set(HEADLESS ON)
add_subdirectory(binaryninja-api EXCLUDE_FROM_ALL SYSTEM)

... I could also create a CMake interface target for all of my compilation warnings to link to my plugin target(s) to work around this as well, but I figured I'd try opening this issue to attempt changing things here instead, which might also prevent future folks from running into this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: APIIssue needs changes to the APIEffort: TrivialIssues require < 1 day of work

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions