FPM with multiple main programs C,C++,Fortran

For demo projects I often arrange examples by type, with main programs for C, C++, and Fortran in the same directory. FPM 0.7.0 doesn’t seem to handle this setup–it tries to link all the C and C++ code with the Fortran main program and then errors on “multiple main programs”.

A possible fix would be for FPM to detect int main( in a C or C++ code file and then ignore those specific files.

A key example of this is at GitHub - scivision/fortran-cpp-interface: Examples of Fortran 2003 C / C++ interfacing with Fortran where I tried to make FPM work by arranging all under “test/” but ran into this problem.

A second problem with this project is I don’t have a “src/” directory because the examples are standalone. I could workaround by putting an empty Fortran module code file under src/.

The lack of a src directory shouldn’t be a problem. If you want multiple programs, you need to put them in separate folders. Something like

.
└── app
    ├── prog1
    │   └── prog1.f90
    ├── prog2
    │   └── prog2.c
    └── prog3
        └── prog3.cpp

Thanks. I was able to rearrange the project accordingly.

Another quirk I noticed, it may be a bug–FPM doesn’t detect a Fortran program under test/ if the program has a contains statement. This is typical in a test program, that it might contain it’s own procedures.

perhaps adding something like this to your fpm.toml my help if you have multiple exes in the app folder:

executable = [
  { name = "xyz", main = "xyz.f90", link = "z" },
  { name = "enc", main = "enc.f90" },
  { name = "ffi", main = "ffi.f90" },
  { name = "ende", main = "ende.c" },
]
1 Like