仅使用g ++工作,但不使用“g ++ -c”和ld [英] Using only g++ works, but not "g++ -c" and ld

查看:273
本文介绍了仅使用g ++工作,但不使用“g ++ -c”和ld的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在main.cpp中有以下源代码:

  #include< iostream> 
#include< iomanip>

int main(){
std :: cout< Hi< std :: endl;
return 0;
}

使用此命令可以工作,并创建可执行文件:

  g ++ -o main main.cpp 

但此命令不起作用:

  g ++ -c main.cpp 
ld -o main main.o

第二个错误与:


$ b b

  ld:warning:找不到条目符号_start;默认为00000000004000e8 
main.o:在函数`main'中:
main.cpp :( .text + 0xa):未定义引用`std :: cout'
main.cpp .text + 0xf):未定义引用`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):未定义引用`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):未定义引用`std :: ostream :: operator& <(std :: ostream&(*)(std :: ostream&))'
main.o:在函数`__static_initialization_and_destruction_0(int,int)':
main.cpp :(。 + 0x4a):未定义引用`std :: ios_base :: Init :: Init()'
main.cpp :(。text + 0x4f):未定义引用`std :: ios_base :: Init ::〜 Init()'
main.cpp :( .text + 0x54):未定义的引用`__dso_handle'
main.cpp :(。text + 0x61):未定义引用`__cxa_atexit'


解决方案

我认为如果你使用 ld 直接它不包括C ++库默认情况下。您可以使用 g ++ 执行链接,它将使用正确的设置调用 ld

  g ++ -c main.cpp 
g ++ -o main main.o
pre>

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

The second one errors with:

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'

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