Skip to content

Commit 0afe973

Browse files
committed
add Fortran submodule example
1 parent 0b5c0ac commit 0afe973

22 files changed

+228
-79
lines changed

.github/workflows/ci_cmake.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
strategy:
2424
matrix:
2525
gcc-version: [7, 8, 9, 10, 11]
26+
shared: [true, false]
2627

2728
env:
2829
CC: gcc-${{ matrix.gcc-version }}
@@ -36,11 +37,9 @@ jobs:
3637
sudo apt update
3738
sudo apt install ninja-build gcc-${{ matrix.gcc-version }} g++-${{ matrix.gcc-version }} gfortran-${{ matrix.gcc-version }}
3839
39-
4040
- uses: actions/checkout@v2
4141

42-
43-
- run: cmake --preset multi
42+
- run: cmake --preset multi -DBUILD_SHARED_LIBS:BOOL=${{ matrix.shared }}
4443

4544
- run: cmake --build --preset debug
4645
- run: ctest --preset debug -V
@@ -70,7 +69,7 @@ jobs:
7069
sudo apt update
7170
sudo apt install ninja-build clang-${{ matrix.clang-version }}
7271
73-
- run: cmake --preset multi
72+
- run: cmake --preset multi -DBUILD_SHARED_LIBS:BOOL=${{ matrix.shared }}
7473

7574
- run: cmake --build --preset debug
7675
- run: ctest --preset debug -V
@@ -87,6 +86,7 @@ jobs:
8786
strategy:
8887
matrix:
8988
compiler: [ {cpp: g++-11, c: gcc-11}, {cpp: clang++, c: clang} ]
89+
shared: [true,false]
9090

9191
env:
9292
FC: gfortran-11
@@ -99,7 +99,7 @@ jobs:
9999

100100
- uses: actions/checkout@v2
101101

102-
- run: cmake --preset multi
102+
- run: cmake --preset multi -DBUILD_SHARED_LIBS:BOOL=${{ matrix.shared }}
103103

104104
- run: cmake --build --preset debug
105105
- run: ctest --preset debug -V
@@ -113,12 +113,20 @@ jobs:
113113
runs-on: windows-latest
114114
timeout-minutes: 10
115115

116+
strategy:
117+
matrix:
118+
compiler: [ {cpp: g++, c: gcc} ]
119+
shared: [true,false]
120+
116121
env:
117122
CMAKE_GENERATOR: MinGW Makefiles
123+
FC: gfortran
124+
CC: ${{ matrix.compiler.c }}
125+
CXX: ${{ matrix.compiler.cpp }}
118126

119127
steps:
120128
- uses: actions/checkout@v2
121129

122-
- run: cmake -Bbuild
130+
- run: cmake -Bbuild -DBUILD_SHARED_LIBS:BOOL=${{ matrix.shared }}
123131
- run: cmake --build build
124132
- run: ctest --test-dir build --preset default

.github/workflows/intel-oneapi.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ on:
2020
jobs:
2121

2222
linux-intel-oneapi:
23+
strategy:
24+
matrix:
25+
shared: [true,false]
26+
2327
runs-on: ubuntu-latest
2428
timeout-minutes: 10
2529

@@ -45,7 +49,7 @@ jobs:
4549
printenv >> $GITHUB_ENV
4650
4751
- name: Configure multi
48-
run: cmake --preset multi
52+
run: cmake --preset multi -DBUILD_SHARED_LIBS:BOOL=${{ matrix.shared }}
4953

5054
- name: print config log
5155
if: ${{ failure() }}

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ We CI test with compilers including:
2626
* Clang ≥ 6
2727
* Intel oneAPI
2828

