File tree Expand file tree Collapse file tree 3 files changed +25
-15
lines changed Expand file tree Collapse file tree 3 files changed +25
-15
lines changed Original file line number Diff line number Diff line change @@ -16,13 +16,9 @@ For example:
1616
1717Demonstrate 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.
2622In 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
4743export CXXFLAGS=-I$CPLUS_INCLUDE_PATH
4844export 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 )
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change @@ -21,7 +21,9 @@ add_executable(c_fortran_error c/error_main.c)
2121target_link_libraries (c_fortran_error PRIVATE error_fortran)
2222# LINKER_LANGUAGE option is necessary for ifort at least
2323set_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
2729add_executable (cxx_call_fortran cxx/main.cxx)
@@ -32,12 +34,11 @@ add_executable(cxx_fortran_error cxx/error_main.cxx)
3234target_link_libraries (cxx_fortran_error PRIVATE error_fortran)
3335# LINKER_LANGUAGE option is necessary for ifort at least
3436set_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-
4142set_tests_properties (C++_Fortran_math C++_Fortran_error Fortran_call_C Fortran_call_C++ PROPERTIES
4243TIMEOUT 5
4344)
You can’t perform that action at this time.
0 commit comments