1.cpp
1 #include 2 using namespace std;
3 int main()
4 {
5 cout<<"this is me\n";
6 return 0;
7 }
[root@localhost c++]# vim 1.cpp
[root@localhost c++]# g++ -o 1 1.cpp
/tmp/cc6PsEMy.o: In function `__static_initialization_and_destruction_0(int, int)':
1.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc6PsEMy.o: In function `__tcf_0':
1.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc6PsEMy.o: In function `main':
1.cpp:(.text+0x8e): undefined reference to `std::cout'
1.cpp:(.text+0x93): undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
/tmp/cc6PsEMy.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[root@localhost c++]#
jilangzhe 于 2008-03-13 23:50:19发表:
先来解决你的问题:
#include /*你的头文件有问题,仔细一点*/
using namespace std; /*添加此句*/
int main()
{
int sum=0, val=1;
while(val<=10){
sum+=val;
++val;
}
cout << "Sum of 1 to 10 inclusive is"
<< sum << endl; /*把 std:: 去掉*/
return 0;
}
他提示 /tmp/cc3oMJry.o(.text+0x39): In function `main':
: undefined reference to `std::basic_ostream >& std::endl >(std::basic_ostream >&)'
/tmp/cc3oMJry.o(.text+0x4c): In function `main':
: undefined reference to `std::cout'
/tmp/cc3oMJry.o(.text+0x51): In function `main':
: undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
/tmp/cc3oMJry.o(.text+0x5a): In function `main':
: undefined reference to `std::basic_ostream >::operator<<(int)'
/tmp/cc3oMJry.o(.text+0x63): In function `main':
: undefined reference to `std::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
/tmp/cc3oMJry.o(.text+0x90): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/cc3oMJry.o(.text+0xbf): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/cc3oMJry.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[ 本帖最后由 yunlerini 于 2008-3-10 10:33 编辑 ]
yunlerini 于 2008-03-10 10:13:24发表:
为什么这段代码编译不成功?
#include int main()
{
int sum=0, val=1;
while(val<=10){
sum+=val;
++val;
}
std::cont << "Sum of 1 to 10 inclusive is"
<< sum << std::endl;
return 0;
}
用gcc -o sum sum.cc编译不了啊
gcc -o sum sum.c 也不行 我保存了两个名字后缀都用过了
syfs 于 2012-09-15 21:00:07发表:
用g++可以编译C和C++的源代码,gcc编译要加选项的。
要是混编就用g++就行了
胶头滴管3 于 2012-09-12 21:40:17发表:
g++不久可以了嘛
LG_OPEN 于 2012-09-12 19:07:12发表:
g++即可。。。
于 2012-09-11 13:36:22发表:
1.cpp
>& std::operator<< >(std::basic_ostream >&, char const*)'
1 #include
2 using namespace std;
3 int main()
4 {
5 cout<<"this is me\n";
6 return 0;
7 }
[root@localhost c++]# vim 1.cpp
[root@localhost c++]# g++ -o 1 1.cpp
/tmp/cc6PsEMy.o: In function `__static_initialization_and_destruction_0(int, int)':
1.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc6PsEMy.o: In function `__tcf_0':
1.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc6PsEMy.o: In function `main':
1.cpp:(.text+0x8e): undefined reference to `std::cout'
1.cpp:(.text+0x93): undefined reference to `std::basic_ostream
/tmp/cc6PsEMy.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[root@localhost c++]#
jilangzhe 于 2008-03-13 23:50:19发表:
先来解决你的问题:
/*你的头文件有问题,仔细一点*/
和是不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。
时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。
和等等这样的头文件,一个是为了兼容以前的C++代码,一个是为了支持新的标准。
#include
using namespace std; /*添加此句*/
int main()
{
int sum=0, val=1;
while(val<=10){
sum+=val;
++val;
}
cout << "Sum of 1 to 10 inclusive is"
<< sum << endl; /*把 std:: 去掉*/
return 0;
}
编译命令为:g++ -o sum sum.cc
这里给你附上一些 using namespace std 的介绍:好好学呀!
C++ using namespace std 详解
所谓namespace,是指标识符的各种可见范围。C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。
一 :
后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。
因此,当使用
二:
所谓namespace,是指标识符的各种可见范围。
C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。
由于namespace的概念,使用C++标准程序库的任何标识符时,可以有三种选择:
1、直接指定标识符。例如std::ostream而不是ostream。完整语句如下:
std::cout << std::hex << 3.4 << std::endl;
2、使用using关键字。
using std::cout;
using std::endl;
以上程序可以写成
cout << std::hex << 3.4 << endl;
3、最方便的就是使用using namespace std;
例如:
#include
#include
#include
using namespace std;
这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写:
cout << hex << 3.4 << endl;
因为标准库非常的庞大,所程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都被放在名字空间std中。但这又会带来了一个新问题。无数原有的C++代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。
所以就有了
命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加".h"
yunlerini 于 2008-03-11 08:27:48发表:
两天了还是没人来解答一下吗
yunlerini 于 2008-03-10 17:10:30发表:
顶~~~~~怎么就没人回答呢!!!!!
yunlerini 于 2008-03-10 14:52:28发表:
自己顶
yunlerini 于 2008-03-10 12:42:39发表:
大家教我下吧...555
yunlerini 于 2008-03-10 10:39:35发表:
来人教我一下啊.........
yunlerini 于 2008-03-10 10:14:59发表:
他提示 /tmp/cc3oMJry.o(.text+0x39): In function `main': >& std::endl >(std::basic_ostream >&)'
>& std::operator<< >(std::basic_ostream >&, char const*)'
>::operator<<(int)'
>::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
: undefined reference to `std::basic_ostream
/tmp/cc3oMJry.o(.text+0x4c): In function `main':
: undefined reference to `std::cout'
/tmp/cc3oMJry.o(.text+0x51): In function `main':
: undefined reference to `std::basic_ostream
/tmp/cc3oMJry.o(.text+0x5a): In function `main':
: undefined reference to `std::basic_ostream
/tmp/cc3oMJry.o(.text+0x63): In function `main':
: undefined reference to `std::basic_ostream
/tmp/cc3oMJry.o(.text+0x90): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/cc3oMJry.o(.text+0xbf): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/cc3oMJry.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[ 本帖最后由 yunlerini 于 2008-3-10 10:33 编辑 ]
yunlerini 于 2008-03-10 10:13:24发表:
为什么这段代码编译不成功?
#include
int main()
{
int sum=0, val=1;
while(val<=10){
sum+=val;
++val;
}
std::cont << "Sum of 1 to 10 inclusive is"
<< sum << std::endl;
return 0;
}
用gcc -o sum sum.cc编译不了啊
gcc -o sum sum.c 也不行 我保存了两个名字后缀都用过了
[ 本帖最后由 yunlerini 于 2008-3-10 10:31 编辑 ]
NetFlow 于 2008-03-10 10:00:07发表:
gcc都可以