29-
In general, strongly avoid the FortranCInterface of CMake and mangling function names--just use Fortran 2003 standard `bind(C)`
29+
In general, strongly avoid the FortranCInterface of CMake and mangling function names--just use Fortran 2003 standard `bind(C)`.
30+
31+
## Examples
32+
33+
C++ examples include [./cxx/vector_main.cxx](vector_main.cxx) that shows using a C++ contiguous
34+
[std::vector](https://en.cppreference.com/w/cpp/container/vector)
35+
to pass 1-D arrays to/from Fortran.
3036

3137
## build
3238

@@ -46,8 +52,6 @@ meson compile -C build
4652
meson test -C build
4753
```
4854

49-
50-
5155
### MacOS
5256

5357
For MacOS with Apple's Clang and Homebrew GCC,

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project('Fortran_c_cxx_interface', 'c', 'cpp', 'fortran',
22
version : '1.3.0',
33
meson_version: '>= 0.55.0',
4-
default_options : ['default_library=static', 'cpp_std=c++11', 'c_std=c99'])
4+
default_options : ['cpp_std=c++11', 'c_std=c99'])
55

66
cpp = meson.get_compiler('cpp')
77
cstdlib = cpp.find_library('stdc++', required : true)

src/CMakeLists.txt

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ add_subdirectory(cxx)
55
add_subdirectory(fortran)
66

77
# -- Fortran calling C++
8-
add_executable(fortran_cxx_math fortran/math_main.f90)
9-
target_link_libraries(fortran_cxx_math PRIVATE math_cxx)
8+
add_executable(fortran_cxx_vector fortran/vector_main.f90)
9+
target_link_libraries(fortran_cxx_vector PRIVATE vector_cxx)
1010
# LINKER_LANGUAGE option is necessary for ifort at least
11-
set_target_properties(fortran_cxx_math PROPERTIES LINKER_LANGUAGE Fortran)
12-
add_test(NAME Fortran_C++_math COMMAND fortran_cxx_math)
11+
set_target_properties(fortran_cxx_vector PROPERTIES LINKER_LANGUAGE Fortran)
12+
add_test(NAME Fortran_C++_vector COMMAND fortran_cxx_vector)
1313

1414
add_executable(fortran_cxx_struct fortran/struct_main.f90)
1515
target_link_libraries(fortran_cxx_struct PRIVATE struct_cxx)
@@ -24,9 +24,9 @@ COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:fortran_cxx_error> -Dexp_code=42 -P
2424
)
2525

2626
# -- Fortran calling C
27-
add_executable(fortran_c_math fortran/math_main.f90)
28-
target_link_libraries(fortran_c_math PRIVATE math_c)
29-
add_test(NAME Fortran_C_math COMMAND fortran_c_math)
27+
add_executable(fortran_c_vector fortran/vector_main.f90)
28+
target_link_libraries(fortran_c_vector PRIVATE vector_c)
29+
add_test(NAME Fortran_C_vector COMMAND fortran_c_vector)
3030

3131
add_executable(fortran_c_struct fortran/struct_main.f90)
3232
target_link_libraries(fortran_c_struct PRIVATE struct_c)
@@ -39,6 +39,11 @@ COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:fortran_c_error> -Dexp_code=42 -P $
3939
)
4040

4141
# -- C calling Fortran
42+
add_executable(c_fortran_submodule c/submodule_main.c)
43+
target_link_libraries(c_fortran_submodule PRIVATE submodule_fortran)
44+
set_target_properties(c_fortran_submodule PROPERTIES LINKER_LANGUAGE C)
45+
add_test(NAME C_Fortran_submodule COMMAND c_fortran_submodule)
46+
4247
add_executable(c_fortran_error c/error_main.c)
4348
target_link_libraries(c_fortran_error PRIVATE error_fortran)
4449
# LINKER_LANGUAGE option is necessary for ifort at least
@@ -54,9 +59,18 @@ target_compile_definitions(c_fortran_struct PRIVATE $<$<BOOL:${MSVC}>:_CRT_SECUR
5459
add_test(NAME C_Fortran_struct COMMAND c_fortran_struct)
5560

5661
# -- C++ calling Fortran
57-
add_executable(cxx_call_fortran cxx/math_main.cxx)
58-
target_link_libraries(cxx_call_fortran PRIVATE math_fortran)
59-
add_test(NAME C++_Fortran_math COMMAND cxx_call_fortran)
62+
add_executable(cxx_fortran_submodule cxx/submodule_main.cxx)
63+
target_link_libraries(cxx_fortran_submodule PRIVATE submodule_fortran)
64+
target_compile_definitions(cxx_fortran_submodule PRIVATE _USE_MATH_DEFINES)
65+
add_test(NAME C++_Fortran_submodule COMMAND cxx_fortran_submodule)
66+
67+
add_executable(cxx_fortran_array cxx/array_main.cxx)
68+
target_link_libraries(cxx_fortran_array PRIVATE vector_fortran)
69+
add_test(NAME C++_Fortran_array COMMAND cxx_fortran_array)
70+
71+
add_executable(cxx_fortran_vector cxx/vector_main.cxx)
72+
target_link_libraries(cxx_fortran_vector PRIVATE vector_fortran)
73+
add_test(NAME C++_Fortran_vector COMMAND cxx_fortran_vector)
6074

6175
add_executable(cxx_fortran_error cxx/error_main.cxx)
6276
target_link_libraries(cxx_fortran_error PRIVATE error_fortran)
@@ -76,9 +90,9 @@ add_test(NAME C++_Fortran_struct COMMAND cxx_fortran_struct)
7690
# -- test wrapup
7791
set_tests_properties(
7892
C_Fortran_error C_Fortran_struct
79-
C++_Fortran_math C++_Fortran_error C++_Fortran_struct
80-
Fortran_C_math Fortran_C_struct
81-
Fortran_C++_math Fortran_C++_struct
93+
C++_Fortran_vector C++_Fortran_error C++_Fortran_struct
94+
Fortran_C_vector Fortran_C_struct
95+
Fortran_C++_vector Fortran_C++_struct
8296
PROPERTIES
8397
TIMEOUT 5
8498
)

src/c/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(math_c OBJECT math_lib.c)
1+
add_library(vector_c OBJECT vector_lib.c)
22

33
add_library(struct_c OBJECT struct_lib.c)
44

src/c/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
math_c = library('math_c', 'math_lib.c')
1+
vector_c = library('vector_c', 'vector_lib.c')
22

33
struct_c = library('struct_c', 'struct_lib.c')
44

src/c/submodule_main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// main C program using a Fortran submodule
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#define _USE_MATH_DEFINES
7+
#include <math.h>
8+
9+
extern float pi();
10+
11+
int main() {
12+
13+
if (fabs(pi() - M_PI) > 1e-4) {
14+
fprintf(stderr, "pi() unexpected value: %f\n", pi());
15+
return EXIT_FAILURE;
16+
}
17+
18+
printf("OK: pi() value: %f\n", pi());
19+
20+
return EXIT_SUCCESS;
21+
22+
}
File renamed without changes.

src/cxx/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(math_cxx OBJECT math_lib.cxx)
1+
add_library(vector_cxx OBJECT vector_lib.cxx)
22

33
add_library(struct_cxx OBJECT struct_lib.cxx)
44
target_include_directories(struct_cxx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../c)

0 commit comments

Comments
 (0)