简体   繁体   中英

Why does g++ link the standard library?

I have a simple piece of c++ code:

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

When I generate the assembly of this code I get a huge assembly file which I presume is compilation of the standard library. Why does this happen and how can I prevent it?

Much of the standard library is made up of templates. When you use a template it gets specialized for your use and the specialization will be part of your binary. This can't be avoided, although you can ensure a specialization is only in a single one of your translation units using extern templates.

std::cout is part of the standard library.

You used it.

Pretty obvious really.

Iostreams are templates, so the code is emitted in your object, not in a shared library, but with optimizations enabled the (stripped) size shouldn't be larger than necessary to implement your helloworld.

See http://www2.research.att.com/~bs/bs_faq.html#Hello-world for another answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM