ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux³ÌÐò¿ª·¢»ù´¡¸ÅÄî

·¢²¼Ê±¼ä:2008-03-02 00:13:57À´Ô´:ºìÁª×÷Õß:Inetxper
ÔÚÉèÖÃLinuxµÄϵͳ·¾¶Ê±£¬Ê¹ÓÃðºÅ·Ö¸ôÿ¸ö·¾¶Ãû¡£Èç:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

ÔÚLinuxÖеijÌÐòÓÐÁ½ÖÖ£¬Ò»ÖÖÊÇ¿ÉÖ´ÐгÌÐò£¬ÓëWindowsϵÄ.exeÎļþÀàËÆ£¬Ò»ÖÖÊǽű¾£¬ÓëWindowsϵÄ.batÎļþÀàËÆ¡£

LinuxÖг£ÓõijÌÐò´æ·Å·¾¶ÓÐÒÔϼ¸¸ö£º

/bin£¬¸Ã·¾¶´æ·ÅϵͳÆô¶¯Ê±ÐèҪʹÓõijÌÐò¡£

/usr/bin£¬¸Ã·¾¶´æ·ÅÓû§ÐèʹÓõıê×¼³ÌÐò¡£

/usr/local/bin£¬¸Ã·¾¶´æ·Å±¾µØ°²×°µÄ³ÌÐò¡£

LinuxʹÓÃб¸Ü"/"·Ö¸ô·¾¶Ãû£¬¶ø²»ÊÇWindowsµÄ·´Ð±¸Ü"\"¡£

LinuxϵÄC±àÒëÆ÷ʹÓÃGCC£¬ÓÉÓÚÀúÊ·µÄÔ­Òò£¬ÔÚPOSIX¼æÈݵIJÙ×÷ϵͳÖУ¬C±àÒëÆ÷¶¼½Ðcc£¬ËùÒÔLinuxÏÂÒ²ÓÐÒ»¸öccÃüÁËüÊÇÒ»¸öµ½gccµÄÈíÁ´½Ó¡£

¿ª·¢¹¤¾ß£¬¶àÊýλÓÚ/usr/bin»ò/usr/local/binĿ¼Ï¡£

Í·Îļþ£¬Î»ÓÚ/usr/includeĿ¼¡£Í·Îļþ°üº¬Óг£Á¿¶¨Ò塢ϵͳµ÷ÓúͿ⺯Êýµ÷ÓõÄÉùÃ÷¡£ÕâÊÇϵͳĬÈϵÄÍ·Îļþ´æ·Å·¾¶£¬ÔÚ±àÒë³ÌÐòʱ£¬±àÒëÆ÷»á×Ô¶¯²éÕÒ¸ÃĿ¼¡£gcc±àÒëÆ÷ÔÚ±àÒë³ÌÐòʱҲ¿ÉÓÃ-I²ÎÊýÖ¸¶¨ÁíÍâµÄÍ·Îļþ·¾¶¡£È磺

gcc -I/usr/local/myinclude test.c¡£
¿âÎļþ£¬¿âÊÇÒ»×éÒѱàÒëµÄº¯Êý¼¯ºÏ£¬¿É·½±ãÎÒÃÇÖØÓôúÂ롣ĬÈÏ´æ·ÅÔÚ/libºÍ/usr/libĿ¼¡£¿âÎļþ¿É·ÖΪ¾²Ì¬ºÍ¹²ÏíÁ½Àà¡£

.a£¬¾²Ì¬¿âÎļþ¡£Ê¹Óþ²Ì¬¿â½«»á°ÑËùÓеĿâ´úÂëÒýÈë³ÌÐò£¬Õ¼Óøü¶àµÄ´ÅÅÌ¿Õ¼äºÍÄÚ´æ¿Õ¼ä£¬ËùÒÔÒ»°ã½¨ÒéʹÓù²Ïí¿â¡£

.so£¬¹²Ïí¿âÎļþ¡£Ê¹Óù²Ïí¿âµÄ³ÌÐò²»°üº¬¿â´úÂ룬ֻÔÚ³ÌÐòÔËÐвŵ÷Óù²Ïí¿âÖеĴúÂë¡£

ÔÚ±àÒëʱ¿ÉÓðüº¬Â·¾¶µÄ¿âÎļþÃû»òÓÃ-l²ÎÊýÖ¸¶¨Ê¹ÓõĿâÎļþ£¬/usr/lib/libm.aµÈ¼ÛÓÚ-lm¡£Èç:

gcc -o hello hello.c /usr/lib/libm.a
»òÓÃ-l²ÎÊýд³É
gcc -o hello hello.c -lm
Èç¹ûÎÒÃÇҪʹÓõĿâÎļþ²»ÔÚĬÈÏλÖã¬ÔÚ±àÒë³ÌÐòʱ¿ÉÓÃ-L²ÎÊýÖ¸¶¨¿âÎļþµÄ·¾¶¡£ÏÂÃæÀý×ÓʹÓÃÁË/usr/hello/libĿ¼ÏµÄlibhello¿âÎļþ£º

gcc -o hello -L/usr/hello/lib hello.c -lhello
´´½¨ºÍʹÓþ²Ì¬¿â¡£

·Ö±ð´´½¨Á½¸öº¯Êý£¬º¯ÊýaµÄÄÚÈÝÈçÏ£º

#include

void a(char *arg)
{
printf("function a,hello world %s\n",arg);
}
º¯ÊýbµÄÄÚÈÝÈçÏ£º

#include

void b(int arg)
{
printf("function b,hello world %d\n",arg);
}
½Ó×Å£¬Éú³ÉÁ½¸ö¶ÔÏóÎļþ¡£

