Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
4 tasks
certik opened this issue Feb 9, 2021 · 6 comments
Open
4 tasks

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

certik opened this issue Feb 9, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@certik
Copy link
Member

certik commented Feb 9, 2021

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 (Support MPI natively #354). I remember there being issues regarding whether the main program has to be linked as a C++ or Fortran project.

@ivan-pi
Copy link
Member

ivan-pi commented Feb 9, 2021

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).

@certik
Copy link
Member Author

certik commented Feb 10, 2021

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

@ivan-pi
Copy link
Member

ivan-pi commented Feb 11, 2021

  • 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...

@awvwgk
Copy link
Member

awvwgk commented Feb 12, 2021

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.

@certik
Copy link
Member Author

certik commented Feb 12, 2021

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

@awvwgk
Copy link
Member

awvwgk commented Dec 13, 2021

@wclodius2 @jvdp1 This might be needed to allow using C++ in the fpm version of stdlib (wclodius2/stdlib#6)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants