Skip to content

Commit

Permalink
fortran code sample for 2nd exercise (conditioals)
Browse files Browse the repository at this point in the history
  • Loading branch information
code4yonglei committed Sep 4, 2024
1 parent 0d182f1 commit 9593a59
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
15 changes: 15 additions & 0 deletions content/code/02_conditionals-f/hello-world.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
program hello_world

use messaging

implicit none

type(Messenger) :: hello, bye

hello%message_ = 'Hello, CMake world!'
print *, print_message(hello)

bye%message_ = 'Bye, CMake world!'
print *, print_message(bye)

end program
25 changes: 25 additions & 0 deletions content/code/02_conditionals-f/message.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module messaging

implicit none

public Messenger
type Messenger
character(len=19) :: message_
end type

public print_message

private

contains

pure function print_message(postman) result(m)

type(Messenger), intent(in) :: postman
character(len=19) :: m

m = postman%message_

end function

end module
19 changes: 19 additions & 0 deletions content/code/02_conditionals-f/solution/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# set minimum cmake version
cmake_minimum_required(VERSION 3.18)

# project name and language
project(conditionals LANGUAGES Fortran)

set(MAKE_SHARED_LIBRARY OFF)

if(MAKE_SHARED_LIBRARY)
message(STATUS "Build shared library")
add_library(message SHARED message.f90)
else()
message(STATUS "Build static library")
add_library(message STATIC message.f90)
endif()

add_executable(hello-world hello-world.f90)

target_link_libraries(hello-world PRIVATE message)
15 changes: 15 additions & 0 deletions content/code/02_conditionals-f/solution/hello-world.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
program hello_world

use messaging

implicit none

type(Messenger) :: hello, bye

hello%message_ = 'Hello, CMake world!'
print *, print_message(hello)

bye%message_ = 'Bye, CMake world!'
print *, print_message(bye)

end program
25 changes: 25 additions & 0 deletions content/code/02_conditionals-f/solution/message.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module messaging

implicit none

public Messenger
type Messenger
character(len=19) :: message_
end type

public print_message

private

contains

pure function print_message(postman) result(m)

type(Messenger), intent(in) :: postman
character(len=19) :: m

m = postman%message_

end function

end module

0 comments on commit 9593a59

Please sign in to comment.