Skip to content

Commit a84ab9a

Browse files
committed
improve layout
1 parent 93c59e4 commit a84ab9a

21 files changed

+85
-98
lines changed

CMakeLists.txt

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
# Demonstrate linking of
2-
# * C++ calling Fortran
3-
# * Fortran calling C++
4-
# * Fortran calling C
5-
#
6-
# https://stackoverflow.com/tags/fortran-iso-c-binding/info
7-
#
8-
# this also works between ABI compatible compilers
9-
# e.g. Clang and GCC.
10-
#
11-
# This also works for MacOS with Apple's Clang and Homebrew GCC,
12-
# by being sure you have in ~/.zshrc like the following:
13-
# (check directory / versions on your Mac)
14-
#
15-
# export LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib
16-
#
17-
# export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include
18-
# export CXXFLAGS=-I$CPLUS_INCLUDE_PATH
19-
# export CFLAGS=$CXXFLAGS
20-
#
21-
# in general, CMake >= 3.14 have better link resolution than CMake 3.13.
221
cmake_minimum_required(VERSION 3.14...3.21)
232

243
project(Fortran_C_CXX_interface
25-
LANGUAGES C Fortran CXX
26-
VERSION 1.1.1)
4+
LANGUAGES C CXX Fortran
5+
VERSION 1.2.0)
276

287
include(CTest)
298

@@ -32,6 +11,4 @@ include (cmake/compilers.cmake)
3211
set(CMAKE_C_STANDARD 99)
3312
set(CMAKE_CXX_STANDARD 11)
3413

35-
add_subdirectory(c)
36-
37-
add_subdirectory(cxx)
14+
add_subdirectory(src)

CMakePresets.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"configurePresets": [
55
{
66
"name": "default",
7-
"binaryDir": "${sourceDir}/build",
8-
"cacheVariables": {
9-
"CMAKE_BUILD_TYPE": "Release"
10-
}
7+
"binaryDir": "${sourceDir}/build"
118
},
129
{
1310
"name": "multi", "inherits": "default",

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@ For example:
1414
* Clang and Gfortran
1515
* (Windows) MSVC and Intel oneAPI ifort
1616

17-
## build
18-
19-
```sh
20-
cmake -B build
17+
Demonstrate linking of
2118

22-
cmake --build build
19+
* C++ calling Fortran
20+
* Fortran calling C++
21+
* Fortran calling C
2322

24-
ctest --test-dir build
25-
```
23+
https://stackoverflow.com/tags/fortran-iso-c-binding/info
2624

27-
## Example
25+
In general, CMake >= 3.14 has better link resolution than CMake 3.13.
26+
In general, strongly avoid the FortranCInterface of CMake and mangling function names--just use Fortran 2003 standard `bind(C)`
2827

29-
```fortran
30-
module flibs
28+
## MacOS
3129

32-
use,intrinsic:: iso_c_binding, only: c_int, c_float, c_double
30+
For MacOS with Apple's Clang and Homebrew GCC,
31+
be sure you have in ~/.zshrc like the following:
32+
(check directory / versions on your Mac)
3333

34-
implicit none
34+
export LIBRARY_PATH=$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/lib
35+
export CPLUS_INCLUDE_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/usr/include
36+
export CXXFLAGS=-I$CPLUS_INCLUDE_PATH
37+
export CFLAGS=$CXXFLAGS
3538

36-
subroutine cool(X,N) bind(c)
39+
## build
3740

38-
real(c_double), intent(inout) :: X
39-
integer(c_int), intent(in) :: N
41+
```sh
42+
cmake -B build
4043

41-
...
44+
cmake --build build
4245

43-
end subroutine cool
46+
ctest --test-dir build
4447
```
45-
46-
`bind(c)` makes the name `cool` available to C/C++.
47-
In general, strongly avoid the FortranCInterface of CMake and mangling function names--just use Fortran 2003 standard `bind(C)`

c/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

c/meson.build

Lines changed: 0 additions & 7 deletions
This file was deleted.

cxx/CMakeLists.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

cxx/meson.build

Lines changed: 0 additions & 9 deletions
This file was deleted.

meson.build

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
project('Fortran_c_cxx_interface', 'c', 'cpp', 'fortran',
2-
version : '1.1.1',
2+
version : '1.2.0',
33
meson_version: '>= 0.52.0',
4-
default_options : ['default_library=static', 'buildtype=release', 'warning_level=3',
4+
default_options : ['default_library=static', 'warning_level=3',
55
'cpp_std=c++11', 'c_std=c99'])
66

7-
fcc_src = files('f_call_c.f90')
8-
9-
subdir('c')
10-
11-
subdir('cxx')
7+
subdir('src')

src/CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
add_subdirectory(c)
2+
3+
add_subdirectory(cxx)
4+
5+
add_subdirectory(fortran)
6+
7+
# -- Fortran calling C++
8+
add_executable(fortran_call_cxx fortran/main.f90)
9+
target_link_libraries(fortran_call_cxx PRIVATE math_cxx)
10+
# LINKER_LANGUAGE option is necessary for ifort at least
11+
set_target_properties(fortran_call_cxx PROPERTIES LINKER_LANGUAGE Fortran)
12+
add_test(NAME Fortran_call_C++ COMMAND fortran_call_cxx)
13+
14+
# -- Fortran calling C
15+
add_executable(fortran_call_c fortran/main.f90)
16+
target_link_libraries(fortran_call_c PRIVATE math_c)
17+
add_test(NAME Fortran_call_C COMMAND fortran_call_c)
18+
19+
# -- C++ calling Fortran
20+
add_executable(cxx_call_fortran cxx/cxx_main.cxx)
21+
target_link_libraries(cxx_call_fortran PRIVATE math_fortran)
22+
add_test(NAME C++_call_Fortran COMMAND cxx_call_fortran)
23+
24+
set_tests_properties(C++_call_Fortran Fortran_call_C Fortran_call_C++ PROPERTIES
25+
TIMEOUT 5
26+
)

src/c/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_library(math_c OBJECT math.c)

0 commit comments

Comments
 (0)