错误"undefined reference to 'std::cout'"
Error "undefined reference to 'std::cout'"
这是
一个例子:
#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 7 (Wheezy)。
编译程序:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
由于cout
存在于C++标准库中,因此在使用gcc
时需要与-lstdc++
显式链接; 默认情况下,g++
链接标准库。
对于gcc
,(g++
应该优先于gcc
)
gcc main.cpp -lstdc++ -o main.o
是的,使用g++
命令对我有用:
g++ my_source_code.cpp
假设code.cpp
是源代码,以下内容不会抛出错误:
make code
./code
在这里,第一个命令编译代码并创建具有相同名称的可执行文件,第二个命令运行它。在这种情况下,无需指定g++
关键字。
生成文件
如果您正在使用 makefile 并且您最终像我一样来到这里,那么这可能是您正在寻找的内容,或者:
如果您使用的是生成文件,则需要更改cc
my_executable : main.o
cc -o my_executable main.o
自
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o
在 CMake 中添加以下行会使 gcc 与 std 链接,从而识别 std::cout
target_link_libraries(your_project
PRIVATE
-lstdc++
)
FWIW,如果你想要一个makefile,这里有一个你可以通过切换顶部的编译器来做任何一个答案。
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello
相关文章:
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- 为什么我需要做'using namespace std'而不是"使用 std::cout"?
- main() 中的 std::cout 在调试期间不会在调试控制台中打印任何内容
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- std::cout 来自多个线程
- 在 c++ 中以十六进制格式打印无符号字符(BYTE).使用 std::cout
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 使用 std::addressof(std::cout) 而不是 &std::cout 是否有任何风险?
- 是否可以为 std::cout 创建别名?
- std::cout 在打印变量与函数表达式时的行为不同
- 带有 std::cout 的多线程控制台文本动画
- 为什么在语句"std::cout << std::endl;"中使用时需要命名空间限定,给定依赖于参数的查找?
- "std::cout << std::endl;"如何编译?
- std::cout/std::cerr seg faults