Skip to content

Can not build with Visual Studio due GCC-style warning flags #846

@uilianries

Description

@uilianries

Greetings!

During the packaging steps for version 3.1.12 of libmodbus in Conan Center Index, PR conan-io/conan-center-index#30083, the following error was found when trying to build on Visual Studio (msvc 194):

Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35213 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

make[2]: *** [Makefile:489: modbus.lo] Error 1
make[2]: *** Waiting for unfinished jobs....
cl : Command line error D8021 : invalid numeric argument '/Wmissing-declarations'

Full CI build log is available here: https://c3i.jfrog.io/artifactory/cci-build-logs/cci/prod/PR-30083/3/package_build_logs/build_log_libmodbus_3_1_12_558aface5c2bae5aaed7b456c51a9ed9_19e318a866610969ae17aaa36552350ac86725f0.txt

Doing a check on configure.ac of version 3.1.12 and also on the master branch, I can see:

Configure hardcodes the warning flags to use GCC-style only: https://github.com/stephane/libmodbus/blob/v3.1.12/configure.ac#L150

WARNING_CFLAGS="-Wall \
-Wmissing-declarations -Wmissing-prototypes \
-Wnested-externs -Wpointer-arith \
-Wsign-compare -Wchar-subscripts \
-Wstrict-prototypes -Wshadow \
-Wformat-security"
AC_SUBST([WARNING_CFLAGS])

Then, those warning flags are consumed in the Makefile later: https://github.com/stephane/libmodbus/blob/v3.1.12/src/Makefile.am#L10

AM_CFLAGS = ${WARNING_CFLAGS}

It's possible to understand that the MSVC compiler does not recognize those warning flags, resulting in a failure during its build process. The same works fine when using GCC or even Clang.


As a workaround, we can patch to avoid using those warning flags, but it would be much better to have a proper fix coming from the upstream.

A possible solution would be using AX_CHECK_COMPILE_FLAG, so you can check if the compiler works or not, something like:

WARNING_CFLAGS=""
m4_foreach_w([flag], [
    -Wall
    -Wmissing-declarations
    -Wmissing-prototypes
    -Wnested-externs
    -Wpointer-arith
    -Wsign-compare
    -Wchar-subscripts
    -Wstrict-prototypes
    -Wshadow
    -Wformat-security
], [
    AX_CHECK_COMPILE_FLAG([flag],
        [WARNING_CFLAGS="$WARNING_CFLAGS flag"])
])
AC_SUBST([WARNING_CFLAGS])

Or, just add an if condition based on the compiler to skip Visual Studio.

Let me know if you need any help reproducing or to get more information about this case.

Regards!

Environment

OS: Windows
CPU: x86_64
Build type: Release
Compiler: Visual Studio - MSVC 194
Libmodbus version: 3.1.12

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