Using only g++ works, but not quot;g++ -cquot; and ld(仅使用 g++ 有效,但不能使用“g++ -c;和 ld)
问题描述
我在 main.cpp 中有以下源代码:
I have the following source code in main.cpp:
#include <iostream>
#include <iomanip>
int main() {
std::cout << "Hi" << std::endl;
return 0;
}
使用此命令有效,并创建可执行文件:
Using this command works, and creates the executable file:
g++ -o main main.cpp
但是这个命令不起作用:
But this commands don't work:
g++ -c main.cpp
ld -o main main.o
第二个错误:
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
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.cpp:(.text+0x14): 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> >&)'
main.cpp:(.text+0x1c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x4a): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4f): undefined reference to `std::ios_base::Init::~Init()'
main.cpp:(.text+0x54): undefined reference to `__dso_handle'
main.cpp:(.text+0x61): undefined reference to `__cxa_atexit'
推荐答案
我想如果你直接使用 ld
默认不包含 C++ 库.您也可以使用 g++
进行链接,它会以正确的设置调用 ld
.
I think if you use ld
directly it does not include the C++ libraries by default. You can use g++
to do the linking as well, it will call ld
with the correct settings.
g++ -c main.cpp
g++ -o main main.o
这篇关于仅使用 g++ 有效,但不能使用“g++ -c";和 ld的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅使用 g++ 有效,但不能使用“g++ -c";和 ld


基础教程推荐
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01