Skip to content

Commit c500140

Browse files
committed
fortran main error example
1 parent 60328e3 commit c500140

File tree

9 files changed

+85
-4
lines changed

9 files changed

+85
-4
lines changed

src/CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ target_link_libraries(fortran_cxx_struct PRIVATE struct_cxx)
1616
set_target_properties(fortran_cxx_struct PROPERTIES LINKER_LANGUAGE Fortran)
1717
add_test(NAME Fortran_C++_struct COMMAND fortran_cxx_struct)
1818

19+
add_executable(fortran_cxx_error fortran/error_main.f90)
20+
target_link_libraries(fortran_cxx_error PRIVATE error_cxx)
21+
set_target_properties(fortran_cxx_error PROPERTIES LINKER_LANGUAGE Fortran)
22+
add_test(NAME Fortran_C++_error
23+
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:fortran_cxx_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
24+
)
25+
1926
# -- Fortran calling C
2027
add_executable(fortran_c_math fortran/math_main.f90)
2128
target_link_libraries(fortran_c_math PRIVATE math_c)
@@ -25,20 +32,28 @@ add_executable(fortran_c_struct fortran/struct_main.f90)
2532
target_link_libraries(fortran_c_struct PRIVATE struct_c)
2633
add_test(NAME Fortran_C_struct COMMAND fortran_c_struct)
2734

35+
add_executable(fortran_c_error fortran/error_main.f90)
36+
target_link_libraries(fortran_c_error PRIVATE error_c)
37+
add_test(NAME Fortran_C_error
38+
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:fortran_c_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
39+
)
40+
2841
# -- C calling Fortran
2942
add_executable(c_fortran_error c/error_main.c)
3043
target_link_libraries(c_fortran_error PRIVATE error_fortran)
3144
# LINKER_LANGUAGE option is necessary for ifort at least
3245
set_target_properties(c_fortran_error PROPERTIES LINKER_LANGUAGE C)
3346
add_test(NAME C_Fortran_error
34-
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:c_fortran_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
47+
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:c_fortran_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
3548
)
3649

3750
add_executable(c_fortran_struct c/struct_main.c)
3851
target_link_libraries(c_fortran_struct PRIVATE struct_fortran)
3952
set_target_properties(c_fortran_struct PROPERTIES LINKER_LANGUAGE C)
53+
target_compile_definitions(c_fortran_struct PRIVATE $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>)
4054
add_test(NAME C_Fortran_struct COMMAND c_fortran_struct)
4155

56+
4257
# -- C++ calling Fortran
4358
add_executable(cxx_call_fortran cxx/math_main.cxx)
4459
target_link_libraries(cxx_call_fortran PRIVATE math_fortran)
@@ -56,6 +71,7 @@ add_executable(cxx_fortran_struct cxx/struct_main.cxx)
5671
target_include_directories(cxx_fortran_struct PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/c)
5772
target_link_libraries(cxx_fortran_struct PRIVATE struct_fortran)
5873
set_target_properties(cxx_fortran_struct PROPERTIES LINKER_LANGUAGE CXX)
74+
target_compile_definitions(cxx_fortran_struct PRIVATE $<$<BOOL:${MSVC}>:_CRT_SECURE_NO_WARNINGS>)
5975
add_test(NAME C++_Fortran_struct COMMAND cxx_fortran_struct)
6076

6177
# -- test wrapup

src/c/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
add_library(math_c OBJECT math_lib.c)
22

33
add_library(struct_c OBJECT struct_lib.c)
4+
5+
add_library(error_c OBJECT error_lib.c)

src/c/error_lib.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdlib.h>
2+
3+
void err(int);
4+
5+
void err(int code){
6+
exit(code);
7+
}

src/c/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
math_c = library('math_c', 'math_lib.c')
22

33
struct_c = library('struct_c', 'struct_lib.c')
4+
5+
error_c = library('error_c', 'error_lib.c')

