質問するログイン新規登録

質問編集履歴

2

修正したがまだコンパイルできないので、再投稿

2016/10/10 06:12

投稿

cingyan
cingyan

スコア29

title CHANGED
File without changes
body CHANGED
@@ -19,11 +19,17 @@
19
19
  Ubuntu 15.10を使用しています。
20
20
  よろしくお願いします。
21
21
  ---
22
- 追記
22
+ 追記 : 16/10/10
23
+ 教えて頂いた通りに修正しましたがまだ、コンパイルができず、実行ファイルができません。
23
- 本から写したのです。打ち間違があるかもせん
24
+ 修正したコードと実行結果を載せますのでよろしくお願いしま
24
25
 
25
26
  07_01.c
27
+ ```
28
+ #include <stdlib.h>
29
+ #include <unistd.h>
30
+ #include <fcntl.h>
31
+ #include <signal.h>
26
- ```#include <sys/time.h>
32
+ #include <sys/time.h>
27
33
  #define DATA_FILENAME "./stopwatch.dat"
28
34
 
29
35
  int iniTime(struct timeval *startTime)
@@ -34,7 +40,7 @@
34
40
 
35
41
  if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
36
42
  return -1;
37
- if(Write(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
43
+ if(write(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
38
44
  ret=-1;
39
45
  close(fd);
40
46
  return ret;
@@ -43,12 +49,18 @@
43
49
  ---
44
50
  07_02.c
45
51
  ```
52
+ #include <stdlib.h>
53
+ #include <unistd.h>
54
+ #include <sys/time.h>
55
+ #include <fcntl.h>
56
+ #include <signal.h>
57
+ #define DATA_FILENAME "./stopwatch.dat"
46
58
  int restoreTime(struct timeval *startTime)
47
59
  {
48
- int fs,ret=0;
60
+ int fd,ret=0;
49
61
  if((fd=open(DATA_FILENAME,O_RDONLY))<0)
50
62
  return -1;
51
- if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
63
+ if(read(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
52
64
  ret =-1;
53
65
  close(fd);
54
66
  return ret;
@@ -57,12 +69,19 @@
57
69
  ---
58
70
  07_04.c
59
71
  ```
72
+ #include <stdlib.h>
73
+ #include <unistd.h>
74
+ #include <sys/time.h>
75
+ #include <fcntl.h>
76
+ #include <signal.h>
77
+ int restoreTime(struct timeval *startTime);
78
+ void printDiff(struct timeval *t1,struct timeval *t2);
60
79
  void lastTime(int unused)
61
80
  {
62
81
  struct timeval startTime,lastTime;
63
82
 
64
83
  if(restoreTime(&startTime)<0)
65
- exit(EXIT_FAILRE);
84
+ exit(EXIT_FAILURE);
66
85
  gettimeofday(&lastTime,NULL);
67
86
  printDiff(&lastTime,&startTime);
68
87
  exit(EXIT_SUCCESS);
@@ -71,6 +90,12 @@
71
90
  ---
72
91
  07_05.c
73
92
  ```
93
+ #include <stdio.h>
94
+ #include <stdlib.h>
95
+ #include <unistd.h>
96
+ #include <sys/time.h>
97
+ #include <fcntl.h>
98
+ #include <signal.h>
74
99
  void printDiff(struct timeval *t1,struct timeval *t2)
75
100
  {
76
101
  struct timeval diff;
@@ -84,14 +109,15 @@
84
109
  ```
85
110
  ---
86
111
  main.c
112
+ ```
87
- ```#include <stdio.h>
113
+ #include <stdio.h>
88
114
  #include <stdlib.h>
89
115
  #include <unistd.h>
90
116
  #include <sys/time.h>
91
117
  #include <fcntl.h>
92
118
  #include <signal.h>
93
119
 
94
- int iniTime(struct timeval *startTime);
120
+ int initTime(struct timeval *startTime);
95
121
  int restoreTime(struct timeval *startTime);
96
122
  void lastTime(int unused);
97
123
  void printDiff(struct timeval *t1,struct timeval *t2);
@@ -139,113 +165,8 @@
139
165
  ターミナルでコンパイルすると、
140
166
  ```
141
167
  $ gcc 07_01.c 07_02.c 07_04.c 07_05.c main.c
142
- 07_01.c: In function ‘iniTime’:
143
- 07_01.c:8:25: error: ‘NULL’ undeclared (first use in this function)
144
- gettimeofday(startTime,NULL);
145
- ^
146
- 07_01.c:8:25: note: each undeclared identifier is reported only once for each function it appears in
147
- 07_01.c:10:9: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
148
- if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
149
- ^
150
- 07_01.c:10:28: error: ‘O_WRONLY’ undeclared (first use in this function)
151
- if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
152
- ^
153
- 07_01.c:10:37: error: ‘O_CREAT’ undeclared (first use in this function)
154
- if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
155
- ^
156
- 07_01.c:10:45: error: ‘O_TRUNC’ undeclared (first use in this function)
157
- if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
158
- ^
159
- 07_01.c:12:5: warning: implicit declaration of function ‘Write’ [-Wimplicit-function-declaration]
160
- if(Write(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
161
- ^
162
- 07_01.c:14:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
163
- close(fd);
164
- ^
165
- 07_02.c:1:24: warning: ‘struct timeval’ declared inside parameter list
166
- int restoreTime(struct timeval *startTime)
167
- ^
168
- 07_02.c:1:24: warning: its scope is only this definition or declaration, which is probably not what you want
169
- 07_02.c: In function ‘restoreTime’:
170
- 07_02.c:4:6: error: ‘fd’ undeclared (first use in this function)
171
- if((fd=open(DATA_FILENAME,O_RDONLY))<0)
168
+ /tmp/cc8inniA.o: 関数 `main' 内:
172
- ^
173
- 07_02.c:4:6: note: each undeclared identifier is reported only once for each function it appears in
174
- 07_02.c:4:9: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
175
- if((fd=open(DATA_FILENAME,O_RDONLY))<0)
176
- ^
177
- 07_02.c:4:14: error: ‘DATA_FILENAME’ undeclared (first use in this function)
178
- if((fd=open(DATA_FILENAME,O_RDONLY))<0)
179
- ^
180
- 07_02.c:4:28: error: ‘O_RDONLY’ undeclared (first use in this function)
181
- if((fd=open(DATA_FILENAME,O_RDONLY))<0)
182
- ^
183
- 07_02.c:6:5: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
184
- if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
185
- ^
186
- 07_02.c:6:13: error: ‘strtTime’ undeclared (first use in this function)
187
- if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
188
- ^
189
- 07_02.c:6:29: error: dereferencing pointer to incomplete type ‘struct timeval’
190
- if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
191
- ^
192
- 07_02.c:8:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
193
- close(fd);
194
- ^
195
- 07_04.c: In function ‘lastTime’:
196
- 07_04.c:3:17: error: storage size of ‘startTime’ isn’t known
197
- struct timeval startTime,lastTime;
198
- ^
199
- 07_04.c:3:27: error: storage size of ‘lastTime’ isn’t known
200
- struct timeval startTime,lastTime;
201
- ^
202
- 07_04.c:5:5: warning: implicit declaration of function ‘restoreTime’ [-Wimplicit-function-declaration]
203
- if(restoreTime(&startTime)<0)
204
- ^
205
- 07_04.c:6:3: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
206
- exit(EXIT_FAILRE);
207
- ^
208
- 07_04.c:6:3: warning: incompatible implicit declaration of built-in function ‘exit’
209
- 07_04.c:6:3: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit’
210
- 07_04.c:6:8: error: ‘EXIT_FAILRE’ undeclared (first use in this function)
211
- exit(EXIT_FAILRE);
212
- ^
213
- 07_04.c:6:8: note: each undeclared identifier is reported only once for each function it appears in
214
- 07_04.c:7:2: warning: implicit declaration of function ‘gettimeofday’ [-Wimplicit-function-declaration]
215
- gettimeofday(&lastTime,NULL);
216
- ^
217
- 07_04.c:7:25: error: ‘NULL’ undeclared (first use in this function)
218
- gettimeofday(&lastTime,NULL);
219
- ^
220
- 07_04.c:8:2: warning: implicit declaration of function ‘printDiff’ [-Wimplicit-function-declaration]
221
- printDiff(&lastTime,&startTime);
169
+ main.c:(.text+0x4a): `initTime' に対する定義されていない参照です
222
- ^
223
- 07_04.c:9:2: warning: incompatible implicit declaration of built-in function ‘exit’
224
- exit(EXIT_SUCCESS);
225
- ^
226
- 07_04.c:9:2: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit’
227
- 07_04.c:9:7: error: ‘EXIT_SUCCESS’ undeclared (first use in this function)
228
- exit(EXIT_SUCCESS);
229
- ^
230
- 07_05.c:1:42: warning: ‘struct timeval’ declared inside parameter list
231
- void printDiff(struct timeval *t1,struct timeval *t2)
232
- ^
233
- 07_05.c:1:42: warning: its scope is only this definition or declaration, which is probably not what you want
234
- 07_05.c: In function ‘printDiff’:
235
- 07_05.c:3:17: error: storage size of ‘diff’ isn’t known
170
+ collect2: error: ld returned 1 exit status
236
- struct timeval diff;
237
- ^
238
- 07_05.c:5:2: warning: implicit declaration of function ‘timersub’ [-Wimplicit-function-declaration]
239
- timersub(t1,t2,&diff);
240
- ^
241
- 07_05.c:6:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
242
- printf("%02lu:%2lu.%02lu\n",
243
- ^
244
- 07_05.c:6:2: warning: incompatible implicit declaration of built-in function ‘printf’
245
- 07_05.c:6:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
246
- main.c: In function ‘main’:
247
- main.c:23:7: warning: implicit declaration of function ‘initTime’ [-Wimplicit-function-declaration]
248
- if(initTime(&startTime)<0)
249
- ^
250
171
  $
251
172
  ```

1

コードを伝える。

2016/10/10 06:12

投稿

cingyan
cingyan

スコア29

title CHANGED
File without changes
body CHANGED
@@ -17,4 +17,235 @@
17
17
  POSIXと記録されているので、gccでもシステムコールの関数が使えると思うのですが、やり方を教えてください。
18
18
  それともgccではできないのでしょうか。
19
19
  Ubuntu 15.10を使用しています。
20
- よろしくお願いします。
20
+ よろしくお願いします。
21
+ ---
22
+ 追記
23
+ 本から写したものです。打ち間違いがあるかもしれません。
24
+
25
+ 07_01.c
26
+ ```#include <sys/time.h>
27
+ #define DATA_FILENAME "./stopwatch.dat"
28
+
29
+ int iniTime(struct timeval *startTime)
30
+ {
31
+ int fd,ret=0;
32
+
33
+ gettimeofday(startTime,NULL);
34
+
35
+ if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
36
+ return -1;
37
+ if(Write(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
38
+ ret=-1;
39
+ close(fd);
40
+ return ret;
41
+ }
42
+ ```
43
+ ---
44
+ 07_02.c
45
+ ```
46
+ int restoreTime(struct timeval *startTime)
47
+ {
48
+ int fs,ret=0;
49
+ if((fd=open(DATA_FILENAME,O_RDONLY))<0)
50
+ return -1;
51
+ if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
52
+ ret =-1;
53
+ close(fd);
54
+ return ret;
55
+ }
56
+ ```
57
+ ---
58
+ 07_04.c
59
+ ```
60
+ void lastTime(int unused)
61
+ {
62
+ struct timeval startTime,lastTime;
63
+
64
+ if(restoreTime(&startTime)<0)
65
+ exit(EXIT_FAILRE);
66
+ gettimeofday(&lastTime,NULL);
67
+ printDiff(&lastTime,&startTime);
68
+ exit(EXIT_SUCCESS);
69
+ }
70
+ ```
71
+ ---
72
+ 07_05.c
73
+ ```
74
+ void printDiff(struct timeval *t1,struct timeval *t2)
75
+ {
76
+ struct timeval diff;
77
+
78
+ timersub(t1,t2,&diff);
79
+ printf("%02lu:%2lu.%02lu\n",
80
+ diff.tv_sec/60,
81
+ diff.tv_sec%60,
82
+ diff.tv_usec/10000);
83
+ }
84
+ ```
85
+ ---
86
+ main.c
87
+ ```#include <stdio.h>
88
+ #include <stdlib.h>
89
+ #include <unistd.h>
90
+ #include <sys/time.h>
91
+ #include <fcntl.h>
92
+ #include <signal.h>
93
+
94
+ int iniTime(struct timeval *startTime);
95
+ int restoreTime(struct timeval *startTime);
96
+ void lastTime(int unused);
97
+ void printDiff(struct timeval *t1,struct timeval *t2);
98
+
99
+ int main(int argc,char *argv[])
100
+ {
101
+ int c;
102
+ struct timeval startTime={0,0},currentTime;
103
+
104
+ while((c=getopt(argc,argv,"cs"))!=EOF)
105
+ {
106
+ switch(c)
107
+ {
108
+ case 's':
109
+ if(initTime(&startTime)<0)
110
+ {
111
+ fprintf(stderr,"開始時刻の初期化、書き込みに失敗\n");
112
+ exit(EXIT_FAILURE);
113
+ }
114
+ break;
115
+ case 'c':
116
+ if(restoreTime(&startTime)<0)
117
+ {
118
+ fprintf(stderr,"開始時刻の読み込みに失敗\n");
119
+ exit(EXIT_FAILURE);
120
+ }
121
+ break;
122
+ }
123
+ }
124
+ if(startTime.tv_sec==0||argc!=2)
125
+ {
126
+ fprintf(stderr,"-sまたは-cオプションを指定してください。\n");
127
+ exit(EXIT_FAILURE);
128
+ }
129
+ signal(SIGINT,lastTime);
130
+ while(1)
131
+ {
132
+ sleep(1);
133
+ gettimeofday(&currentTime,NULL);
134
+ printDiff(&currentTime,&startTime);
135
+ }
136
+ exit(EXIT_SUCCESS);
137
+ }
138
+ ```
139
+ ターミナルでコンパイルすると、
140
+ ```
141
+ $ gcc 07_01.c 07_02.c 07_04.c 07_05.c main.c
142
+ 07_01.c: In function ‘iniTime’:
143
+ 07_01.c:8:25: error: ‘NULL’ undeclared (first use in this function)
144
+ gettimeofday(startTime,NULL);
145
+ ^
146
+ 07_01.c:8:25: note: each undeclared identifier is reported only once for each function it appears in
147
+ 07_01.c:10:9: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
148
+ if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
149
+ ^
150
+ 07_01.c:10:28: error: ‘O_WRONLY’ undeclared (first use in this function)
151
+ if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
152
+ ^
153
+ 07_01.c:10:37: error: ‘O_CREAT’ undeclared (first use in this function)
154
+ if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
155
+ ^
156
+ 07_01.c:10:45: error: ‘O_TRUNC’ undeclared (first use in this function)
157
+ if((fd=open(DATA_FILENAME,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
158
+ ^
159
+ 07_01.c:12:5: warning: implicit declaration of function ‘Write’ [-Wimplicit-function-declaration]
160
+ if(Write(fd,startTime,sizeof(*startTime))!=sizeof(*startTime))
161
+ ^
162
+ 07_01.c:14:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
163
+ close(fd);
164
+ ^
165
+ 07_02.c:1:24: warning: ‘struct timeval’ declared inside parameter list
166
+ int restoreTime(struct timeval *startTime)
167
+ ^
168
+ 07_02.c:1:24: warning: its scope is only this definition or declaration, which is probably not what you want
169
+ 07_02.c: In function ‘restoreTime’:
170
+ 07_02.c:4:6: error: ‘fd’ undeclared (first use in this function)
171
+ if((fd=open(DATA_FILENAME,O_RDONLY))<0)
172
+ ^
173
+ 07_02.c:4:6: note: each undeclared identifier is reported only once for each function it appears in
174
+ 07_02.c:4:9: warning: implicit declaration of function ‘open’ [-Wimplicit-function-declaration]
175
+ if((fd=open(DATA_FILENAME,O_RDONLY))<0)
176
+ ^
177
+ 07_02.c:4:14: error: ‘DATA_FILENAME’ undeclared (first use in this function)
178
+ if((fd=open(DATA_FILENAME,O_RDONLY))<0)
179
+ ^
180
+ 07_02.c:4:28: error: ‘O_RDONLY’ undeclared (first use in this function)
181
+ if((fd=open(DATA_FILENAME,O_RDONLY))<0)
182
+ ^
183
+ 07_02.c:6:5: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
184
+ if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
185
+ ^
186
+ 07_02.c:6:13: error: ‘strtTime’ undeclared (first use in this function)
187
+ if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
188
+ ^
189
+ 07_02.c:6:29: error: dereferencing pointer to incomplete type ‘struct timeval’
190
+ if(read(fd,strtTime,sizeof(*startTime))!=sizeof(*startTime))
191
+ ^
192
+ 07_02.c:8:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
193
+ close(fd);
194
+ ^
195
+ 07_04.c: In function ‘lastTime’:
196
+ 07_04.c:3:17: error: storage size of ‘startTime’ isn’t known
197
+ struct timeval startTime,lastTime;
198
+ ^
199
+ 07_04.c:3:27: error: storage size of ‘lastTime’ isn’t known
200
+ struct timeval startTime,lastTime;
201
+ ^
202
+ 07_04.c:5:5: warning: implicit declaration of function ‘restoreTime’ [-Wimplicit-function-declaration]
203
+ if(restoreTime(&startTime)<0)
204
+ ^
205
+ 07_04.c:6:3: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
206
+ exit(EXIT_FAILRE);
207
+ ^
208
+ 07_04.c:6:3: warning: incompatible implicit declaration of built-in function ‘exit’
209
+ 07_04.c:6:3: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit’
210
+ 07_04.c:6:8: error: ‘EXIT_FAILRE’ undeclared (first use in this function)
211
+ exit(EXIT_FAILRE);
212
+ ^
213
+ 07_04.c:6:8: note: each undeclared identifier is reported only once for each function it appears in
214
+ 07_04.c:7:2: warning: implicit declaration of function ‘gettimeofday’ [-Wimplicit-function-declaration]
215
+ gettimeofday(&lastTime,NULL);
216
+ ^
217
+ 07_04.c:7:25: error: ‘NULL’ undeclared (first use in this function)
218
+ gettimeofday(&lastTime,NULL);
219
+ ^
220
+ 07_04.c:8:2: warning: implicit declaration of function ‘printDiff’ [-Wimplicit-function-declaration]
221
+ printDiff(&lastTime,&startTime);
222
+ ^
223
+ 07_04.c:9:2: warning: incompatible implicit declaration of built-in function ‘exit’
224
+ exit(EXIT_SUCCESS);
225
+ ^
226
+ 07_04.c:9:2: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit’
227
+ 07_04.c:9:7: error: ‘EXIT_SUCCESS’ undeclared (first use in this function)
228
+ exit(EXIT_SUCCESS);
229
+ ^
230
+ 07_05.c:1:42: warning: ‘struct timeval’ declared inside parameter list
231
+ void printDiff(struct timeval *t1,struct timeval *t2)
232
+ ^
233
+ 07_05.c:1:42: warning: its scope is only this definition or declaration, which is probably not what you want
234
+ 07_05.c: In function ‘printDiff’:
235
+ 07_05.c:3:17: error: storage size of ‘diff’ isn’t known
236
+ struct timeval diff;
237
+ ^
238
+ 07_05.c:5:2: warning: implicit declaration of function ‘timersub’ [-Wimplicit-function-declaration]
239
+ timersub(t1,t2,&diff);
240
+ ^
241
+ 07_05.c:6:2: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
242
+ printf("%02lu:%2lu.%02lu\n",
243
+ ^
244
+ 07_05.c:6:2: warning: incompatible implicit declaration of built-in function ‘printf’
245
+ 07_05.c:6:2: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
246
+ main.c: In function ‘main’:
247
+ main.c:23:7: warning: implicit declaration of function ‘initTime’ [-Wimplicit-function-declaration]
248
+ if(initTime(&startTime)<0)
249
+ ^
250
+ $
251
+ ```