{"id":18076270,"url":"https://github.com/scivision/fortran-cpp-interface","last_synced_at":"2025-04-05T19:43:02.467Z","repository":{"id":45627047,"uuid":"293382224","full_name":"scivision/fortran-cpp-interface","owner":"scivision","description":"Examples of Fortran 2003 C / C++ interfacing with Fortran","archived":false,"fork":false,"pushed_at":"2025-01-27T15:02:14.000Z","size":411,"stargazers_count":31,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-02-11T17:59:45.188Z","etag":null,"topics":["fortran-interface","interoperability"],"latest_commit_sha":null,"homepage":"","language":"Fortran","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scivision.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-07T00:02:47.000Z","updated_at":"2025-01-27T15:02:18.000Z","dependencies_parsed_at":"2025-01-23T01:55:22.825Z","dependency_job_id":null,"html_url":"https://github.com/scivision/fortran-cpp-interface","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Ffortran-cpp-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Ffortran-cpp-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Ffortran-cpp-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scivision%2Ffortran-cpp-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scivision","download_url":"https://codeload.github.com/scivision/fortran-cpp-interface/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393538,"owners_count":20931809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fortran-interface","interoperability"],"created_at":"2024-10-31T11:09:26.397Z","updated_at":"2025-04-05T19:43:02.446Z","avatar_url":"https://github.com/scivision.png","language":"Fortran","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Interoperability examples between C, C++ and Fortran\n\n[![ci](https://github.com/scivision/fortran-cpp-interface/actions/workflows/ci.yml/badge.svg)](https://github.com/scivision/fortran-cpp-interface/actions/workflows/ci.yml)\n[![oneapi-linux](https://github.com/scivision/fortran-cpp-interface/actions/workflows/oneapi-linux.yml/badge.svg)](https://github.com/scivision/fortran-cpp-interface/actions/workflows/oneapi-linux.yml)\n\nFortran subroutines and functions are easily called from C and C++.\n\nUse the standard C binding to define variable and bind functions/subroutines.\n\nThis project is also a way to quickly check if compilers you have are ABI-compatible.\nFor example:\n\n* Clang and Gfortran\n* (Windows) MSVC and Intel oneAPI ifx\n\nDemonstrate linking of\n\n* C and C++ program calling Fortran libraries\n* Fortran program calling C and C++ libraries\n\nWe assume the compilers are C++20 and Fortran 2018 capable.\n\nThis repo's examples are also known to work with:\n\n* NVidia HPC SDK (nvc++, nvfortran)\n* AOCC AMD Optimizing Compliers\n* Cray compilers (cc, ftn)\n\nIn general, avoid the FortranCInterface of CMake and mangling function names.\nInstead, use Fortran 2003 standard `bind(C)`.\n\nOther real-world examples include\n[fortran-filesystem](https://github.com/scivision/fortran-filesystem)\nusing C++ stdlib filesystem from Fortran and\n[standard sleep implementation](./src/sleep)\nas used in\n[blocktran](https://github.com/fortran-gaming/blocktran)\nand Fortran Standard Library.\n\n## Build\n\n```sh\ncmake -B build\n\ncmake --build build\n\nctest --test-dir build\n```\n\nNote the use of CMake target property\n[LINKER_LANGUAGE](https://cmake.org/cmake/help/latest/prop_tgt/LINKER_LANGUAGE.html)\nnecessary for CMake with Intel oneAPI or NVIDIA HPC SDK on Linux.\n\n* C main program with Fortran library: `LINKER_LANGUAGE C`\n* C++ main program with Fortran library: `LINKER_LANGUAGE CXX`\n* Fortran main program with C or C++ library: `LINKER_LANGUAGE Fortran`\n\n```cmake\nadd_executable(f_main main.f90 lib.cpp)\nset_property(TARGET f_main PROPERTY LINKER_LANGUAGE Fortran)\n\nadd_executable(c_main main.c lib.f90)\nset_property(TARGET c_main PROPERTY LINKER_LANGUAGE C)\n\nadd_executable(cpp_main main.cpp lib.f90)\nset_property(TARGET cpp_main PROPERTY LINKER_LANGUAGE CXX)\n```\n\n## Examples\n\nWhile the examples prioritize C++, there are also several companion C examples to go with the C++ examples.\nThere are also some Fortran main programs calling C or C++.\n\n### arrays\n\nThe examples \"array\", \"malloc\", \"vector\" show distinct ways to send arrays to/from Fortran with C and C++.\n\n### bool\n\n[nvfortran](https://forums.developer.nvidia.com/t/nvfortran-c-bool-bind-c-not-improper-value/291896)\nsupports F2018 standard `C_BOOL` if `nvfortran -Munixlogical` is used.\n\n* [ifort might return an incorrect C_BOOL .true. in iso_c_binding - Intel Community](https://community.intel.com/t5/Intel-Fortran-Compiler/ifort-might-return-an-incorrect-C-BOOL-true-in-iso-c-binding/m-p/999050)\n* [Internal representation of LOGICAL variables (The GNU Fortran Compiler](https://gcc.gnu.org/onlinedocs/gfortran/Internal-representation-of-LOGICAL-variables.html#Internal-representation-of-LOGICAL-variables)\n* [Solved: Re: Error #8809: An OPTIONAL or EXTERNAL dummy argument to a BIND(C) procedure is not interoperable - Intel Community](https://community.intel.com/t5/Intel-Fortran-Compiler/Error-8809-An-OPTIONAL-or-EXTERNAL-dummy-argument-to-a-BIND-C/m-p/1250873#M154133)\n* [Branchless conditional arithmetic oddities - #18 by sblionel - Language enhancement - Fortran Discourse](https://fortran-lang.discourse.group/t/branchless-conditional-arithmetic-oddities/5451/18)\n* [oneAPI -standard-semantics](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2024-1/standard-semantics.html)\n* https://www.fortran90.org/src/gotchas.html#c-fortran-interoperability-of-logical\n* https://info.ornl.gov/sites/publications/Files/Pub158443.pdf Section 3.1.2\n* https://shroud.readthedocs.io/_/downloads/en/latest/pdf/ section 11.4\n\n\u003e The logical constants .TRUE. and .FALSE. are defined to be the four-byte values -1 and 0 respectively.\nA logical expression is defined to be .TRUE. if its least significant bit is 1 and .FALSE. otherwise.\n\nExample output, Fortran interfacing with C or C++\n\n* Intel oneAPI 2023 without `-fpscomp logicals`\n* NVHPC 2023.5 without `-Munixlogical`\n\n```\n logical_not(T): F\n   storage_size()  bits   hex(in)  hex(out)\n         C_BOOL:     8         1        FE\n logical_not(true) should be false: 1\n\n logical_not(F): T\n   storage_size()  bits   hex(in)  hex(out)\n         C_BOOL:     8         0        FF\n```\n\n* Intel oneAPI 2023 with `-fpscomp logicals`\n* NVHPC 2023.5 with `-Munixlogical`\n\n```\n logical_not(T): F\n   storage_size()  bits   hex(in)  hex(out)\n         C_BOOL:     8         1         0\n\n logical_not(F): T\n  storage_size()  bits   hex(in)  hex(out)\n        C_BOOL:     8         0         1\n OK: boolean-logical not\n```\n\n### Error handling\n\nUsing Fortran statement \"stop\" or \"error stop\" with a C/C++ main program works like with a Fortran main program.\nThe \"error\" examples show this.\n\n### ISO_Fortran_binding.h\n\nOn 2019-01-12 [a GCC commit](https://github.com/gcc-mirror/gcc/commit/bbf18dc5d248a79a20ebf4b3a751669cd75485fd)\nfrom Paul Thomas brought ISO_Fortran_binding.h to\n[GCC 9](https://gcc.gnu.org/gcc-9/changes.html).\n\nThis means if using Clang compiler e.g. on macOS, you may need to switch to GCC (till Clang/Flang someday includes ISO_Fortran_binding.h).\n\nCompilers having \"ISO_Fortran_binding.h\" include:\n\n* GCC \u0026ge; 9\n* Intel oneAPI\n* Clang / Flang LLVM \u0026ge; 19\n* IBM OpenXL\n* Cray Fortran\n\n\n## Notes\n\n### struct memory alignment\n\nSome examples use scalar C struct.\nISO_C_BINDING handles mapping struct to/from Fortran TYPE.\nFor arrays of struct, compiler\n[pragma may be needed](https://stackoverflow.com/questions/53161673/data-alignment-inside-a-structure-in-intel-fortran).\n\nValgrind needed suppression to avoid memory alignment (uninitialized memory) warnings for C struct input to nanosleep.\n\n### MacOS\n\nFor MacOS with Apple's Clang and Homebrew GCC,\nit MAY be needed to have in ~/.zshrc like the following:\n(check directory / versions on your Mac)\n\n```sh\nexport LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib\nexport CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include\nexport CXXFLAGS=-I$CPLUS_INCLUDE_PATH\nexport CFLAGS=$CXXFLAGS\n```\n\n## References\n\n* [StackOverflow](\nhttps://stackoverflow.com/tags/fortran-iso-c-binding/info)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Ffortran-cpp-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscivision%2Ffortran-cpp-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscivision%2Ffortran-cpp-interface/lists"}