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

gccºÍg++µÄÇø±ð

·¢²¼Ê±¼ä:2008-09-28 18:05:17À´Ô´:ºìÁª×÷Õß:jerry520
ºÜ¶àÈ˲»ÊǺÜÃ÷°×GCCÓëG++µÄÇø±ð
gccºÍg++¶¼ÊÇGNU(×éÖ¯)µÄÒ»¸ö±àÒëÆ÷¡£
ÎóÇøÒ»:gccÖ»ÄܱàÒëc´úÂë,g++Ö»ÄܱàÒëc++´úÂë
Á½Õ߶¼¿ÉÒÔ£¬µ«ÊÇÇë×¢Ò⣺
1.ºó׺Ϊ.cµÄ£¬gcc°ÑËüµ±×÷ÊÇC³ÌÐò£¬¶øg++µ±×÷ÊÇc++³ÌÐò£»ºó׺Ϊ.cppµÄ£¬Á½Õ߶¼»áÈÏΪÊÇc++³ÌÐò£¬×¢Ò⣬ËäÈ»c++ÊÇcµÄ³¬¼¯£¬µ«ÊÇÁ½Õ߶ÔÓï·¨µÄÒªÇóÊÇÓÐÇø±ðµÄ¡£C++µÄÓï·¨¹æÔò¸ü¼ÓÑϽ÷һЩ¡£
2.±àÒë½×¶Î£¬g++»áµ÷ÓÃgcc£¬¶ÔÓÚc++´úÂ룬Á½ÕßÊǵȼ۵ģ¬µ«ÊÇÒòΪgccÃüÁî²»ÄÜ×Ô¶¯ºÍC£«£«³ÌÐòʹÓõĿâÁª½Ó£¬ËùÒÔͨ³£ÓÃg++À´Íê³ÉÁ´½Ó£¬ÎªÁËͳһÆð¼û£¬¸É´à±àÒë/Á´½ÓͳͳÓÃg++ÁË£¬Õâ¾Í¸øÈËÒ»ÖÖ´í¾õ£¬ºÃÏñcpp³ÌÐòÖ»ÄÜÓÃg++ËƵġ£

ÎóÇø¶þ:gcc²»»á¶¨Òå__cplusplusºê£¬¶øg++»á
ʵ¼ÊÉÏ£¬Õâ¸öºêÖ»ÊDZêÖ¾×űàÒëÆ÷½«»á°Ñ´úÂë°´C»¹ÊÇC++Óï·¨À´½âÊÍ£¬ÈçÉÏËùÊö£¬Èç¹ûºó׺Ϊ.c£¬²¢ÇÒ²ÉÓÃgcc±àÒëÆ÷£¬Ôò¸Ãºê¾ÍÊÇ䶨ÒåµÄ£¬·ñÔò£¬¾ÍÊÇÒѶ¨Òå¡£

ÎóÇøÈý:±àÒëÖ»ÄÜÓÃgcc£¬Á´½ÓÖ»ÄÜÓÃg++
ÑϸñÀ´Ëµ£¬Õâ¾ä»°²»Ëã´íÎ󣬵«ÊÇËü»ìÏýÁ˸ÅÄӦ¸ÃÕâÑù˵£º±àÒë¿ÉÒÔÓÃgcc/g++£¬¶øÁ´½Ó¿ÉÒÔÓÃg++»òÕßgcc -lstdc++¡£ÒòΪgccÃüÁî²»ÄÜ×Ô¶¯ºÍC£«£«³ÌÐòʹÓõĿâÁª½Ó£¬ËùÒÔͨ³£Ê¹ÓÃg++À´Íê³ÉÁª½Ó¡£µ«ÔÚ±àÒë½×¶Î£¬g++»á×Ô¶¯µ÷ÓÃgcc£¬¶þÕߵȼۡ£
gccºÍg++µÄÇø±ð
ÎÒÃÇÔÚ±àÒëc/c++´úÂëµÄʱºò£¬ÓÐÈËÓÃgcc£¬ÓÐÈËÓÃg++£¬ÓÚÊǸ÷ÖÖ˵·¨¶¼À´ÁË£¬Æ©Èçc´úÂëÓÃgcc£¬¶øc++´úÂëÓÃg++£¬»òÕß˵±àÒëÓà gcc£¬Á´½ÓÓÃg++£¬Ò»Ê±Ò²²»ÖªÄĸö˵·¨ÕýÈ·£¬Èç¹ûÔÙÓöÉϸöextern "C"£¬·ÖÆç¾Í¸ü¶àÁË£¬ÕâÀïÎÒÏë×÷¸öÁ˽ᣬ±Ï¾¹ÖªÊ¶µÄÄ¿µÄÊÇÁîÈ˸üÇåÐÑ£¬¶ø²»ÊǸüºýÍ¿¡£

