Ç°ÑÔ:
ÕâƪÎÄÕ½éÉÜÔÚLINUXϽøÐÐCÓïÑÔ±à³ÌËùÐèÒªµÄ»ù´¡ÖªÊ¶.ÔÚÕâƪÎÄÕµ±ÖÐ,ÎÒÃǽ«»áѧµ½ÒÔÏÂÄÚÈÝ:
Ô´³ÌÐò±àÒë
MakefileµÄ±àд
³ÌÐò¿âµÄÁ´½Ó
³ÌÐòµÄµ÷ÊÔ
Í·ÎļþºÍϵͳÇóÖú
--------------------------------------------------------------------------------
1.Ô´³ÌÐòµÄ±àÒë
ÔÚLinuxÏÂÃæ,Èç¹ûÒª±àÒëÒ»¸öCÓïÑÔÔ´³ÌÐò,ÎÒÃÇҪʹÓÃGNUµÄgcc±àÒëÆ÷. ÏÂÃæÎÒÃÇÒÔÒ»¸öʵÀýÀ´ËµÃ÷ÈçºÎʹÓÃgcc±àÒëÆ÷.
¼ÙÉèÎÒÃÇÓÐÏÂÃæÒ»¸ö·Ç³£¼òµ¥µÄÔ´³ÌÐò(hello.c):
int main(int argc,char **argv)
{
printf("Hello Linuxn");
}
Òª±àÒëÕâ¸ö³ÌÐò,ÎÒÃÇÖ»ÒªÔÚÃüÁîÐÐÏÂÖ´ÐÐ:
gcc -o hello hello.c
gcc ±àÒëÆ÷¾Í»áΪÎÒÃÇÉú³ÉÒ»¸öhelloµÄ¿ÉÖ´ÐÐÎļþ.Ö´ÐÐ./hello¾Í¿ÉÒÔ¿´µ½³ÌÐòµÄÊä³ö½á¹ûÁË.ÃüÁîÐÐÖÐ gcc±íʾÎÒÃÇÊÇÓÃgccÀ´±àÒëÎÒÃǵÄÔ´³ÌÐò,-o Ñ¡Ïî±íʾÎÒÃÇÒªÇó±àÒëÆ÷¸øÎÒÃÇÊä³öµÄ¿ÉÖ´ÐÐÎļþÃûΪhello ¶øhello.cÊÇÎÒÃǵÄÔ´³ÌÐòÎļþ.
gcc±àÒëÆ÷ÓÐÐí¶àÑ¡Ïî,Ò»°ãÀ´ËµÎÒÃÇÖ»ÒªÖªµÀÆäÖеļ¸¸ö¾Í¹»ÁË. -oÑ¡ÏîÎÒÃÇÒѾ֪µÀÁË,±íʾÎÒÃÇÒªÇóÊä³öµÄ¿ÉÖ´ÐÐÎļþÃû. -cÑ¡Ïî±íʾÎÒÃÇÖ»ÒªÇó±àÒëÆ÷Êä³öÄ¿±ê´úÂë,¶ø²»±ØÒªÊä³ö¿ÉÖ´ÐÐÎļþ. -gÑ¡Ïî±íʾÎÒÃÇÒªÇó±àÒëÆ÷ÔÚ±àÒëµÄʱºòÌṩÎÒÃÇÒÔºó¶Ô³ÌÐò½øÐе÷ÊÔµÄÐÅÏ¢.
ÖªµÀÁËÕâÈý¸öÑ¡Ïî,ÎÒÃǾͿÉÒÔ±àÒëÎÒÃÇ×Ô¼ºËùдµÄ¼òµ¥µÄÔ´³ÌÐòÁË,Èç¹ûÄãÏëÒªÖªµÀ¸ü¶àµÄÑ¡Ïî,¿ÉÒԲ鿴gccµÄ°ïÖúÎĵµ,ÄÇÀïÓÐ×ÅÐí¶à¶ÔÆäËüÑ¡ÏîµÄÏêϸ˵Ã÷.
2.MakefileµÄ±àд
¼ÙÉèÎÒÃÇÓÐÏÂÃæÕâÑùµÄÒ»¸ö³ÌÐò,Ô´´úÂëÈçÏÂ:
/* main.c */
#include "mytool1.h"
#include "mytool2.h"
int main(int argc,char **argv)
{
mytool1_print("hello");
mytool2_print("hello");
}
/* mytool1.h */
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char *print_str);
#endif
/* mytool1.c */
#include "mytool1.h"
void mytool1_print(char *print_str)
{
printf("This is mytool1 print %sn",print_str);
}
/* mytool2.h */
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char *print_str);
#endif
/* mytool2.c */
#include "mytool2.h"
void mytool2_print(char *print_str)
{
printf("This is mytool2 print %sn",print_str);
}
µ±È»ÓÉÓÚÕâ¸ö³ÌÐòÊǺ̵ܶÄÎÒÃÇ¿ÉÒÔÕâÑùÀ´±àÒë
gcc -c main.c
gcc -c mytool1.c
gcc -c mytool2.c
gcc -o main main.o mytool1.o mytool2.o
ÕâÑùµÄ»°ÎÒÃÇÒ²¿ÉÒÔ²úÉúmain³ÌÐò,¶øÇÒÒ²²»Ê±ºÜÂé·³.µ«ÊÇÈç¹ûÎÒÃÇ¿¼ÂÇÒ»ÏÂÈç¹ûÓÐÒ»ÌìÎÒÃÇÐÞ¸ÄÁËÆäÖеÄÒ»¸öÎļþ(±ÈÈç˵mytool1.c)ÄÇôÎÒÃÇÄѵÀ»¹ÒªÖØÐÂÊäÈëÉÏÃæµÄÃüÁî?Ò²ÐíÄã»á˵,Õâ¸öºÜÈÝÒ×½â¾ö°¡,ÎÒдһ¸öSHELL½Å±¾,ÈÃËý°ïÎÒÈ¥Íê³É²»¾Í¿ÉÒÔÁË.ÊǵĶÔÓÚÕâ¸ö³ÌÐòÀ´Ëµ,ÊÇ¿ÉÒÔÆðµ½×÷ÓõÄ.µ«Êǵ±ÎÒÃÇ°ÑÊÂÇéÏëµÄ¸ü¸´ÔÓÒ»µã,Èç¹ûÎÒÃǵijÌÐòÓм¸°Ù¸öÔ´³ÌÐòµÄʱºò,ÄѵÀÒ²Òª±àÒëÆ÷ÖØÐÂÒ»¸öÒ»¸öµÄÈ¥±àÒë?
Ϊ´Ë,´ÏÃ÷µÄ³ÌÐòÔ±ÃÇÏë³öÁËÒ»¸öºÜºÃµÄ¹¤¾ßÀ´×öÕâ¼þÊÂÇé,Õâ¾ÍÊÇmake.ÎÒÃÇÖ»ÒªÖ´ÐÐÒÔÏÂmake,¾Í¿ÉÒÔ°ÑÉÏÃæµÄÎÊÌâ½â¾öµô.ÔÚÎÒÃÇÖ´ÐÐmake֮ǰ,ÎÒÃÇÒªÏȱàдһ¸ö·Ç³£ÖØÒªµÄÎļþ.--Makefile.¶ÔÓÚÉÏÃæµÄÄǸö³ÌÐòÀ´Ëµ,¿ÉÄܵÄÒ»¸öMakefileµÄÎļþÊÇ:
# ÕâÊÇÉÏÃæÄǸö³ÌÐòµÄMakefileÎļþ
main:main.o mytool1.o mytool2.o
gcc -o main main.o mytool1.o mytool2.o
main.o:main.c mytool1.h mytool2.h
gcc -c main.c
mytool1.o:mytool1.c mytool1.h
gcc -c mytool1.c
mytool2.o:mytool2.c mytool2.h
gcc -c mytool2.c
ÓÐÁËÕâ¸öMakefileÎļþ,²»¹ýÎÒÃÇʲôʱºòÐÞ¸ÄÁËÔ´³ÌÐòµ±ÖеÄʲôÎļþ,ÎÒÃÇÖ»ÒªÖ´ÐÐmakeÃüÁî,ÎÒÃǵıàÒëÆ÷¶¼Ö»»áÈ¥±àÒëºÍÎÒÃÇÐ޸ĵÄÎļþÓйصÄÎļþ,ÆäËüµÄÎļþËýÁ¬Àí¶¼²»ÏëÈ¥ÀíµÄ.
ÏÂÃæÎÒÃÇѧϰMakefileÊÇÈçºÎ±àдµÄ.
ÔÚMakefileÖÐÒ²#¿ªÊ¼µÄÐж¼ÊÇ×¢ÊÍÐÐ.MakefileÖÐ×îÖØÒªµÄÊÇÃèÊöÎļþµÄÒÀÀµ¹ØϵµÄ˵Ã÷.Ò»°ãµÄ¸ñʽÊÇ:
target: components
TAB rule
µÚÒ»ÐбíʾµÄÊÇÒÀÀµ¹Øϵ.µÚ¶þÐÐÊǹæÔò.
±ÈÈç˵ÎÒÃÇÉÏÃæµÄÄǸöMakefileÎļþµÄµÚ¶þÐÐ
main:main.o mytool1.o mytool2.o
±íʾÎÒÃǵÄÄ¿±ê(target)mainµÄÒÀÀµ¶ÔÏó(components)ÊÇmain.o mytool1.o mytool2.o µ±ÒÐÀµµÄ¶ÔÏóÔÚÄ¿±êÐ޸ĺóÐ޸ĵĻ°,¾ÍҪȥִÐйæÔòÒ»ÐÐËùÖ¸¶¨µÄÃüÁî.¾ÍÏóÎÒÃǵÄÉÏÃæÄǸöMakefileµÚÈýÐÐËù˵µÄÒ»ÑùÒªÖ´ÐÐ gcc -o main main.o mytool1.o mytool2.o ×¢Òâ¹æÔòÒ»ÐÐÖеÄTAB±íʾÄÇÀïÊÇÒ»¸öTAB¼ü
MakefileÓÐÈý¸ö·Ç³£ÓÐÓõıäÁ¿.·Ö±ðÊÇ$@,$^,$
int main(int argc,char **argv)
{
double value;
printf("Value:%fn",value);
}
Õâ¸ö³ÌÐòÏ൱¼òµ¥,µ«Êǵ±ÎÒÃÇÓà gcc -o temp temp.c ±àÒëʱ»á³öÏÖÏÂÃæËùʾµÄ´íÎó.
/tmp/cc33Kydu.o: In function `main':
/tmp/cc33Kydu.o(.text+0xe): undefined reference to `log'
collect2: ld returned 1 exit status
³öÏÖÕâ¸ö´íÎóÊÇÒòΪ±àÒëÆ÷ÕÒ²»µ½logµÄ¾ßÌåʵÏÖ.ËäÈ»ÎÒÃÇ°üÀ¨ÁËÕýÈ·µÄÍ·Îļþ,µ«ÊÇÎÒÃÇÔÚ±àÒëµÄʱºò»¹ÊÇÒªÁ¬½ÓÈ·¶¨µÄ¿â.ÔÚLinuxÏÂ,ΪÁËʹÓÃÊýѧº¯Êý,ÎÒÃDZØÐëºÍÊýѧ¿âÁ¬½Ó,Ϊ´ËÎÒÃÇÒª¼ÓÈë -lm Ñ¡Ïî. gcc -o temp temp.c -lmÕâÑù²ÅÄܹ»ÕýÈ·µÄ±àÒë.Ò²ÐíÓÐÈËÒªÎÊ,Ç°ÃæÎÒÃÇÓÃprintfº¯ÊýµÄʱºòÔõôûÓÐÁ¬½Ó¿âÄØ?ÊÇÕâÑùµÄ,¶ÔÓÚһЩ³£Óõĺ¯ÊýµÄʵÏÖ,gcc±àÒëÆ÷»á×Ô¶¯È¥Á¬½ÓһЩ³£Óÿâ,ÕâÑùÎÒÃǾÍûÓбØÒª×Ô¼ºÈ¥Ö¸¶¨ÁË. ÓÐʱºòÎÒÃÇÔÚ±àÒë³ÌÐòµÄʱºò»¹ÒªÖ¸¶¨¿âµÄ·¾¶,Õâ¸öʱºòÎÒÃÇÒªÓõ½±àÒëÆ÷µÄ -LÑ¡ÏîÖ¸¶¨Â·¾¶.±ÈÈç˵ÎÒÃÇÓÐÒ»¸ö¿âÔÚ /home/hoyt/mylibÏÂ,ÕâÑùÎÒÃDZàÒëµÄʱºò»¹Òª¼ÓÉÏ -L/home/hoyt/mylib.¶ÔÓÚһЩ±ê×¼¿âÀ´Ëµ,ÎÒÃÇûÓбØÒªÖ¸³ö·¾¶.Ö»ÒªËüÃÇÔÚÆðȱʡ¿âµÄ·¾¶Ï¾ͿÉÒÔÁË.ϵͳµÄȱʡ¿âµÄ·¾¶/lib /usr/lib /usr/local/lib ÔÚÕâÈý¸ö·¾¶ÏÂÃæµÄ¿â,ÎÒÃÇ¿ÉÒÔ²»Ö¸¶¨Â·¾¶.
»¹ÓÐÒ»¸öÎÊÌâ,ÓÐʱºòÎÒÃÇʹÓÃÁËij¸öº¯Êý,µ«ÊÇÎÒÃDz»ÖªµÀ¿âµÄÃû×Ö,Õâ¸öʱºòÔõô°ìÄØ?ºÜ±§Ç¸,¶ÔÓÚÕâ¸öÎÊÌâÎÒÒ²²»ÖªµÀ´ð°¸,ÎÒÖ»ÓÐÒ»¸öɵ°ì·¨.Ê×ÏÈ,ÎÒµ½±ê×¼¿â·¾¶ÏÂÃæÈ¥ÕÒ¿´¿´ÓÐûÓкÍÎÒÓõĺ¯ÊýÏà¹ØµÄ¿â,ÎÒ¾ÍÕâÑùÕÒµ½ÁËÏß³Ì(thread)º¯ÊýµÄ¿âÎļþ(libpthread.a). µ±È»,Èç¹ûÕÒ²»µ½,Ö»ÓÐÒ»¸ö±¿·½·¨.±ÈÈçÎÒÒªÕÒsinÕâ¸öº¯ÊýËùÔڵĿâ. ¾ÍÖ»ºÃÓà nm -o /lib/*.so|grep sin>~/sin ÃüÁî,È»ºó¿´~/sinÎļþ,µ½ÄÇÀïÃæÈ¥ÕÒÁË. ÔÚsinÎļþµ±ÖÐ,ÎÒ»áÕÒµ½ÕâÑùµÄÒ»ÐÐlibm-2.1.2.so:00009fa0 W sin ÕâÑùÎÒ¾ÍÖªµÀÁËsinÔÚ libm-2.1.2.so¿âÀïÃæ,ÎÒÓà -lmÑ¡Ïî¾Í¿ÉÒÔÁË(È¥µôÇ°ÃæµÄlibºÍºóÃæµÄ°æ±¾±êÖ¾,¾ÍÊ£ÏÂmÁËËùÒÔÊÇ -lm). Èç¹ûÄãÖªµÀÔõôÕÒ,Çë¸Ï¿ì¸æËßÎÒ,Îһطdz£¸Ð¼¤µÄ.лл!
4.³ÌÐòµÄµ÷ÊÔ
ÎÒÃDZàдµÄ³ÌÐò²»Ì«¿ÉÄÜÒ»´ÎÐԾͻá³É¹¦µÄ,ÔÚÎÒÃǵijÌÐòµ±ÖÐ,»á³öÏÖÐíÐí¶à¶àÎÒÃÇÏë²»µ½µÄ´íÎó,Õâ¸öʱºòÎÒÃǾÍÒª¶ÔÎÒÃǵijÌÐò½øÐе÷ÊÔÁË.
×î³£Óõĵ÷ÊÔÈí¼þÊÇgdb.Èç¹ûÄãÏëÔÚͼÐνçÃæϵ÷ÊÔ³ÌÐò,ÄÇôÄãÏÖÔÚ¿ÉÒÔÑ¡Ôñxxgdb.¼ÇµÃÒªÔÚ±àÒëµÄʱºò¼ÓÈë -gÑ¡Ïî.¹ØÓÚgdbµÄʹÓÿÉÒÔ¿´gdbµÄ°ïÖúÎļþ.ÓÉÓÚÎÒûÓÐÓùýÕâ¸öÈí¼þ,ËùÒÔÎÒÒ²²»Äܹ»Ëµ³öÈçºÎʹÓÃ. ²»¹ýÎÒ²»Ï²»¶ÓÃgdb.¸ú×ÙÒ»¸ö³ÌÐòÊǺܷ³µÄÊÂÇé,ÎÒÒ»°ãÓÃÔÚ³ÌÐòµ±ÖÐÊä³öÖмä±äÁ¿µÄÖµÀ´µ÷ÊÔ³ÌÐòµÄ.µ±È»Äã¿ÉÒÔÑ¡Ôñ×Ô¼ºµÄ°ì·¨,ûÓбØҪȥѧ±ðÈ˵Ä.ÏÖÔÚÓÐÁËÐí¶àIDE»·¾³,ÀïÃæÒѾ×Ô¼º´øÁ˵÷ÊÔÆ÷ÁË.Äã¿ÉÒÔÑ¡Ôñ¼¸¸öÊÔÒ»ÊÔÕÒ³ö×Ô¼ºÏ²»¶µÄÒ»¸öÓÃ.
5.Í·ÎļþºÍϵͳÇóÖú
ÓÐʱºòÎÒÃÇÖ»ÖªµÀÒ»¸öº¯ÊýµÄ´ó¸ÅÐÎʽ,²»¼ÇµÃÈ·Çеıí´ïʽ,»òÕßÊDz»¼ÇµÃן¯ÊýÔÚÄǸöÍ·Îļþ½øÐÐÁË˵Ã÷.Õâ¸öʱºòÎÒÃÇ¿ÉÒÔÇóÖúϵͳ.
±ÈÈç˵ÎÒÃÇÏëÖªµÀfreadÕâ¸öº¯ÊýµÄÈ·ÇÐÐÎʽ,ÎÒÃÇÖ»ÒªÖ´ÐÐ man fread ϵͳ¾Í»áÊä³öן¯ÊýµÄÏêϸ½âÊ͵Ä.ºÍÕâ¸öº¯ÊýËùÔÚµÄÍ·Îļþ˵Ã÷ÁË. Èç¹ûÎÒÃÇÒªwriteÕâ¸öº¯ÊýµÄ˵Ã÷,µ±ÎÒÃÇÖ´ÐÐman writeʱ,Êä³öµÄ½á¹ûÈ´²»ÊÇÎÒÃÇËùÐèÒªµÄ. ÒòΪÎÒÃÇÒªµÄÊÇwriteÕâ¸öº¯ÊýµÄ˵Ã÷,¿ÉÊdzöÀ´µÄÈ´ÊÇwriteÕâ¸öÃüÁîµÄ˵Ã÷.ΪÁ˵õ½writeµÄº¯Êý˵Ã÷ÎÒÃÇÒªÓà man 2 write. 2±íʾÎÒÃÇÓõÄwriteÕâ¸öº¯ÊýÊÇϵͳµ÷Óú¯Êý,»¹ÓÐÒ»¸öÎÒÃdz£ÓõÄÊÇ3±íʾº¯ÊýÊÇCµÄ¿âº¯Êý.
¼Çס²»¹Üʲôʱºò,man¶¼ÊÇÎÒÃǵÄ×îºÃÖúÊÖ.
--------------------------------------------------------------------------------
ºÃÁË,ÕâÒ»Õ¾ͽ²Õâô¶àÁË,ÓÐÁËÕâЩ֪ʶÎÒÃǾͿÉÒÔ½øÈ뼤¶¯ÈËÐĵÄLinuxϵÄC³ÌÐò̽Ïջ.
²»»ýõͲ½,ÎÞÒÔÖÁǧÀï!
geekf ÓÚ 2015-11-17 15:54:37·¢±í:
лл·ÖÏí£¡
honckly ÓÚ 2007-01-18 11:03:04·¢±í:
·Ç³£µÄ°ô£¡£¡£¡
60.176.46.* ÓÚ 2006-12-20 20:35:44·¢±í:
Ö§³Ö£¡£¡£¡
sx_r ÓÚ 2006-08-01 18:21:23·¢±í:
hao
xu9989 ÓÚ 2006-08-01 10:52:56·¢±í:
ÐÁ¿àÁË
MRR2 ÓÚ 2006-07-31 09:52:03·¢±í:
Ö§³Ö
Redassociation ÓÚ 2006-07-27 21:42:51·¢±í:
good
McJared ÓÚ 2006-03-18 10:22:08·¢±í:
very good
MOSSC ÓÚ 2005-12-14 09:18:59·¢±í:
ºÃÎÄÕ¾ÍÒªÍùÉ϶¥²Å¶ÔÂï
luogqiang ÓÚ 2005-11-18 08:56:15·¢±í:
artiomgy ÓÚ 2005-11-17 00:43:04·¢±í:
¶¥
ÐÂРÓÚ 2005-09-30 00:28:35·¢±í:
UP
artiomgy ÓÚ 2005-09-06 00:09:29·¢±í:
¶¥
ABC ÓÚ 2005-08-10 10:26:35·¢±í:
Ö§³Ö
zz123 ÓÚ 2005-08-01 00:15:46·¢±í:
Ö§³Ö°¡
ºÎ¸ç ÓÚ 2005-07-06 00:23:46·¢±í:
Ö§³Ö
chentong ÓÚ 2005-06-19 11:45:55·¢±í:
²»´íµÄ¶«¶«
ÖйúÈË ÓÚ 2005-06-07 00:36:22·¢±í:
¶¥
chentong ÓÚ 2005-05-24 00:34:11·¢±í:
¶¥
rebort_com ÓÚ 2005-05-17 21:33:42·¢±í:
¶¥~~~~~~~~~~~~~~``
yangzihy ÓÚ 2005-05-17 08:22:05·¢±í:
Ö§³Ö¡£
yangzi ÓÚ 2005-05-10 09:39:31·¢±í:
Ö§³Ö¡£
thej ÓÚ 2005-05-07 02:32:42·¢±í:
hehe