Skip to content

Commit c775b60

Browse files
committed
add C->Fortran string array
1 parent 1313190 commit c775b60

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ set_target_properties(c_fortran_struct PROPERTIES LINKER_LANGUAGE C)
5353
target_compile_definitions(c_fortran_struct PRIVATE $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>)
5454
add_test(NAME C_Fortran_struct COMMAND c_fortran_struct)
5555

56+
add_executable(c_fortran_string_array c/string_array_main.c)
57+
target_link_libraries(c_fortran_string_array PRIVATE string_array_fortran)
58+
set_target_properties(c_fortran_string_array PROPERTIES LINKER_LANGUAGE C)
59+
add_test(NAME C_Fortran_string_array COMMAND c_fortran_string_array)
5660

5761
# -- C++ calling Fortran
5862
add_executable(cxx_call_fortran cxx/math_main.cxx)

src/c/string_array_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
void print_cstring_array(int* n, char* []);
2+
3+
int main(void) {
4+
char* cstring[] = { "abc", "def", "ghi", "jkl" };
5+
int n = 4;
6+
print_cstring_array(&n, cstring);
7+
}

src/fortran/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ add_library(math_fortran OBJECT math_lib.f90)
33
add_library(error_fortran OBJECT error_lib.f90)
44

55
add_library(struct_fortran OBJECT struct_lib.f90)
6+
7+
add_library(string_array_fortran OBJECT string_array_lib.f90)

src/fortran/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ math_fortran = library('math_fortran', 'math_lib.f90')
33
error_fortran = library('error_fortran', 'error_lib.f90')
44

55
struct_fortran = library('struct_fortran', 'struct_lib.f90')
6+
7+
string_array_fortran = library('string_array_fortran', 'string_array_lib.f90')

src/fortran/string_array_lib.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module str_mod
2+
3+
use iso_c_binding, only: c_ptr, c_int, c_f_pointer, c_loc, c_null_char
4+
5+
implicit none
6+
7+
contains
8+
9+
subroutine print_cstring_array(n, cstring) bind(C)
10+
11+
integer(kind=c_int), intent(in) :: n
12+
type(c_ptr), dimension(n), target, intent(in) :: cstring
13+
character, pointer :: fstring(:)
14+
integer :: i
15+
16+
do i = 1, n
17+
call c_f_pointer(cstring(i), fstring, [4])
18+
write(*,*) fstring
19+
end do
20+
21+
end subroutine print_cstring_array
22+
23+
end module str_mod

src/meson.build

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ c_fortran_struct = executable('c_fortran_struct',
7171
link_with: struct_fortran,
7272
link_language: 'fortran'
7373
)
74-
test('C Fortran struct', c_fortran_struct,
75-
timeout: 5
74+
test('C Fortran struct', c_fortran_struct, timeout: 5)
75+
76+
c_fortran_string_array = executable('c_fortran_string_array',
77+
sources: files('c/string_array_main.c'),
78+
link_with: string_array_fortran,
79+
link_language: 'fortran'
7680
)
81+
test('C Fortran string array', c_fortran_string_array, timeout: 5)
7782

7883
# -- C++ calling Fortran
7984
cxx_call_fortran = executable('cxx_call_fortran',

0 commit comments

Comments
 (0)