ÎóÇøÒ»:gccÖ»ÄܱàÒëc´úÂë,g++Ö»ÄܱàÒëc++´úÂë
Á½Õ߶¼¿ÉÒÔ£¬µ«ÊÇÇë×¢Ò⣺
1.ºó׺Ϊ.cµÄ£¬gcc°ÑËüµ±×÷ÊÇC³ÌÐò£¬¶øg++µ±×÷ÊÇc++³ÌÐò£»ºó׺Ϊ.cppµÄ£¬Á½Õ߶¼»áÈÏΪÊÇc++³ÌÐò£¬×¢Ò⣬ËäÈ»c++ÊÇcµÄ³¬¼¯£¬µ«ÊÇÁ½Õ߶ÔÓï·¨µÄÒªÇóÊÇÓÐÇø±ðµÄ£¬ÀýÈ磺
#include
int main(int argc, char* argv[]) {
if(argv == 0) return;
printString(argv);
return;
}
int printString(char* string) {
sprintf(string, "This is a test.\n");
}
Èç¹û°´ÕÕCµÄÓï·¨¹æÔò£¬OK£¬Ã»ÎÊÌ⣬µ«ÊÇ£¬Ò»µ©°Ñºó׺¸ÄΪcpp£¬Á¢¿Ì±¨Èý¸ö´í£º¡°printString䶨Ò塱£»
¡°cannot convert `char**' to `char*¡±£»
¡±return-statement with no value¡°£»
·Ö±ð¶ÔӦǰÃæºìÉ«±ê×¢µÄ²¿·Ö¡£¿É¼ûC++µÄÓï·¨¹æÔò¸ü¼ÓÑϽ÷һЩ¡£
2.±àÒë½×¶Î£¬g++»áµ÷ÓÃgcc£¬¶ÔÓÚc++´úÂ룬Á½ÕßÊǵȼ۵ģ¬µ«ÊÇÒòΪgccÃüÁî²»ÄÜ×Ô¶¯ºÍC£«£«³ÌÐòʹÓõĿâÁª½Ó£¬ËùÒÔͨ³£ÓÃg++À´Íê³ÉÁ´½Ó£¬ÎªÁËͳһÆð¼û£¬¸É´à±àÒë/Á´½ÓͳͳÓÃg++ÁË£¬Õâ¾Í¸øÈËÒ»ÖÖ´í¾õ£¬ºÃÏñcpp³ÌÐòÖ»ÄÜÓÃg++ËƵġ£

ÎóÇø¶þ:gcc²»»á¶¨Òå__cplusplusºê£¬¶øg++»á
ʵ¼ÊÉÏ£¬Õâ¸öºêÖ»ÊDZêÖ¾×űàÒëÆ÷½«»á°Ñ´úÂë°´C»¹ÊÇC++Óï·¨À´½âÊÍ£¬ÈçÉÏËùÊö£¬Èç¹ûºó׺Ϊ.c£¬²¢ÇÒ²ÉÓÃgcc±àÒëÆ÷£¬Ôò¸Ãºê¾ÍÊÇ䶨ÒåµÄ£¬·ñÔò£¬¾ÍÊÇÒѶ¨Òå¡£

ÎóÇøÈý:±àÒëÖ»ÄÜÓÃgcc£¬Á´½ÓÖ»ÄÜÓÃg++
ÑϸñÀ´Ëµ£¬Õâ¾ä»°²»Ëã´íÎ󣬵«ÊÇËü»ìÏýÁ˸ÅÄӦ¸ÃÕâÑù˵£º±àÒë¿ÉÒÔÓÃgcc/g++£¬¶øÁ´½Ó¿ÉÒÔÓÃg++»òÕßgcc -lstdc++¡£ÒòΪgccÃüÁî²»ÄÜ×Ô¶¯ºÍC£«£«³ÌÐòʹÓõĿâÁª½Ó£¬ËùÒÔͨ³£Ê¹ÓÃg++À´Íê³ÉÁª½Ó¡£µ«ÔÚ±àÒë½×¶Î£¬g++»á×Ô¶¯µ÷ÓÃgcc£¬¶þÕߵȼۡ£

