fpm icon indicating copy to clipboard operation
fpm copied to clipboard

Support for C and C++ source files and linking everything together

Open certik opened this issue 4 years ago • 7 comments

Fpm already has a preliminary support for C and C++ source files. But there are still some issues to iron out. Here are a few that I am aware of, we should create individual issues out of these and link them here.

  • [ ] One issue is that you currently have to manually specify stdc++ in fpm.toml. It should work automatically.

  • [ ] I think we need to add tests for this and test it at our CI.

  • [ ] Document this feature in our documentation

  • [ ] Ensure this works with MPI. Fpm does not support MPI yet, so we should add support first (#354). I remember there being issues regarding whether the main program has to be linked as a C++ or Fortran project.

certik avatar Feb 09 '21 18:02 certik

I will have a mixed Fortran/C++ package available soon (it's an interface to the nanoflann k-d tree library in C++). My current plan was to put

link = ["stdc++"]

inside fpm.toml. The C++ library is a header only library, so it's easy to ship it with my wrapper modules. As long as the source folder is in the include path, I imagine that would be enough to get it to work.

One issue I'm afraid of is that in C++ different conventions exist in practice (.hpp, .cpp, .c++, .hh, .cc, ...). In fact even the C file extensions .c and .h are just conventions. I don't think we want fpm to become a full C/C++ package manager. Perhaps some restricted package layout is sufficient for the majority of mixed-language cases.

For more complex C/C++ dependencies it might be a better idea to delegate them to a conan or CMake adaptor. In Rust they have a cmake crate which uses the two-way build script interface. Vice-versa, C++ projects which would depend on fpm packages, could export CMake packages (once this is supported, see #69).

ivan-pi avatar Feb 09 '21 19:02 ivan-pi

It is tempting to also make fpm be a solid C++ package manager. But I have a suspicion there might be C++ specific issues that we might prefer to avoid addressing. That being said, every time I want to try something in C++ or depend on something, I am already missing fpm. I opened an issue for Mamba to create a C++ package manager cpm, but didn't get much traction.

Given that we will have CMake (#69) backends, I would love to make fpm working for C++ as much as we can make it work, and a user is not locked in, they can always export to CMake (or any other build system that we will write a backend for) if they need something more complex.

In fact, I am looking forward to start new C++ projects by simply creating an fpm package (by adding some option to fpm to create a sample Hello World program in C++ instead of Fortran) and then export it to CMake, as I never remember all the things one must do in CMake to make it work off top of my head.

I think we can support all the .cpp and .h conventions for C++. In the default layout, I don't think there is anything that is Fortran specific, we simply follow the Cargo layout, more or less. So it should work for C++ also.

cc @milancurcic

certik avatar Feb 10 '21 00:02 certik

  • I remember there being issues regarding whether the main program has to be linked as a C++ or Fortran project.

In CMake, you need to manually specify the linker language is Fortran when adding executable targets in Fortran that depend on C++ libraries:

add_executable(main main.f90)
target_link_libraries(main my_cpp_lib)
set_property(TARGET main PROPERTY LINKER_LANGUAGE Fortran)

Alternatively, you need to play with the CMAKE_<LANG>_LINKER_PREFERENCE setting. Apparently the default preference rules give C++ higher priority.

With the Intel Fortran compiler, when the executable is in C and depends on a Fortran subprogram, and linking is performed with ifort, one needs to add the flag -nofor-main.

In fact, I am looking forward to start new C++ projects by simply creating an fpm package (by adding some option to fpm to create a sample Hello World program in C++ instead of Fortran) and then export it to CMake, as I never remember all the things one must do in CMake to make it work off top of my head.

Ditto. I just spent the last 2 hours creating CMake files for my mixed Fortran/C++ project of five files...

ivan-pi avatar Feb 11 '21 12:02 ivan-pi

Well, there is dds as C++ counterpart to fpm. The project happens to be in alpha stage, but I have seen some projects like https://github.com/marzer/tomlplusplus to actually support it.

awvwgk avatar Feb 12 '21 14:02 awvwgk

@awvwgk finally somebody is doing it! Thanks for the link.

certik avatar Feb 12 '21 14:02 certik

@wclodius2 @jvdp1 This might be needed to allow using C++ in the fpm version of stdlib (https://github.com/wclodius2/stdlib/pull/6#)

awvwgk avatar Dec 13 '21 19:12 awvwgk