debian:~/c# gcc -c a.c b.c
debian:~/c# ls *.o
a.o b.o
×îºó£¬ÓÃar¹éµµÃüÁî°ÑÉú³ÉµÄ¶ÔÏóÎļþ´ò°ü³ÉÒ»¸ö¾²Ì¬¿âlibhello.a¡£

debian:~/c# ar crv libhello.a a.o b.o
r - a.o
r - b.o
ΪÎÒÃǵľ²Ì¬¿â¶¨ÒåÒ»¸öÍ·Îļþlib.h£¬°üº¬ÕâÁ½¸öº¯ÊýµÄ¶¨Òå¡£

/*
* this is a header file.
*/
void a(char *arg);
void b(int arg);
}}}
* ´´½¨jims.c³ÌÐò£¬ÄÚÈÝÈçÏ¡£{{{#!cplusplus
#include "lib.h"

int main()
{
a("jims.yang");
b(3);
exit(0);
}
ÀûÓþ²Ì¬Á´½Ó¿â±àÒë³ÌÐò¡£

debian:~/c# gcc -c jims.c
debian:~/c# gcc -o jims jims.o libhello.a
debian:~/c# ./jims
function a,hello world jims.yang
function b,hello world 3
debian:~/c#
gcc -o jims jims.o libhello.aÒ²¿ÉÒÔд³Égcc -o jims jims.o -L. -lhello

Ô¤´¦Àí£¬ÔÚ³ÌÐò¿ªÍ·ÒÔ¡°#¡±¿ªÍ·µÄÃüÁî¾ÍÊÇÔ¤´¦ÀíÃüÁËüÔÚÓ﷨ɨÃèºÍ·ÖÎö·¨Ê±±»Ô¤´¦Àí³ÌÐò´¦Àí¡£Ô¤´¦ÀíÓÐÒÔϼ¸Àࣺ

ºê¶¨Ò壬ÓÃ#defineÖ¸ÁÒå¡£È磺#define BUFFER 1024¡£È¡Ïûºê¶¨ÒåÓÃ#undefÖ¸Áî¡£ºê»¹¿É´ø²ÎÊý£¬È磺

#define BUF(x) x*3
°üº¬Í·Îļþ£¬ÓÃ#includeÖ¸Á¿É°Ñ°üº¬µÄÎļþ´úÂë²åÈ뵱ǰλÖá£È磺

<#include ¡£
°üº¬µÄÎļþ¿ÉÒÔÓüâÀ¨ºÅ£¬Ò²¿ÉÓÃË«ÒýºÅ£¬È磺

#include "stdio.h"¡£
²»Í¬Ö®´¦ÊÇ£¬Ê¹ÓüâÀ¨ºÅ±íʾÔÚϵͳµÄ°üº¬Ä¿Â¼£¨/usr/include£©Ï²éÕÒ¸ÃÎļþ£¬¶øË«ÒýºÅ±íʾÔÚµ±Ç°Ä¿Â¼Ï²éÕÒ°üº¬Îļþ¡£Ã¿ÐÐÖ»ÄÜ°üº¬Ò»¸ö°üº¬Îļþ£¬Òª°üº¬¶à¸öÎļþÒªÓöà¸ö#includeÖ¸Áî¡£

Ìõ¼þ±àÒ룬¸ñʽÈçÏ£º

¸ñʽһ£¬Èç¹û¶¨ÒåÁ˱êʶ·û£¬Ôò±àÒë³ÌÐò¶Î1£¬·ñÔò±àÒë³ÌÐò¶Î2£º
#ifdef ±êʶ·û
³ÌÐò¶Î1
#else
³ÌÐò¶Î2
#endif

¸ñʽ¶þ£¬Èç¹û¶¨ÒåÁ˱êʶ·û£¬Ôò±àÒë³ÌÐò¶Î2£¬·ñÔò±àÒë³ÌÐò¶Î1£¬Óë¸ñʽһÏà·´£º
#ifndef ±êʶ·û
³ÌÐò¶Î1
#else
³ÌÐò¶Î2
#endif

¸ñʽÈý£¬³£Á¿±í´ïʽΪÕæÔò±àÒë³ÌÐò¶Î1£¬·ñÔò±àÒë³ÌÐò¶Î2£º
#if ³£Á¿±í´ïʽ
³ÌÐò¶Î1
#else
³ÌÐò¶Î2
#endif

ʹÓÃgcc±àÒë³ÌÐòʱ£¬Òª¾­¹ýËĸö²½Öè¡£

Ô¤´¦Àí£¨Pre-Processing£©£¬ÓÃ-E²ÎÊý¿ÉÒÔÉú³ÉÔ¤´¦ÀíºóµÄÎļþ¡£

debian:~/c# gcc -E hello.c -o hello.i
±àÒ루Compiling£©

»ã±à£¨Assembling£©

Á´½Ó£¨Linking£©

GCCĬÈϽ«.iÎļþ¿´³ÉÊÇÔ¤´¦ÀíºóµÄCÓïÑÔÔ´´úÂ룬ËùÒÔÎÒÃÇ¿ÉÒÔÕâÑù°Ñ.iÎļþ±àÒë³ÉÄ¿±êÎļþ¡£

debian:~# gcc -c hello.i -o hello.o}}}
ÔÚGCCÖÐʹÓÃ-pedanticÑ¡ÏîÄܹ»°ïÖú³ÌÐòÔ±·¢ÏÖһЩ²»·ûºÏANSI/ISO C±ê×¼µÄ´úÂ룬µ«²»ÊÇÈ«²¿¡£´Ó³ÌÐòÔ±µÄ½Ç¶È¿´£¬º¯Êý¿âʵ¼ÊÉϾÍÊÇһЩͷÎļþ£¨.h£©ºÍ¿âÎļþ£¨.so»òÕß.a£©µÄ¼¯ºÏ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