下面就是这个例子:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
它抛出错误:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): 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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
另外,此示例如下:
#include <iostream>
int main()
{
std::cout<<"Hola, moondo.\n";
}
抛出错误:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): 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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
注意:我使用的是Debian Wheezy。
发布于 2015-07-10 11:52:38
是的,使用g++
命令对我有效:
g++ my_source_code.cpp
发布于 2020-09-30 14:12:43
假设源代码为code.cpp
,则以下代码不会抛出错误:
make code
./code
在这里,第一个命令编译代码并创建一个同名的可执行文件,第二个命令运行它。这种情况下不需要指定g++
关键字。
发布于 2019-09-03 04:19:39
生成文件
如果您正在使用makefile,并且最终像我一样来到了这里,那么这可能就是您正在寻找的东西,或者:
如果您使用的是makefile,则需要更改cc
,如下所示
my_executable : main.o
cc -o my_executable main.o
至
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
https://stackoverflow.com/questions/28236870
复制相似问题