-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstatic_pattern_rules.html
More file actions
1017 lines (574 loc) · 41.6 KB
/
static_pattern_rules.html
File metadata and controls
1017 lines (574 loc) · 41.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="ko" >
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-161001174-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-161001174-4');
</script>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Static Pattern Rules | Introduction</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 2.6.7">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
<link rel="next" href="./recipes.html" />
<link rel="prev" href="./pattern_rules.html" />
</head>
<body>
<div class="book"
data-level="5.3"
data-chapter-title="Static Pattern Rules"
data-filepath="static_pattern_rules.md"
data-basepath="."
data-innerlanguage="">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="0" data-path="index.html">
<a href="./index.html">
<i class="fa fa-check"></i>
Introduction
</a>
</li>
<li class="chapter " data-level="1" data-path="bug_reports.html">
<a href="./bug_reports.html">
<i class="fa fa-check"></i>
<b>1.</b>
Bug reports
</a>
</li>
<li class="chapter " data-level="2" data-path="variables_and_functions.html">
<a href="./variables_and_functions.html">
<i class="fa fa-check"></i>
<b>2.</b>
Variables and Functions
</a>
<ul class="articles">
<li class="chapter " data-level="2.1" data-path="multiline_variables.html">
<a href="./multiline_variables.html">
<i class="fa fa-check"></i>
<b>2.1.</b>
Multi-Line Variables
</a>
</li>
<li class="chapter " data-level="2.2" data-path="target_specific_variables.html">
<a href="./target_specific_variables.html">
<i class="fa fa-check"></i>
<b>2.2.</b>
Target-Specific Variables
</a>
</li>
<li class="chapter " data-level="2.3" data-path="automatic_variables.html">
<a href="./automatic_variables.html">
<i class="fa fa-check"></i>
<b>2.3.</b>
Automatic Variables
</a>
</li>
<li class="chapter " data-level="2.4" data-path="substitution_references.html">
<a href="./substitution_references.html">
<i class="fa fa-check"></i>
<b>2.4.</b>
Substitution References
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="3" data-path="special_variables.html">
<a href="./special_variables.html">
<i class="fa fa-check"></i>
<b>3.</b>
Special Variables
</a>
</li>
<li class="chapter " data-level="4" data-path="directives.html">
<a href="./directives.html">
<i class="fa fa-check"></i>
<b>4.</b>
Directives
</a>
<ul class="articles">
<li class="chapter " data-level="4.1" data-path="conditional_directives.html">
<a href="./conditional_directives.html">
<i class="fa fa-check"></i>
<b>4.1.</b>
Conditional Directives
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="5" data-path="rules.html">
<a href="./rules.html">
<i class="fa fa-check"></i>
<b>5.</b>
Rules
</a>
<ul class="articles">
<li class="chapter " data-level="5.1" data-path="double_colon_rules.html">
<a href="./double_colon_rules.html">
<i class="fa fa-check"></i>
<b>5.1.</b>
Double-Colon Rules
</a>
</li>
<li class="chapter " data-level="5.2" data-path="pattern_rules.html">
<a href="./pattern_rules.html">
<i class="fa fa-check"></i>
<b>5.2.</b>
Pattern Rules
</a>
</li>
<li class="chapter active" data-level="5.3" data-path="static_pattern_rules.html">
<a href="./static_pattern_rules.html">
<i class="fa fa-check"></i>
<b>5.3.</b>
Static Pattern Rules
</a>
</li>
<li class="chapter " data-level="5.4" data-path="recipes.html">
<a href="./recipes.html">
<i class="fa fa-check"></i>
<b>5.4.</b>
Recipes
</a>
</li>
<li class="chapter " data-level="5.5" data-path="DAG.html">
<a href="./DAG.html">
<i class="fa fa-check"></i>
<b>5.5.</b>
DAG
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="6" data-path="special_targets.html">
<a href="./special_targets.html">
<i class="fa fa-check"></i>
<b>6.</b>
Special Targets
</a>
</li>
<li class="chapter " data-level="7" data-path="recursive_make.html">
<a href="./recursive_make.html">
<i class="fa fa-check"></i>
<b>7.</b>
Recursive Make
</a>
</li>
<li class="chapter " data-level="8" data-path="include.html">
<a href="./include.html">
<i class="fa fa-check"></i>
<b>8.</b>
Include
</a>
</li>
<li class="chapter " data-level="9" data-path="one_DAG.html">
<a href="./one_DAG.html">
<i class="fa fa-check"></i>
<b>9.</b>
One DAG
</a>
</li>
<li class="chapter " data-level="10" data-path="parallel_execution.html">
<a href="./parallel_execution.html">
<i class="fa fa-check"></i>
<b>10.</b>
Parallel Execution
</a>
</li>
<li class="chapter " data-level="11" data-path="builtin_functions.html">
<a href="./builtin_functions.html">
<i class="fa fa-check"></i>
<b>11.</b>
Built-in Functions
</a>
<ul class="articles">
<li class="chapter " data-level="11.1" data-path="builtin_functions/text_functions.html">
<a href="./builtin_functions/text_functions.html">
<i class="fa fa-check"></i>
<b>11.1.</b>
Text Functions
</a>
</li>
<li class="chapter " data-level="11.2" data-path="builtin_functions/file_name_functions.html">
<a href="./builtin_functions/file_name_functions.html">
<i class="fa fa-check"></i>
<b>11.2.</b>
File Name Functions
</a>
</li>
<li class="chapter " data-level="11.3" data-path="builtin_functions/conditional_functions.html">
<a href="./builtin_functions/conditional_functions.html">
<i class="fa fa-check"></i>
<b>11.3.</b>
Conditional Functions
</a>
</li>
<li class="chapter " data-level="11.4" data-path="builtin_functions/foreach_function.html">
<a href="./builtin_functions/foreach_function.html">
<i class="fa fa-check"></i>
<b>11.4.</b>
Foreach Function
</a>
</li>
<li class="chapter " data-level="11.5" data-path="builtin_functions/file_function.html">
<a href="./builtin_functions/file_function.html">
<i class="fa fa-check"></i>
<b>11.5.</b>
File Function
</a>
</li>
<li class="chapter " data-level="11.6" data-path="builtin_functions/call_function.html">
<a href="./builtin_functions/call_function.html">
<i class="fa fa-check"></i>
<b>11.6.</b>
Call Function
</a>
</li>
<li class="chapter " data-level="11.7" data-path="builtin_functions/eval_function.html">
<a href="./builtin_functions/eval_function.html">
<i class="fa fa-check"></i>
<b>11.7.</b>
Eval Function
</a>
</li>
<li class="chapter " data-level="11.8" data-path="builtin_functions/value_function.html">
<a href="./builtin_functions/value_function.html">
<i class="fa fa-check"></i>
<b>11.8.</b>
Value Function
</a>
</li>
<li class="chapter " data-level="11.9" data-path="builtin_functions/origin_function.html">
<a href="./builtin_functions/origin_function.html">
<i class="fa fa-check"></i>
<b>11.9.</b>
Origin Function
</a>
</li>
<li class="chapter " data-level="11.10" data-path="builtin_functions/flavor_function.html">
<a href="./builtin_functions/flavor_function.html">
<i class="fa fa-check"></i>
<b>11.10.</b>
Flavor Function
</a>
</li>
<li class="chapter " data-level="11.11" data-path="builtin_functions/make_control_functions.html">
<a href="./builtin_functions/make_control_functions.html">
<i class="fa fa-check"></i>
<b>11.11.</b>
Make Control Functions
</a>
</li>
<li class="chapter " data-level="11.12" data-path="builtin_functions/shell_function.html">
<a href="./builtin_functions/shell_function.html">
<i class="fa fa-check"></i>
<b>11.12.</b>
Shell Function
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="12" data-path="command_line_options.html">
<a href="./command_line_options.html">
<i class="fa fa-check"></i>
<b>12.</b>
Command-line Options
</a>
</li>
<li class="chapter " data-level="13" data-path="debugging.html">
<a href="./debugging.html">
<i class="fa fa-check"></i>
<b>13.</b>
Debugging
</a>
</li>
<li class="chapter " data-level="14" data-path="tips.html">
<a href="./tips.html">
<i class="fa fa-check"></i>
<b>14.</b>
Tips
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Actions Left -->
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="./" >Introduction</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<h1 id="static-pattern-rules">Static Pattern Rules</h1>
<p><img src="images/static_pattern_rules.png" alt=""></p>
<p>Pattern rule 은 아주 유용한 기능이지만
한가지 단점이 매칭되는 범위가 너무 넓다는 것입니다.
따라서 의도치 않게 매칭이 발생할 여지가 있기 때문에 주의할 필요가 있습니다.
<code>%.o : %.c</code> 패턴의 경우 다음과 같은 타겟들이 모두 매칭되어 실행될 수 있습니다.</p>
<pre><code class="lang-makefile">%.o : %.c
main.o main.c
foo/main.o foo/main.c
foo/bar/main.o foo/bar/main.c
. . . .
</code></pre>
<p>앞에 디렉토리를 추가해서 제한을 하더라도 다음과 같은 타겟들이 모두 매칭이 됩니다.</p>
<pre><code class="lang-makefile">BUILD/lib/%.o : lib/%.c
BUILD/lib/main.o lib/main.c
BUILD/lib/foo/main.o lib/foo/main.c
BUILD/lib/foo/bar/main.o lib/foo/bar/main.c
. . . .
</code></pre>
<p>또한 예를 들어 <code>lib</code> 디렉토리에 포함된 파일들을 두 그룹으로 나누어서
첫 번째 그룹에는 A rule 을 적용시키고 두 번째 그룹에는 B rule 을 적용시킬 수가 없습니다.
이것은 아주 안되는 것은 아닌데 다음과 같이 먼저 파일 이름을 변경한 후에 패턴을 적용해야 합니다.</p>
<pre><code class="lang-makefile">BUILD/lib/A_%.o : lib/A_%.c <span class="hljs-comment"># A 그룹에 적용</span>
command ....
BUILD/lib/B_%.o : lib/B_%.c <span class="hljs-comment"># B 그룹에 적용</span>
command ....
</code></pre>
<p>static pattern rule 을 이용하면 이와 같은 문제들을 쉽게 해결할 수 있습니다.
이름에 포함된 static 의 의미는 pattern rule 을 작성할때 먼저 앞에
적용할 파일들을 명시한다는 뜻입니다.
따라서 해당 파일들에 대해서만 패턴이 적용되게 합니다.
다음 예제를 보면 왼쪽 match-anything pattern rule 의 경우는 아무 파일이나 전부 매칭이 되지만
static pattern rule 의 경우는 앞에 지정한 파일 외에는 매칭이 되지 않는 것을 볼 수 있습니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">MAKEFLAGS</span> := -rR OBJS := foo.o bar.o zoo.o
% : $(OBJS) : % :
@echo target : $@ @echo target : $@
<span class="hljs-comment">####### 실행 결과 ####### ####### 실행 결과 #######</span>
sh$ make foo.o bar.o zoo.o xxx.o sh$ make foo.o bar.o zoo.o xxx.o
target : foo.o target : foo.o
target : bar.o target : bar.o
target : zoo.o target : zoo.o
target : xxx.o make: *** No rule to make target 'xxx.o'. Stop.
</code></pre>
<p>다음은 실행파일을 빌드 하는데 static pattern rule 을 활용하는 예제입니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">OBJS</span> := foo.o bar.o zoo.o
<span class="hljs-constant">OBJS</span> := <span class="hljs-variable">$(OBJS:%=BUILD/%)</span> # BUILD/foo.o BUILD/bar.o ...
<span class="hljs-constant">EXES</span> := <span class="hljs-variable">$(OBJS:%.o=%)</span> # BUILD/foo BUILD/bar ...
all : $(EXES)
$(EXES) : % : %.o
@echo gcc $+ -o $@
.DEFAULT: ;
<span class="hljs-comment">######### 실행 결과 #########</span>
gcc BUILD/foo.o -o BUILD/foo
gcc BUILD/bar.o -o BUILD/bar
gcc BUILD/zoo.o -o BUILD/zoo
</code></pre>
<h3 id="grouping">Grouping</h3>
<p>하나의 디렉토리에 존재하는 파일들을 여러 그룹으로 나누거나
또는 각각 다른 디렉토리에 존재하는 파일들을 그룹으로 구성해 다른 rule 을 적용할 때도
static pattern rule 을 이용하면 간단히 해결할 수 있습니다.</p>
<pre><code class="lang-makefile"><span class="hljs-comment"># lib 하나의 디렉토리에 존재하는 파일들을 두 그룹으로 나누어 각각 다른 rule 을 적용</span>
<span class="hljs-constant">GROUP_A</span> := BUILD/lib/aaa.o BUILD/lib/bbb.o BUILD/lib/ccc.o
<span class="hljs-constant">GROUP_B</span> := BUILD/lib/xxx.o BUILD/lib/yyy.o BUILD/lib/zzz.o
$(GROUP_A) : BUILD/%.o : %.c
command ....
$(GROUP_B) : BUILD/%.o : %.c
command ....
-------------------------------------------------------------------
<span class="hljs-comment"># 각각 다른 디렉토리에 존재하는 파일들을 그룹으로 구성해 다른 rule 을 적용</span>
<span class="hljs-constant">GROUP_A</span> := BUILD/aaa/hello.o BUILD/aaa/bbb/hello.o BUILD/ccc/hello.o
<span class="hljs-constant">GROUP_B</span> := BUILD/xxx/yyy/hello.o BUILD/zzz/hello.o
$(GROUP_A) : BUILD/%.o : %.c
command ....
$(GROUP_B) : BUILD/%.o : %.c
command ....
</code></pre>
<h4 id="explicit-타겟-라인과-같이-사용">explicit 타겟 라인과 같이 사용</h4>
<p>다음과 같이하면 각 실행파일별로 다른 오브젝트, 라이브러리 파일을 지정할 수 있습니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">EXES</span>:= foo bar zoo
$(EXES) : % : %.o
@echo gcc $+ -o $@
bar : aaa.o
zoo : aaa.o libxxx.a bbb.o libyyy.a ccc.o libxxx.a
%.o %.a : ;
<span class="hljs-comment">###### 실행 결과 ######</span>
sh$ make foo
gcc foo.o -o foo
sh$ make bar
gcc bar.o aaa.o -o bar
sh$ make zoo
gcc zoo.o aaa.o libxxx.a bbb.o libyyy.a ccc.o libxxx.a -o zoo
</code></pre>
<h4 id="static-pattern-rule-은-explicit-rule-이다">Static pattern rule 은 explicit rule 이다</h4>
<p>static pattern rule 은 explicit rule 에서 사용되는 targets 에 패턴을 설정하는 형태입니다.
보기에는 pattern rule 처럼 생겼지만 make 실행 시에는 내부적으로 explicit rule 로 변경이 됩니다.
따라서 explicit rule 과 동일하게 targets 이름이 같으면 override 됩니다.
다음을 보면 두 룰의 패턴은 틀리지만 targets 값이 같으므로 동일한 룰로 인식되어
override 되는 것을 볼 수 있습니다.</p>
<pre><code class="lang-makefile">foo.o bar.o : %.o : %.c
@echo 11111
foo.o bar.o : %.o :
@echo 22222
%.c : ;
<span class="hljs-comment">######## 실행 결과 ########</span>
sh$ make
Makefile:6: warning: overriding recipe for target 'foo.o'
Makefile:3: warning: ignoring old recipe for target 'foo.o'
Makefile:6: warning: overriding recipe for target 'bar.o'
Makefile:3: warning: ignoring old recipe for target 'bar.o'
22222
</code></pre>
<p>target-specific 변수를 설정하는것도 explicit rule 과 동일하게 targets 에 설정합니다.</p>
<pre><code class="lang-makefile">foo.o bar.o : %.o : %.c
@echo $@ : $< : $(CFLAGS)
foo.o bar.o : CFLAGS := -g
bar.o : CFLAGS += -Wall
%.c : ;
<span class="hljs-comment">######### 실행 결과 #########</span>
sh$ make
foo.o : foo.c : -g
sh$ make bar.o
bar.o : bar.c : -g -Wall
</code></pre>
<h4 id="pattern-rule-사용시-특정-파일에-대해서만-다른-rule-을-적용">pattern rule 사용시 특정 파일에 대해서만 다른 rule 을 적용</h4>
<p>다음과 같이하면 pattern rule 을 사용할 때 특정 파일에 대해서만 다른 rule 을 적용할 수 있습니다.</p>
<pre><code class="lang-makefile">%.o : %.c <span class="hljs-comment"># 디폴트로 사용되는 룰</span>
@echo gcc -c -DFOO -o $@ $<
foo.o bar.o : %.o : %.c <span class="hljs-comment"># foo.o bar.o 에 대해서만 다른 룰을 적용</span>
@echo gcc -c -DBAR -o $@ $<
%.c : ;
<span class="hljs-comment">######### 실행 결과 #########</span>
sh$ make zoo.o
gcc -c -DFOO -o zoo.o zoo.c
sh$ make foo.o
gcc -c -DBAR -o foo.o foo.c
sh$ make bar.o
gcc -c -DBAR -o bar.o bar.c
</code></pre>
<p><a name="double-colon"></a></p>
<h4 id="doublecolon-rule-을-pattern-에도-적용할-수-있다">double-colon rule 을 pattern 에도 적용할 수 있다</h4>
<p>double-colon rule 을 이용하는 전, 후 처리 기능은 pattern rule 에서는 사용할 수 없는데
( 동일한 패턴 룰은 override 되기 때문에 )
static pattern rule 을 이용하면 가능합니다.</p>
<pre><code class="lang-makefile"><span class="hljs-constant">TARGETS</span> := foo.1 foo.2 ...
$(TARGETS) :: foo.% :
@echo $@ preprocessing ...
$(TARGETS) :: foo.% :
@echo $@ recipe ...
$(TARGETS) :: foo.% :
@echo $@ postprocessing ...
<span class="hljs-comment">######### 실행 결과 #########</span>
sh$ make -j8 foo.1
foo.1 preprocessing ...
foo.1 recipe ...
foo.1 postprocessing ...
sh$ make -j8 foo.2
foo.2 preprocessing ...
foo.2 recipe ...
foo.2 postprocessing ...
</code></pre>
<h4 id="static-shared-라이브러리-동시에-빌드하기">static, shared 라이브러리 동시에 빌드하기</h4>
<p>다음은 static pattern rule 을 사용하여 라이브러리를 빌드하는 예제입니다
( 아래와 같은 경우는 pattern rule 로도 가능합니다 ).
static 라이브러리를 위한 오브젝트 파일은 기존과 동일하게 <code>.o</code> 확장자를 사용하고
shared 라이브러리를 위한 오브젝트 파일은 <code>.lo</code> 확장자를 이용해서 빌드합니다.
static pattern rule 에서 prerequisites 부분은 pattern rule 에서와 같이
<code>%.c | $(BUILD_DIR)/$(SRC_DIR)</code> 부분이 됩니다.
<code>%.c</code> 에 매칭되는 소스파일이 이미 존재하므로 <code>%.o</code> 타겟 파일 생성을 위한
recipe 가 실행되기 전에 먼저 디렉토리 생성을 위한 <code>$(BUILD_DIR)/$(SRC_DIR)</code> 룰이 실행됩니다.</p>
<pre><code class="lang-makefile"><span class="hljs-comment"># 먼저 touch 명령으로 aaa.c bbb.c ccc.c 파일을 생성하고 테스트하세요</span>
<span class="hljs-constant">BUILD_DIR</span> := BUILD
<span class="hljs-constant">SRC_DIR</span> := lib/foo
<span class="hljs-constant">SRCS</span> := aaa.c bbb.c ccc.c
<span class="hljs-constant">OBJS</span> := <span class="hljs-variable">$(addprefix $(BUILD_DIR)</span>/<span class="hljs-variable">$(SRC_DIR)</span>/,<span class="hljs-variable">$(SRCS:.c=.o)</span>)
<span class="hljs-constant">OBJS_SO</span> := <span class="hljs-variable">$(OBJS:.o=.lo)</span>
<span class="hljs-constant">NAME</span> := libfoo
<span class="hljs-constant">STATIC_LIB</span> := <span class="hljs-variable">$(BUILD_DIR)</span>/<span class="hljs-variable">$(SRC_DIR)</span>/<span class="hljs-variable">$(NAME)</span>.a
<span class="hljs-constant">SHARED_LIB</span> := <span class="hljs-variable">$(BUILD_DIR)</span>/<span class="hljs-variable">$(SRC_DIR)</span>/<span class="hljs-variable">$(NAME)</span>.so
<span class="hljs-phony"><span class="hljs-keyword">.PHONY</span>: all $(NAME).a $(NAME).so</span>
all : $(STATIC_LIB) $(SHARED_LIB)
$(NAME).a : $(STATIC_LIB)
$(NAME).so : $(SHARED_LIB)
<span class="hljs-comment">########### static library ###########</span>
$(OBJS) : $(BUILD_DIR)/$(SRC_DIR)/%.o : %.c | $(BUILD_DIR)/$(SRC_DIR)
$(CC) -c $(CFLAGS) -o $@ $<
$(STATIC_LIB) : $(OBJS)
$(RM) $@
$(AR) rcs $@ $^
<span class="hljs-comment">########### shared library ###########</span>
$(OBJS_SO) : $(BUILD_DIR)/$(SRC_DIR)/%.lo : %.c | $(BUILD_DIR)/$(SRC_DIR)
$(CC) -c -fPIC $(CFLAGS) -o $@ $<
$(SHARED_LIB) : $(OBJS_SO)
$(CC) -shared -Wl,-soname,$(NAME).so $^ -o $@
<span class="hljs-comment">########################################</span>
$(BUILD_DIR)/$(SRC_DIR) : ;@mkdir -p $@
clean :
$(RM) $(OBJS) $(OBJS_SO) $(STATIC_LIB) $(SHARED_LIB)
</code></pre>
<h1 id="quiz">Quiz</h1>
<p>Shared library 에서 사용되는 코드는 메모리의 임의의 주소에서 실행될 수 있어야 하기 때문에
컴파일 시에 <code>-fPIC</code> 옵션을 주어서 Position Independent Code 로 만드는데요.
shared library 를 만들 때가 아닌 경우에도 <code>-fPIC</code> 옵션을 사용할 수 있을까요?</p>
<blockquote>
<p>위치 독립적인 코드를 만드는 옵션에는 실행파일을 만들때 사용되는
<code>-fPIE</code> ( Position Independent Executables ) 과
shared library 를 만들때 사용되는 <code>-fPIC</code> ( Position Independent Code ) 가 있습니다.</p>
<ul>
<li><p>먼저 <code>-fPIE</code>, <code>-fPIC</code> 옵션을 사용하지 않고 컴파일한 오브젝트 파일, static library 는
non-PIE 실행파일을 빌드하는 데만 사용할 수 있습니다.
만약에 PIE 실행파일을 빌드하는데 사용할 경우 오류가 발생할 수 있습니다.</p>
</li>
<li><p><code>-fPIE</code> 옵션을 사용해 컴파일한 오브젝트 파일, static library 는
PIE 실행파일을 빌드할 때뿐만 아니라 non-PIE 실행파일을 빌드하는데도 사용될 수 있습니다.
하지만 shared library 를 만드는 데는 사용할 수 없습니다.</p>
</li>
<li><p><code>-fPIC</code> 옵션을 사용해 컴파일한 오브젝트 파일, static library 는
non-PIE, PIE 실행파일 뿐만 아니라 shared library 를 만드는데도 모두 사용할 수 있습니다.</p>
</li>
</ul>
<p>코드가 임의의 주소에서 실행되려면 관련 테이블을 만들어서 indirection 을 해야 하므로
코드 사이즈가 커지고 실행 속도가 느려지는 단점이 있습니다.
하지만 64bit CPU 와 컴파일러 지원에 의해서 지금은 큰 차이는 없다고 합니다.
따라서 요즘에는 컴파일 시에 기본적으로 <code>-fPIC</code> 옵션을 사용하는 경우도 있습니다.
그리고 대부분의 리눅스가 보안과 관련해서 <code>-fPIE</code> 옵션을 사용하고 있어서
실행파일이 매번 다른 주소에서 실행이 됩니다.</p>
</blockquote>
</section>
</div>
</div>
</div>
<a href="./pattern_rules.html" class="navigation navigation-prev " aria-label="Previous page: Pattern Rules"><i class="fa fa-angle-left"></i></a>
<a href="./recipes.html" class="navigation navigation-next " aria-label="Next page: Recipes"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="gitbook/app.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
<script src="gitbook/plugins/gitbook-plugin-search/search.js"></script>
<script src="gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>