gcc/g++ 编译 .cpp文件

贴一段代码

#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$ 
发布了80 篇原创文章 · 获赞 19 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qiukapi/article/details/105095682