ÎóÇøËÄ:extern "C"Óëgcc/g++ÓйØϵ
ʵ¼ÊÉϲ¢ÎÞ¹Øϵ£¬ÎÞÂÛÊÇgcc»¹ÊÇg++£¬ÓÃextern "c"ʱ£¬¶¼ÊÇÒÔCµÄÃüÃû·½Ê½À´ÎªsymbolÃüÃû£¬·ñÔò£¬¶¼ÒÔc++·½Ê½ÃüÃû¡£ÊÔÑéÈçÏ£º
me.h£º
extern "C" void CppPrintf(void);

me.cpp:
#include
#include "me.h"
using namespace std;
void CppPrintf(void)
{
cout
#include
#include "me.h"
int main(void)
{
CppPrintf();
return 0;
}

1. Ïȸøme.h¼ÓÉÏextern "C"£¬¿´ÓÃgccºÍg++ÃüÃûÓÐʲô²»Í¬
[root@root G++]# g++ -S me.cpp
[root@root G++]# less me.s
.globl _Z9CppPrintfv //×¢Òâ´Ëº¯ÊýµÄÃüÃû
.type CppPrintf, @function
[root@root GCC]# gcc -S me.cpp
[root@root GCC]# less me.s
.globl _Z9CppPrintfv //×¢Òâ´Ëº¯ÊýµÄÃüÃû
.type CppPrintf, @function
ÍêÈ«Ïàͬ£¡

2. È¥µôme.hÖÐextern "C"£¬¿´ÓÃgccºÍg++ÃüÃûÓÐʲô²»Í¬
[root@root GCC]# gcc -S me.cpp
[root@root GCC]# less me.s
.globl _Z9CppPrintfv //×¢Òâ´Ëº¯ÊýµÄÃüÃû
.type _Z9CppPrintfv, @function
[root@root G++]# g++ -S me.cpp
[root@root G++]# less me.s
.globl _Z9CppPrintfv //×¢Òâ´Ëº¯ÊýµÄÃüÃû
.type _Z9CppPrintfv, @function
ÍêÈ«Ïàͬ£¡
¡¾½áÂÛ¡¿ÍêÈ«Ïàͬ£¬¿É¼ûextern "C"Óë²ÉÓÃgcc/g++²¢ÎÞ¹Øϵ£¬ÒÔÉϵÄÊÔÑ黹¼ä½ÓµÄÓ¡Ö¤ÁËÇ°ÃæµÄ˵·¨£ºÔÚ±àÒë½×¶Î£¬g++Êǵ÷ÓÃgccµÄ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 9 ÌõÆÀÂÛ

  1. bingyanshusheng ÓÚ 2012-05-08 14:23:06·¢±í:

    ²»´í ͦºÃµÄ

  2. °¬ÏþÝÕ ÓÚ 2011-11-24 18:04:46·¢±í:

    ¶àÉÙÄêÇ°µÄ¶¼ÍÚ³öÀ´ÁË¡£¡£Åå·þ

  3. lbiiun ÓÚ 2011-11-24 14:51:58·¢±í:

    ¿ÉÒÔ¼òµ¥µÄÈÏΪ g++ = gcc -lstdc++ -x c++ Âð£¿

  4. szzfq61 ÓÚ 2009-02-22 11:11:09·¢±í:

    good.

  5. xpxpxp111 ÓÚ 2008-10-06 12:15:59·¢±í:

    :0wl;l1 good

  6. aq4120 ÓÚ 2008-10-04 23:07:41·¢±í:

    ¶¥ÆðÀ´°É£¬Ä¿Ç°»¹¿´²»¶®£¬ÒÔºó¿ÉÒÔÀ´Ñо¿£¡

  7. slxxx ÓÚ 2008-10-04 20:08:31·¢±í:

    ÕâÑùºÃµÄÌû×ÓÓ¦¸ÃÈøü¶àµÄÈË¿´µ½~ ¶¥£¡£¡

  8. nnbbyzx ÓÚ 2008-10-01 15:41:41·¢±í:

    ÔÞ£¡

  9. cwqing1973 ÓÚ 2008-09-28 21:27:59·¢±í:

    ºÃÎÄÕ£¬ÄÜд³öÕâÖÖÎÄÕ£¬ÊǾ­ÀúÁËÐí¶àµÄÈÕÈÕÒ¹Ò¹µÄѧϰÓë˼¿¼µÄ¡£Ð»Ð»¡£¼ÓÉϼ¸·Ö£¬ÒÔ±íÖ§³Ö¡£