C++ importing libraries instead of linking?

444 views Asked by At

I'm new to C++. When I write a program I expect it to compile into a standalone executable, but with C++ there's a lot of talk about dynamic and static linking. From what I gather this means the separate libraries used are compiled separately and linked rather than compiled together.

Compile time is not an issue for me. I don't see why I want to link to a library rather than compile it directly with my code as one. Surely that would result in better optimization and inlining.

A perfect example is tcmalloc. I'd like to to use the tcmalloc memory allocator, not the profiler that is bundled with, and not link to it statically or dynamically, but compile it directly into my program with inlining optimizations.

How do I do that?

1

There are 1 answers

1
Joseph Larson On

Okay, a part of C++ programming is the use of libraries. Unless you're doing embedded programming or something weird, you will probably never write a C++ program that doesn't include at least some other libraries. If nothing else, you get the standard system libraries so you have access to things like cout, fopen, or whatever system calls you're trying to make.

If you want to have code inlining for performance reasons, then it should appear in your include files as part of your definitions.

There's no advantage and a whole ton of disadvantages to "compile everything together". First off, you probably don't have access to most of the code. Then you'd have to know what code to include and cat it all into one huge-ass file. Then compile that sucker and come back tomorrow.