gcc/g++ 编译 .cpp文件

4 篇文章 0 订阅

贴一段代码

#include <iostream>

using namespace std;

struct Copyable
{
	Copyable(){}
	Copyable(const Copyable &o)
	{
		cout << "Copied" << endl;
	}

};

Copyable ReturnRValue()
{
	return Copyable();
}

void AcceptVal(Copyable)
{

}

void AcceptRef(const Copyable &){}

int main()
{
	cout << "Pass by value: " << endl;
	AcceptVal(ReturnRValue());
	cout << "Pass by reference: " << endl;
	AcceptRef(ReturnRValue());

	return 0;
}

在 VS编译没有问题

但是用gcc编译报错:

lzf@ubuntu:~/MyTest/copycons$ gcc -o test test.cpp
/tmp/ccuN8uHx.o: In function `main':
test.cpp:(.text+0x55): undefined reference to `std::cout'
test.cpp:(.text+0x5a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x64): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x6f): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0x95): undefined reference to `std::cout'
test.cpp:(.text+0x9a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0xa4): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0xaf): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccuN8uHx.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x10b): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x120): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

这里注意,gcc和g++有点区别,后缀名为.c和.cpp的文件,g++都会当成C++,而gcc会把.c的当成c源程序

另外,最终要的一点是,对于cpp程序,编译的时候可以用gcc/g++,但是链接的时候,一定要在最后加上

"-Istdc++" ,这里第一个字母是大些的 "I"

要这样编译          g++ -o test test.cpp -Istdc++

lzf@ubuntu:~/MyTest/copycons$ g++ -o test test.cpp -Istdc++
lzf@ubuntu:~/MyTest/copycons$ gcc -o test test.cpp -Istdc++
/tmp/ccP5pxZG.o: In function `main':
test.cpp:(.text+0x55): undefined reference to `std::cout'
test.cpp:(.text+0x5a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0x64): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x6f): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0x95): undefined reference to `std::cout'
test.cpp:(.text+0x9a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cpp:(.text+0xa4): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0xaf): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccP5pxZG.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x10b): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x120): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
lzf@ubuntu:~/MyTest/copycons$ 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值