Skip to content

Commit 97810e5

Browse files
authored
Update: cpp example package to use cpp stdlib. (#2)
Uses std:vector and std:algorithm to check that the cpp standard library is linked and called correctly.
1 parent 5100827 commit 97810e5

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

example_packages/cpp_files/src/cpp_files.f90

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ module cpp_files
33
implicit none
44
private
55

6-
public :: hello_world
6+
public :: intvec_maxval
77

88
interface
9-
subroutine hello_world() bind(C, name = "hello_world")
10-
end subroutine hello_world
9+
integer function intvec_maxval(array, n) bind(C, name = "intvec_maxval")
10+
integer, intent(in) :: array(*)
11+
integer, intent(in), value :: n
12+
end function intvec_maxval
1113
end interface
1214
end module cpp_files
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
#include <iostream>
1+
#include <vector>
2+
#include <algorithm>
23

34
extern "C" {
4-
int hello_world();
5-
}
65

7-
int hello_world() {
8-
std::cout << "Hello World";
6+
int intvec_maxval(int* array, size_t n){
7+
8+
std::vector<int> vec(array, array + n);
9+
10+
return *(std::max_element(vec.begin(), vec.end()));
11+
912
}
13+
14+
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
program check
2-
implicit none
2+
use cpp_files
3+
implicit none
4+
5+
integer :: i, max_element
6+
integer, parameter :: array(*) = [(i,i=-50,10)]
7+
8+
max_element = intvec_maxval(array,size(array,1))
9+
10+
if (max_element == maxval(array)) then
11+
write(*,*) ' PASSED: Max element is ',max_element
12+
else
13+
write(*,*) ' (!) FAILED: Incorrect max element returned'
14+
stop 1
15+
end if
316

4-
print *, "Put some tests in here!"
517
end program check

0 commit comments

Comments
 (0)