Skip to content

Commit 9f2e749

Browse files
committed
error test: check that return code is as expected
1 parent cebd7b1 commit 9f2e749

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ For example:
1616

1717
Demonstrate linking of
1818

19-
* C++ calling Fortran
20-
* Fortran calling C++
21-
* Fortran calling C
19+
* C and C++ program calling Fortran libraries
20+
* Fortran program calling C and C++ libraries
2221

23-
https://stackoverflow.com/tags/fortran-iso-c-binding/info
24-
25-
In general, CMake >= 3.14 has better link resolution than CMake 3.13.
2622
In general, strongly avoid the FortranCInterface of CMake and mangling function names--just use Fortran 2003 standard `bind(C)`
2723

2824
## build
@@ -42,13 +38,19 @@ be sure you have in ~/.zshrc like the following:
4238
(check directory / versions on your Mac)
4339

4440
```sh
45-
export LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib
46-
export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include
41+
export LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
42+
export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
4743
export CXXFLAGS=-I$CPLUS_INCLUDE_PATH
4844
export CFLAGS=$CXXFLAGS
4945
```
5046

5147
## Error handling in Fortran with C/C++ main program
5248

53-
Using Fortran statement "stop" or "error stop" with a C/C++ main program results in segmentation fault across compilers and operating systems.
54-
Instead, return an error code from Fortran subroutine to the C/C++ main program, and let C/C++ handle the error.
49+
Using Fortran statement "stop" or "error stop" with a C/C++ main program works like with a Fortran main program.
50+
The "error*" examples show this.
51+
52+
53+
## References
54+
55+
* [StackOverflow](
56+
https://stackoverflow.com/tags/fortran-iso-c-binding/info)

cmake/test_error.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
execute_process(COMMAND ${exe}
2+
RESULT_VARIABLE ret
3+
)
4+
5+
if(NOT ret EQUAL exp_code)
6+
message(FATAL_ERROR "Expected ${exe} code ${exp_code} but got ${ret}")
7+
endif()

src/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ add_executable(c_fortran_error c/error_main.c)
2121
target_link_libraries(c_fortran_error PRIVATE error_fortran)
2222
# LINKER_LANGUAGE option is necessary for ifort at least
2323
set_target_properties(c_fortran_error PROPERTIES LINKER_LANGUAGE C)
24-
add_test(NAME C_Fortran_error COMMAND c_fortran_error)
24+
add_test(NAME C_Fortran_error
25+
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:c_fortran_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
26+
)
2527

2628
# -- C++ calling Fortran
2729
add_executable(cxx_call_fortran cxx/main.cxx)
@@ -32,12 +34,11 @@ add_executable(cxx_fortran_error cxx/error_main.cxx)
3234
target_link_libraries(cxx_fortran_error PRIVATE error_fortran)
3335
# LINKER_LANGUAGE option is necessary for ifort at least
3436
set_target_properties(cxx_fortran_error PROPERTIES LINKER_LANGUAGE CXX)
35-
add_test(NAME C++_Fortran_error COMMAND cxx_fortran_error)
37+
add_test(NAME C++_Fortran_error
38+
COMMAND ${CMAKE_COMMAND} -Dexe=$<TARGET_FILE:cxx_fortran_error> -Dexp_code=42 -P ${PROJECT_SOURCE_DIR}/cmake/test_error.cmake
39+
)
3640

3741
# -- test wrapup
38-
39-
set_tests_properties(C++_Fortran_error C_Fortran_error PROPERTIES WILL_FAIL TRUE)
40-
4142
set_tests_properties(C++_Fortran_math C++_Fortran_error Fortran_call_C Fortran_call_C++ PROPERTIES
4243
TIMEOUT 5
4344
)

0 commit comments

Comments
 (0)