Skip to content

Commit 8da7d98

Browse files
committed
struct: use header file my_struct.h
1 parent 62f0b6d commit 8da7d98

File tree

7 files changed

+26
-35
lines changed

7 files changed

+26
-35
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ add_test(NAME C++_Fortran_error
5353
)
5454

5555
add_executable(cxx_fortran_struct cxx/struct_main.cxx)
56+
target_include_directories(cxx_fortran_struct PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/c)
5657
target_link_libraries(cxx_fortran_struct PRIVATE struct_fortran)
5758
set_target_properties(cxx_fortran_struct PROPERTIES LINKER_LANGUAGE CXX)
5859
add_test(NAME C++_Fortran_struct COMMAND $<TARGET_FILE:cxx_fortran_struct>)

src/c/my_struct.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
enum { LMAX = 1000 };
6+
7+
struct params {
8+
// order and lengths must match in Fortran and C
9+
int my_int;
10+
bool my_bool;
11+
char my_char[LMAX];
12+
};
13+
14+
void struct_check(struct params);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif

src/c/struct_lib.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,8 @@
33
#include <stdlib.h>
44
#include <stdio.h>
55

6-
struct params {
7-
// order and lengths must match in Fortran and C
8-
int my_int;
9-
bool my_bool;
10-
char my_char[1000];
11-
};
6+
#include "my_struct.h"
127

13-
void struct_check(struct params);
148

159
void struct_check(struct params s) {
1610

src/c/struct_main.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@
22
#include <string.h>
33
#include <stdlib.h>
44

5-
enum { Lchar = 1000 };
6-
7-
struct params {
8-
// order and lengths must match in Fortran and C
9-
int my_int;
10-
bool my_bool;
11-
char my_char[Lchar];
12-
};
13-
14-
extern void struct_check(struct params *);
5+
#include "my_struct.h"
156

167
int main(void) {
178

@@ -21,7 +12,7 @@ s.my_int = 123;
2112
s.my_bool = true;
2213
strcpy(s.my_char, "Hello");
2314

24-
struct_check(&s);
15+
struct_check(s);
2516

2617
return EXIT_SUCCESS;
2718

src/cxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_library(math_cxx OBJECT math_lib.cxx)
22

33
add_library(struct_cxx OBJECT struct_lib.cxx)
4+
target_include_directories(struct_cxx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../c)

src/cxx/struct_lib.cxx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
#include <cstring>
33
#include <cstdlib>
44

5-
struct params {
6-
// order and lengths must match in Fortran and C
7-
int my_int;
8-
bool my_bool;
9-
char my_char[1000];
10-
};
11-
12-
extern "C" void struct_check(struct params);
5+
#include "my_struct.h"
136

147
void struct_check(struct params s) {
158

src/cxx/struct_main.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
#include <cstring>
22
#include <cstdlib>
33

4-
struct params {
5-
// order and lengths must match in Fortran and C
6-
int my_int;
7-
bool my_bool;
8-
char my_char[1000];
9-
};
10-
11-
extern "C" void struct_check(struct params *);
4+
#include "my_struct.h"
125

136
int main() {
147

@@ -18,7 +11,7 @@ s.my_int = 123;
1811
s.my_bool = true;
1912
strcpy(s.my_char, "Hello");
2013

21-
struct_check(&s);
14+
struct_check(s);
2215

2316
return EXIT_SUCCESS;
2417

0 commit comments

Comments
 (0)