精华 C++ -- 002

[root@*** tem]# ls
simp.c
[root@*** tem]# cat simp.c
void main()
{}
[root@*** tem]# gcc simp.c -o simp
simp.c: In function `main':
simp.c:2: warning: return type of `main' is not `int'
[root@*** tem]# ls
simp simp.c
[root@*** tem]#
 
与你的默认g++编译参数有关。

$ g++ -Wall -c simp.cpp
simp.cpp:2: return type for `main' changed to `int'
$ ls simp*
simp.cpp
$ g++ -c simp.cpp
$ ls simp*
simp.cpp simp.o
 
that's very interesting. :)
[root@*** tem]# ls
simp.cpp
[root@*** tem]# cat simp.cpp
void main()
{}
[root@*** tem]# g++ -c simp.cpp
simp.cpp:2: `main' must return `int'
[root@*** tem]# ls
simp.cpp
[root@*** tem]# g++ -Wall -c simp.cpp
simp.cpp:2: `main' must return `int'
simp.cpp:2: return type for `main' changed to `int'
[root@*** tem]# ls
simp.cpp
[root@*** tem]#

i will check how to select the compiler parameters.:)
 
在ANSI C和ANSI C++里, main都应该是int,选择好编译option, void main()都能通过.

土图在里面加了一个function,才是妙处所在.
 
i really want to know which parameters you guys put in with g++ command. i can not make the program compile.
 
ok, me again. this time i give u a program that even can not be compiled with vc6, boland c++, and gcc/g++

void main()
{
return 0;
}
 
I think if you focus on compiler options, you totally missed the point. And you'll fail this question during interview.
 
it's not option again, at least i don't think it's.
try to make the last example compile, and show me.
 
最初由 majia041 发布
ok, me again. this time i give u a program that even can not be compiled with vc6, boland c++, and gcc/g++

void main()
{
return 0;
}

My gcc works well on it, just got two warning messages. And congratulations, your code didn't coredump.
 
nothing fancy, gcc filename.c -o filename

2 warnings are:
return type of main is not int
return with a value,in function returning void
 
come on, it is the stituation we want, right?
it's compiled in c, but not c++
try g++, or change the file name to *.C. i mean captial c. or cpp, then use g++ or gcc.
 
ask shusheng, he might use linux, I'm using cygwin, which is not real UNIX environment.
 
I am not gcc/g++ expert, maja041, my feeling is your compiler have a default setting of high warning level. Try to find compile option for ignore warnings, maybe "g++ -w" ?
 
给主考官(猪靠官)一个出其不意的答案:

只要c compiler能通过,c++ compiler选好command line option一样能过.

比如,g++ -x c filename.cc, 就直接把.cc当成c来编译了.

再次感谢大家.我认为,这道题的原创人小时候被猪亲过.
 
后退
顶部