½sĶ¾¹¡]»P°£¿ù¾¹¡^ªº¨Ï¥Î
¬°¤°»ò»Ýn compiler¡]½sĶ¾¹¡^¡H
µ{¦¡¨Ì¦b¹q¸£¤¤¹B¦æªºª½±µ©Î¶¡±µ¤§§O¡A¤Àª½Ä¶»y¨¥»P½sĶ»y¨¥¡C
¹³ BASIC ´N¬Oª½Ä¶¾¹¡C
½sĶ¾¹ªº¨Ï¥Î¤W·|¹J¨ìªº¬ÛÃö³N»y
ì©l½X¡B·½½X¡]source code¡^
½sĶ¡]compile¡^
ª«¥óÀÉ¡]object file¡^
Áåµ²¡]link¡^
µ{¦¡®w¡]library¡^
¥i°õ¦æÀÉ¡]executable¡^
¦b UNIX ¤¤¨Ï¥Î Fortran Compiler ¤§¤èªk
Note : the following example use "f77" as name offortran compiler, depends on different compilers in different systems, the name sof fortran compiler can be
f77 or g77
ifort or ifc
pgf77 or pgf90
gfortran
fort77
xlfY¬O¨t²Î¤½¥Î¥Ø¿ý¤¤¨S¦³ gfortran¡A·|¬Ý¨ì°T®§ bash: gfortran: command not found ©Î gfortran: Command not found. ±q¿Ã¹õ¿é¥X¡C
±ýª¾¹D¨t²Î¤½¦@¥Ø¿ý¤¤¤¤¦³¨S¦³¬YÓ¯S©w«ü¥O¡A¤ñ¤è»¡ gfortran¡A¥i¦b shell ´£¥Ü²Å¸¹¤U¥´ which gfortran <Enter> ©Î whereis gfortran <Enter> ¡A¦b¤@¯ë Linux ª©¥»¤U¡Ag77 ¤Î gfortran ±`¦³¦w¸Ë¡C
½Ðª`·N¡A§Aªº fortran µ{¦¡Àɮפ@©wn¥s¥E *.f ³oºØ¦UºÙ¡A¤£¯à¨S¦³©µ¦ùÀɦW©Î¬O¥s *.txt ³o¼Ë¡C
§Ö³t½d¨Ò
¦³¤@Óµ{¦¡ÀÉ hello.f¡A¨ä¤º®e¥u¦³¤T¦æ
program hello
write(*,*) 'Hello, World.'
end¥H¤U¦C«ü¥O½sĶ
gfortran hello.f <Enter>
«h±o¨ì a.out ¥i°õ¦æÀÉ¡A¤U
./a.out <Enter>
«hµ{¦¡·|¦b¿Ã¹õ¤W¼g¥X
Hello, World.
¼Ð·Ç¾Þ§@½d¨Ò¡G
gfortran my_program_file.f
¡]²£¥Í¥X¨Óªº°õ¦æÀÉ·|³Q¦Û°Ê©R¦W§@ a.out¡AY¤w¦s¦b a.out «hªº·|³Q»\±¼¡A¦P®É¡A¤]·|²£¥Í my_program_file.o ¡^gfortran my_program_file1.f my_program_file2.f
¡]¨âÓµ{¦¡½XÀɮפ@°_½s¡A·|²£¥Í¨âÓª«¥óÀÉ¡A¨ÃÃìµ²¥X¤@Ó°õ¦æÀÉ a.out¡^gfortran *.f
¡]§â¥Ø¿ý¤¤©Ò¦³ *.f µ{¦¡½XÀɮ׳£§@½sĶ¡A·|²£¥Í¦hÓª«¥óÀÉ¡A³Ì«á¨ÃÃìµ²¥X¤@Ó°õ¦æÀÉ a.out¡^gfortran -o my_executable.x my_program_file.f
¡]²£¥Í¥X¨Óªº°õ¦æÀÉ·|³Q«ü©w©R¦W§@ my_executable.x¡^gfortran -o my_executable.x -lMyLib my_program_file.f
¡]¦P¤W¡A¨Ã¥B«ü©wÃB¥~ªºµ{¦¡®w libMyLib.a ¨Ñ§@Ãìµ²²£¥Í¥i°õ¦æÀɤ§¥Î¡^gfortran -o my_executable.x -L/My/Lib/Dir -lMyLib my_program_file.f
¡]¦P¤W¡A¨Ã¥B«ü©w·j´MÃB¥~µ{¦¡®w©Ò¦b¤§¥Ø¿ý¬° /My/Lib/Dir¡^¨ä¥L¦³¥Î½d¨Ò¡G
gfortran -c my_program_file.f
¡]¥u°µ½sĶ¨Ó²£¥Íª«¥óÀÉ¡B¤£§@Áåµ²¦]¦¹¤£²£¥Í¥i°õ¦æÀÉ¡^gfortran -g my_program_file.f
¡]±Ò°Ê¥i°õ¦æÀɤº«Ø°£¿ù¥\¯à¡A°t¦X gdb¡A¥i¶i¦æµ{¦¡¹B¦æ¨BÆJ¤Wªº³v¨B°£¿ù¡^
«÷²Õ library ¤§¤èªk
¦pªG§A¦³«Ü¦h°Æµ{¦¡¡A¦Û¤v·Q§â¥¦Ì¶°¤¤¦b¤@°_¦Ó§Î¦¨¤@¥]¨ç¦¡®w¡A«h¥i¥H¨Ï¥Î ar¡]archive : «ØÀÉ¡^«ü¥O¡A½Ðª`·N©Ò¦³ UNIX ¤Uªº¨ç¦¡®w³£n©R¦W¬° lib####.a ©Î lib####.so ªº¼Ë¦¡¡A¦p¤U¡G
ar rv libMyLib.a my_object.o
¡]§â my_object.o ¥[¤J¨ì libMyLib.a ¤§¤¤¡AlibMyLib.a Y쥻¤£¦s¦b«h²£¥Í¡^ar t libMyLib.a
¡]¦C¥X³oÓµ{¦¡®w¤¤©Ò¥]§t¤§©Ò¦³°Æµ{¦¡¦WºÙ²M³æ¡^
°£¿ùªº¤èªk
°£¿ù¤@¯ë¥i¨Ï¥Î´¡¤J¤¤Â_ÂI¡]¤ñ¤è»¡¦L¥X¤¤¶¡µ²ªG¡^ªºµ¦²¤¡A©Î¬O¨Ï¥Î¸û±Mªùªº gdb °£¿ù¤u¨ãµ{¦¡¡C
gdb ªº¾÷ªì¶¥¨Ï¥Î¤èªk¥Ü½d¡G
º¥ý¬On¥H -g ¨Ó½sĶµ{¦¡¡A¦p f77 -g -o my_prog.x my_prog.f ¡A¦p¦¹©Ò²£¥Í¥X¨Óªº¥i°õ¦æÀÉ my_prog.x
gdb my_prog.x <Enter> ´N·|±Ò°Ê¦b gdb ªººÊ±±¤U°õ¦æ my_prog.x ªº¾÷¨î¡A¤@¶}©l§A·|¬Ý¨ì¦³ gdb> ´£¥ÜªºÀô¹Ò¬É±¡A¦b¦¹®Éµ{¦¡ my_prog.x ¥u¬O³Q gdb ¸ü¤J¦Ó©|¥¼¶}©l°õ¦æ¡An¥´ run ¦p¤U
gdb> run
¤§«á¥i°õ¦æÀÉ·|¶}©l¦p±`¹B¦æ¡A¥]§t©Ò¦³ I/O¡A¤§«á·|¾ÉP¨âºØµ²ªG¡A¨ä¤@¬Oµ{¦¡¥¿±`µ²§ô¡A¨ä¤G¬Oµ{¦¡ crash¡]°õ¦æ¥¢±Ñ¡^¡A«áªÌ¦b gdb ªººÊ±±¤U¤¹³\¨Ï¥ÎªÌ¬Ý¨ì§ó¦hªº¸ê®Æ¡C
¦pªG§A¼gªºµ{¦¡¬Onµ¹°Ñ¼Æªº¡A¤ñ¤è»¡¥¦¤@¯ë¦b¨Ï¥Îªº®ÉÔ¬O¥´¦¨ ./my_prog.x 0.2 0.4 <Enter> ¡A«h¦b¥Î gdb my_prog.x ¸ü¤J¤§«á¡An¥Î gdb> run 0.2 0.4 ³o¼Ëªº¤è¦¡¨Ó¶]¥¦¡C
gdb> where
¤@Ó¦b gdb ¤U crash ªºµ{¦¡¡A §ÚÌ¥i¥HÂǥѦb gdb ¤U¥´ where ¿à¨Ó¦C¥X¬O·í¦bµ{¦¡ªº¨º¤@¦æ¡C³o¹ï©ó°£¿ù¬O«D±`¦³¥Îªº¡C
gdb ªº¶i¶¥¥\¯à
gdb > break #
¦b¥H gdb ¸ü¤J ¥i°õ¦æÀÉ«á¦Ó¥¼¨Ï¥Î run ¤§«e¡]ª½±µ run ¥Nªí¤@¶]¨ì©³¡^¡A¥i¥H¥Î break # ¨Ó¹w³]¤¤Â_ÂI ¡A¨ä¤¤ # ¬Oì©l½Xªº¦æ¸¹¦æ¸¹¡C³]¦n¤¤Â_ÂI¦A¤U run ¤§«á¡Agdb ·|¥u°õ¦æ§¹ #-1 ¦æ¦Ó°±¤U¨Ó¡A§A«h¦³¾÷·|¥h¼fµø¸Ó¶¥¬q®Éªº¦UÓÅܼÆÈ¡C
break «á±¤]¥i¥Hµ¹°Æµ{¦¡¦W¡BÀɦW¥[¦æ¸¹¡B°O¾ÐÅé¦ì§}¡A¥Îªk½Ð¨£¬ÛÃöºô¶±Ð§÷¸ê®Æ¡C
gdb > step
¦pªG gdb ¬O§âµ{¦¡°±¦b¤¤¶¡¡A«h¤U step ·|¦V¤U°õ¦æ¤@¦æ¡C¨Ã¥B¦L¥X¸Ó¦æªºµ{¦¡½X¤º®e¨Ó´£¿ô§A¡C
gdb > backtrace
¥´ backtrace ·|¦L¥X¨ì¥Ø«e¶i¤@¨B¬°¤î¥D°Æµ{¦¡ªº©I¥s¼h¦¸¬[ºc¡A§A¥i¥H¬Ý¨ì¥Ø«e¬O¦b²Ä´X¦æ¡B¥¦¬O¦b¨º¤@ӰƵ{¦¡¤º¡B¥¦ªº¤W¼hì©I¥sµ{¦¡¤S¬O¦b¨º¤@¦æ©I¥s¥¦ªº¡C¥¦·|¥H¥D°Æµ{¦¡©I¥s¤@¸ô¤U¨Ó¦³´X¼h¡]¦b gdb ¥s°µ frame¡^§â¥¦ªí²{¥X¨Ó¡A³o¹ï©ó°lÂܤj¦Ó½ÆÂøªºµ{¦¡¬O«Ü¦³¥Îªº¡C
gdb > frame
¥u¥´ frame ·|¦C¥X²{¦bªº¼h¡]¶V±µªñ¥Ø«e¤¤Â_ÂIªº¼Æ¦r¶V¤p¡^¡A«á±¦³¥[¼Æ¦rªº¸Ü´N·|¸õ¨ì¨º¤@¼h¡A¤£¦P¼h©Ò·|±µÄ²¨ìÅܼƼÆȵ²ªG¬O¤£¤@¼Ëªº¡C¡]¤j®a¦^·Q¤@¤U¡A¦³¥i¯à¦b¥Dµ{¦¡ùØ»P°Æµ{¦¡ùس£¦³¦P¤@Ó¦WºÙªºÅܼơA¦ý¨ä¼ÆȬO¤£¦Pªº¡A¦]¦¹n¥ý½T©w²{¦b¬O¤Á´«¨ì¤F¦b¨º¤@¼hªº°Æµ{¦¡¤¤¡A¤~¯à¶i¤@¨B±´¯ÁÅܼƼÆȤWªºÅܤơC
gdb > print var_name
¦L¥X¬YÓÅÜ¼Æ var_name ¦b·í®É¶¥¬q©Ò°õ¦æ¨ìªºµ²ªG¡A³o·íµM¬O«Ü¦³¥Îªº¥\¯à¡C
gdb > list
¥i¥H¨C¦¸¤Q¦æ¦C¥Xì©l½X¡A¦A¤U¤@¦¸«h¦V¤U¦A¦C¤Q¦æ¡AY«á±¦A±a¦æ¸¹«h¬O¥H¸Ó¦æ¬°¤¤¤ß¦L¥X¤Q¦æì©l½X¡C
§ó¦hÃö©ó gdb ªº¥Îªk¡G
½Ð¨£ man gdb
ºô¸ô¤W¦³¤H¼gªº±Ð§÷¡G
http://www.dirac.org/linux/gdb/¡]±ÀÂË¡^
gdb ªº¹Ï§Î¤¶± ddd
make «ü¥O»P Makefile Àɪº¨Ï¥Î
·Q¹³§A¦³¤@Ó¤jµ{¦¡¡A¥Ñ«Ü¦hÓµ{¦¡Àɮ׺c¦¨¡A¥þ³¡½sĶ«á¦@¦PÃìµ²¦b¤@°_¤~¦¨¬°³æ¤@ªº¥i°õ¦æÀÉ¡A«h¼g¤@Ó´¶³qªº script ¡]¨ä¤¤¥]§t¦h¦æ©R¥O¡^¨Ó§âÓ§O½sĶªº°Ê§@³v¤@°õ¦æ¨Ã§@³Ì«áÃìµ²¡A¦ü¥G¬O«D±`«ê·íªº¡CµM¦Ó¦p¦¹°µªº¸Ü¡AÁöµM²Ä¤@¦¸°µ¥X executable ®É¬O¥þ³¡ªºµ{¦¡Àɮ׳£»Ýn½sĶ¤@¦¸¡A¦ý¤§«á¦pªG¬O§ó°Ê¨ä¤¤¤@ÓÀÉ®×ùتº¤º®e¡A«h¨ä¥L¨S§ó°Ê¨ìªºÀɮפ]nªá®É¶¡«°µ¤@¦¸¡C³o¦ü¥G¤S¤Ó¨S®Ä²v¤F¡C©Î³\§ÚÌ·|·Q¡A§ÚÌ¥un¥h¬Ý *.f ªº®É¶¡¦³¨S¦³¤ñ *.o ªº®É¶¡·s¡A¦pªG¦³¡A«hªí¥Ü¶Z¤W¦¸²£¥Í *.o ÀÉ«áì©l½X *.f ¤v¸g¦³§ó°Ê¹L¤F¡A§ÚÌ¥un°w¹ï¦b¤§«e¦³³Q§ï¹LªºÀɮ׫½s´N¦n¡C
¦pªG¦³¤@ºØ script¡A¯àÅý§ÚÌ©w¸q¦U°Æµ{¦¡Àɮפ§¶¡ªº "¨Ì¦sÃö«Y"¡AµM«á¦³¯S®íªº½sĶ«ü¥O¡A¯à®Ú¾Ú³oÓ script ¤º©Ò°O¸üªº "¨Ì¦sÃö«Y"¡A¥ý«á¦³§Ç¦a½s¥X°õ¦æÀÉ¡A¤£¶È¦p¦¹¡A¨C·ín«·s³y¥X°õ¦æÀɮɡAÁÙ¯à«ö·Ó³o¨Ç¨Ì¦sÃö«Y¥¿½T¦a§â¸Ó«½sªº³¡¤À°Æµ{¦¡«·s½s¦n¡A¤£¦h¤]¤£¤Ö¡C
Mikefile (makefile ¥þ¤p¼g¤]¥i¥H) ³o¬O³o¼Ëªº script¡A¦Ó UNIX ¤Uªº make «ü¥O´N¬O³o¼Ëªº¯S®í«ü¥O¡C
ºô¸ô¤Wªº Makefile ¼¶¼g±Ð¾Ç http://www.study-area.org/cyril/opentools/opentools/makefile.html
automake ¤u¨ã
automake ±Ð¾Ç
http://netlab.cse.yzu.edu.tw/~armor/columns/automake/automake.htm