src/cxx/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ add_library(math_cxx OBJECT math_lib.cxx)
22

33
add_library(struct_cxx OBJECT struct_lib.cxx)
44
target_include_directories(struct_cxx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../c)
5+
6+
add_library(error_cxx OBJECT error_lib.cxx)

src/cxx/error_lib.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <cstdlib>
2+
3+
extern "C" void err(int);
4+
5+
void err(int code){
6+
exit(code);
7+
}

src/cxx/meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
math_cxx = library('math_cxx', 'math_lib.cxx')
22

3-
struct_cxx = library('struct_cxx', 'struct_lib.cxx')
3+
struct_cxx = library('struct_cxx', 'struct_lib.cxx',
4+
include_directories: meson.current_source_dir() / '../c')
5+
6+
error_cxx = library('error_cxx', 'error_lib.cxx')

src/fortran/error_main.f90

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
program error_c
2+
3+
use, intrinsic :: iso_c_binding, only: C_INT
4+
5+
implicit none (type, external)
6+
7+
interface
8+
subroutine err(code) bind(C)
9+
import
10+
integer(C_INT), intent(in), value :: code
11+
end subroutine err
12+
end interface
13+
14+
call err(42)
15+
16+
end program

src/meson.build

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ subdir('cxx')
55
subdir('fortran')
66

77
# -- Fortran calling C++
8-
fortran_cxx_math = executable('fortran_call_cxx',
8+
fortran_cxx_math = executable('fortran_cxx_math',
99
sources: files('fortran/math_main.f90'),
1010
link_with: math_cxx,
1111
link_language: 'fortran'
1212
)
1313
test('Fortran C++ math', fortran_cxx_math, timeout: 5)
1414

15+
fortran_cxx_struct = executable('fortran_cxx_struct',
16+
sources: files('fortran/struct_main.f90'),
17+
include_directories: meson.current_source_dir() / 'c',
18+
link_with: struct_cxx
19+
)
20+
test('Fortran C++ struct', fortran_cxx_struct, timeout: 5)
21+
22+
fortran_cxx_err = executable('fortran_cxx_err',
23+
sources: files('fortran/error_main.f90'),
24+
link_with: error_cxx,
25+
link_language: 'fortran'
26+
)
27+
test('Fortran C++ error', fortran_cxx_err,
28+
timeout: 5,
29+
should_fail: true)
30+
1531
# -- Fortran calling C
1632
fortran_c_math = executable('fortran_c_math',
1733
sources: files('fortran/math_main.f90'),
@@ -20,13 +36,22 @@ fortran_c_math = executable('fortran_c_math',
2036
)
2137
test('Fortran C math', fortran_c_math, timeout: 5)
2238

23-
fortran_c_struct = executable('fortran_call_c',
39+
fortran_c_struct = executable('fortran_c_struct',
2440
sources: files('fortran/struct_main.f90'),
2541
link_with: struct_c,
2642
link_language: 'fortran'
2743
)
2844
test('Fortran C struct', fortran_c_struct, timeout: 5)
2945

46+
fortran_c_err = executable('fortran_c_err',
47+
sources: files('fortran/error_main.f90'),
48+
link_with: error_c,
49+
link_language: 'fortran'
50+
)
51+
test('Fortran C error', fortran_c_err,
52+
timeout: 5,
53+
should_fail: true)
54+
3055
# -- C calling Fortran
3156
c_fortran_error = executable('c_fortran_error',
3257
sources: files('c/error_main.c'),
@@ -64,6 +89,7 @@ test('C++ Fortran error', cxx_fortran_error,
6489

6590
cxx_fortran_struct = executable('cxx_fortran_struct',
6691
sources: files('cxx/struct_main.cxx'),
92+
include_directories: meson.current_source_dir() / 'c',
6793
link_with: struct_fortran,
6894
link_language: 'fortran'
6995
)

0 commit comments

Comments
 (0)