Skip to content

BUG: f2py compile fail, require C99 standard #22572

Closed

Description

@Songningw99

Describe the issue:

After I ran f2py -c test_sub.f90 test_sub.pyf, I got an error compiling the codes:

......
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c:707:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (int i = 0; i < rank; ++i) {
     ^
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c:707:5: note: use option -std=c99 or -std=gnu99 to compile your code
error: Command "gcc -pthread -B /data/keeling/a/songning/miniconda3/envs/default/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC -DNPY_DISABLE_OPTIMIZATION=1 -I/tmp/tmphlgqv6nk/src.linux-x86_64-3.8 -I/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include -I/data/keeling/a/songning/miniconda3/envs/default/include/python3.8 -c /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c -o /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.o -MMD -MF /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.o.d" failed with exit status 1

I don't find an entrance to pass -std=c99, is there any specific reason that the int i = 0 parameter is place inside for statement here rather than make the statement before for? I don't remember seeing this error in the earlier version.

# f2py -v
1.23.4

My codes if it's needed:
test_sub.f90:

subroutine test_sub(a)
    use:: ISO_FORTRAN_ENV
    use:: IEEE_ARITHMETIC, only: ieee_support_nan, ieee_is_nan, &
                                            ieee_quiet_nan, ieee_value

    implicit none
    real(real32), dimension(:), intent(in):: a
    integer(int32):: n=len(a)

    do i = 1:n 
        write(*, *) i, ieee_is_nan(a(i))
    end do

end subroutine test_sub

test_sub.pyf:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module test_sub ! in 
    interface  ! in :test_sub
        subroutine test_sub(a) ! in :test_sub:test_sub.f90
            real(kind=real32) dimension(:),intent(in) :: a
        end subroutine test_sub
    end interface 
end python module test_sub

! This file was auto-generated with f2py (version:1.22.4).
! See:
! https://web.archive.org/web/20140822061353/http://cens.ioc.ee/projects/f2py2e

Reproduce the code example:

!f2py -c test_sub.f90 test_sub.pyf

Error message:

running build
running config_cc
INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
INFO: build_src
INFO: building extension "test_sub" sources
creating /tmp/tmphlgqv6nk/src.linux-x86_64-3.8
INFO: f2py options: []
INFO: f2py: test_sub.pyf
Reading fortran codes...
        Reading file 'test_sub.pyf' (format:free)
Post-processing...
        Block: test_sub
                        Block: test_sub
Post-processing (stage 2)...
Building modules...
    Building module "test_sub"...
    Generating possibly empty wrappers"
    Maybe empty "test_sub-f2pywrappers2.f90"
    Maybe empty "test_sub-f2pywrappers.f"
                Creating wrapper for Fortran subroutine "test_sub"("test_sub")...
        Constructing wrapper function "test_sub"...
getctype: "real(kind=real32)" is mapped to C "float" (to override define dict(real = dict(real32="<C typespec>")) in /data/jtrapp/a/songning/f_cape/.f2py_f2cmap file).
getctype: "real(kind=real32)" is mapped to C "float" (to override define dict(real = dict(real32="<C typespec>")) in /data/jtrapp/a/songning/f_cape/.f2py_f2cmap file).
getctype: "real(kind=real32)" is mapped to C "float" (to override define dict(real = dict(real32="<C typespec>")) in /data/jtrapp/a/songning/f_cape/.f2py_f2cmap file).
          test_sub(a)
    Wrote C/API module "test_sub" to file "/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_submodule.c"
    Fortran 90 wrappers are saved to "/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_sub-f2pywrappers2.f90"
INFO:   adding '/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c' to sources.
INFO:   adding '/tmp/tmphlgqv6nk/src.linux-x86_64-3.8' to include_dirs.
copying /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmphlgqv6nk/src.linux-x86_64-3.8
copying /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmphlgqv6nk/src.linux-x86_64-3.8
INFO:   adding '/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_sub-f2pywrappers.f' to sources.
INFO:   adding '/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_sub-f2pywrappers2.f90' to sources.
INFO: build_src: building npy-pkg config files
/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
running build_ext
INFO: customize UnixCCompiler
INFO: customize UnixCCompiler using build_ext
INFO: get_default_fcompiler: matching types: '['arm', 'gnu95', 'intel', 'lahey', 'pg', 'nv', 'absoft', 'nag', 'vast', 'compaq', 'intele', 'intelem', 'gnu', 'g95', 'pathf95', 'nagfor', 'fujitsu']'
INFO: customize ArmFlangCompiler
WARN: Could not locate executable armflang
INFO: customize Gnu95FCompiler
INFO: Found executable /usr/bin/gfortran
INFO: customize Gnu95FCompiler
INFO: customize Gnu95FCompiler using build_ext
INFO: building 'test_sub' extension
INFO: compiling C sources
INFO: C compiler: gcc -pthread -B /data/keeling/a/songning/miniconda3/envs/default/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC

creating /tmp/tmphlgqv6nk/tmp
creating /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk
creating /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk/src.linux-x86_64-3.8
INFO: compile options: '-DNPY_DISABLE_OPTIMIZATION=1 -I/tmp/tmphlgqv6nk/src.linux-x86_64-3.8 -I/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include -I/data/keeling/a/songning/miniconda3/envs/default/include/python3.8 -c'
INFO: gcc: /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_submodule.c
INFO: gcc: /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c
In file included from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/ndarraytypes.h:1948:0,
                 from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/arrayobject.h:5,
                 from /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.h:13,
                 from /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c:2:
/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
In file included from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/ndarraytypes.h:1948:0,
                 from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/arrayobject.h:5,
                 from /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.h:13,
                 from /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_submodule.c:23:
/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
 #warning "Using deprecated NumPy API, disable it with " \
  ^
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/test_submodule.c:144:12: warning: ‘f2py_size’ defined but not used [-Wunused-function]
 static int f2py_size(PyArrayObject* var, ...)
            ^
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c: In function ‘find_first_negative_dimension’:
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c:707:5: error: ‘for’ loop initial declarations are only allowed in C99 mode
     for (int i = 0; i < rank; ++i) {
     ^
/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c:707:5: note: use option -std=c99 or -std=gnu99 to compile your code
error: Command "gcc -pthread -B /data/keeling/a/songning/miniconda3/envs/default/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC -O2 -isystem /data/keeling/a/songning/miniconda3/envs/default/include -fPIC -DNPY_DISABLE_OPTIMIZATION=1 -I/tmp/tmphlgqv6nk/src.linux-x86_64-3.8 -I/data/keeling/a/songning/miniconda3/envs/default/lib/python3.8/site-packages/numpy/core/include -I/data/keeling/a/songning/miniconda3/envs/default/include/python3.8 -c /tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.c -o /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.o -MMD -MF /tmp/tmphlgqv6nk/tmp/tmphlgqv6nk/src.linux-x86_64-3.8/fortranobject.o.d" failed with exit status 1

NumPy/Python version information:

1.23.4
3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10)
[GCC 10.3.0]

Context for the issue:

No response

Activity

  1. rgommers commented on Nov 12, 2022

    @rgommers
    Member

    That looks like a regression indeed. What is odd is that you're using GCC 10, which should default to at least C99.

  2. briantoby commented on Dec 10, 2022

    @briantoby

    I saw this when I used the default gfortran and gcc on a RedHat Enterprise Linux build machine. It turned out that the versions were 4.5.something. Using newer compilers avoided the problem. I suspect that while GCC 10 was used to compile Python, something older is being accessed by f2py.

  3. rgommers commented on Dec 10, 2022

    @rgommers
    Member

    This problem will go away with the switch to Meson as the build system for NumPy; the checks for compiler version are already implemented (GCC 8 is the minimum), and a -std=c99 flag is passed by default.

    Now that you've understood and avoided the problem, I think we can close this. Thanks for the update @briantoby

  4. briantoby commented on Dec 10, 2022

    @briantoby

    @rgommers I am not sure the problem will "go away" as people (including me) use f2py for plenty of tasks other than to build NumPy, but if there are now gcc version checks included into f2py, that will help a lot.

  5. danbeibei commented on Jul 3, 2023

    @danbeibei
    Contributor

    Hi, I run into the same problem building something else than numpy with f2py and GCC 4.8.5. Unfortunatly updating GCC is not an option for me. I did not managed to pass the -std=c99 flag to the command line, is there any way of doing it?

  6. melissawm commented on Jul 3, 2023

    @melissawm
    Member

    I'll reopen so we can see what we can do.

  7. danbeibei commented on Jul 3, 2023

    @danbeibei
    Contributor

    I was a bit quick in my last message: I did managed to set to c99 standard with the environment variable CFLAGS=-std=c99.
    Nevertheless, it still might be worth it to declare the variable out of the for loop for convenience.

  8. HaoZeke commented on Aug 23, 2023

    @HaoZeke
    Member

    I'm not sure I see an f2py bug here, setting compiler flags via environment variables when requested by the compiler is independent of f2py. With regards to moving the integer declaration out, I'm not sure there's any point striving for pre-C99 code, and local scoping is very helpful.

  9. jwagnerhki commented on Aug 24, 2023

    @jwagnerhki

    All other functions in f2py fortranobject.c use the pre-C99 declaration of loop variables at the start of the function. Only find_first_negative_dimension() triggers the compile error due to its outlier for(int i=...) style of declaration. Coding style bug. I'd suggest to fix it.

  10. added a commit that references this issue on Aug 24, 2023
  11. added a commit that references this issue on Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions