1

How does some std-lib, external-libs or any other pre-compiled src code such as the well-known header file <iostream> with its corresponding object file or static/dll lib get linked into my own application automatically? Does the compiler do it implicitly/under-the-hood or something like a compiler pre-linked list operation?

If such a case exist how do we use its functionality in our accord, Is there a way to put my own obj, dll, static-lib or src file into that ideal list via writing some special syntax without changing the initial directories of each of it, neither the help of an IDE config and outside-software, the goal is to drop the linking phase explicitly at terminal, want to do this configuration inside of the src-code.

Does every std-lib had a direct/inline special src-code that doing this kind of operation? If there are, then how do we take advantage of it? Or if everything is done by a compiler/handler and if it is generic-type then you could modified it with less problem but the delema is, it is fixed with the compiler and hate to forcefully modified/forked it. If there is alreadly a way to do this without explicitly tinkering it, for such doing it only at your onw src-code/write-time as a said at the first line of this block statement: "Does every std-lib/external-lib had a direct/inline special src-code that doing this kind of operation?".

// a.cpp
#include<iostream>

// there's no linking on iostream obj, src, dll, static-lib file
// love to have this kind of special features to our own none-std-lib/etc.
>c/cpp-compiler -c a.cpp
>c/cpp-compiler -o a a.o

Note: some of my terminologies are base on my own experience so watch out and be open-minded. For as I grow in the coding-community using terminology/standard way of communicating are a mess specially exploring from low to another low and to high to another high level prog-lang.

  • 2
    The compiler has lots of default option settings. Linking with the standard library is one of them. – Barmar Nov 17 '21 at 04:20
  • 1
    Try running `your-compiler -v -o a a.o`. For gcc and clang, the last part of this output will be the actual lower-level link command it ends up running. – aschepler Nov 17 '21 at 04:33
  • @Barmar, is the current compiler cannot do this, I imagine I have this packaging file ( zip/jar/etc... ) that containing my own self-proclaim-std-lib/src then setting it as an arguments in the compiler such like a `-std=c++*` but in this way `-std-pkg=self-proclaim-std.zip` which is adding or changing the std-lib. Or alternatively `#include"mylib.h" @link(path/to/mylib.so)`. or even a programmatic syntax that can be done at write-time. – Liveon Phoenix Nov 17 '21 at 04:43
  • The [GCC](http://gcc.gnu.org/) compiler is like [binutils](https://www.gnu.org/software/binutils/) open source. You are allowed to study its source code and improve it. It will take years of work. And you could develop your [GCC plugin](https://gcc.gnu.org/onlinedocs/gccint/Plugins.html). See also the [DECODER](https://decoder-project.eu/) project – Basile Starynkevitch Nov 17 '21 at 06:01
  • 1
    I don't know what you mean by "write-time". The standard library is *not* linked into the object file. It is linked into the executable. If you're using GCC, you can provide a custom spec file; I'm pretty sure the spec file defines the names and filepaths for the automatically linked libraries. The same facilities exist, in radically different forms, in other compilers. But that's an awful lot of work to go to just to avoid a few `-l` options. – rici Nov 17 '21 at 06:02
  • Related: [Link only libc, and not libc++ in C++](https://stackoverflow.com/questions/35411370/link-only-libc-and-not-libc-in-c) – JaMiT Nov 17 '21 at 06:04
  • Here's the Clang equivalent to GCC spec files: [configuration files](https://clang.llvm.org/docs/UsersManual.html#configuration-files). Unlike GCC (afaik), Clang makes it possible to easily create a custom executable with its own config file; all you need to do is make a symlink to clang to give your custom version its own name, and put a config file with that same name and extension `.cfg` in the same directory as the clang executable. (I haven't actually tried this, but I have used GCC spec files.) – rici Nov 17 '21 at 06:39
  • I do not understand last two questions. `has a direct/inline special src-code running in the background` To what property of standard library or "external libraries" does this refer to? If a shared library executes something in the background, depends if it spawns threads, it can do that when running the program it was linked to. I do not understand, there is nothing "running in the background". – KamilCuk Nov 17 '21 at 08:49
  • Please do **not** overuse **bold** to **catch our attention**. It has the **opposite** effect. Thank **you** for **your** understanding. – n. m. could be an AI Nov 17 '21 at 09:05
  • C and C++ are *abstract specifications*. They do not prescribe what exact actions an implementation must take in order to make user programs run, nor do they specify ways to change these actions. Each implementation may or may not have its own way to do so. If you are interested in a particular compiler/implementation/toolchain, please specify it in the question, otherwise the answer is juts "read your compiler documentation". – n. m. could be an AI Nov 17 '21 at 09:16

4 Answers4

3

It depends on what you call "the compiler".

Most modern toolchains - including gcc, clang, Visual C++ - are based on a "compile then link" model, with several components. One of those components is the preprocessor (which does text substitution on C or C++ source code, to produce some modified source code), a "compiler" that translates preprocessed source code into object files, utility programs that produce libraries from sets of source files, a linker that produces an executable file from a set of object files and libraries, and - last but by no means least - a driver program that coordinates execution of other components.

The specifics are different between toolchains - e.g. VC++ does things quite differently than gcc/g++ or clang. The concepts are similar.

In what follows, I'll give a very over-simplistic (imprecise, details omitted) discussion of what gcc and g++ (in the gnu compiler collection) do.

When you use gcc or g++ at the command line you're actually using a driver program, that orchestrates execution of a bunch of other programs (the preprocessor, the compiler, the linker, etc). Depending on what options you provide, the result produced differs. For example, gcc -E only completes preprocessing of source files, g++ -c means the process stops after compiling source files to produce object files. If used to produce an executable, the driver program will use the linker to (well!) link object files and libraries together to produce an executable.

So, if you think of gcc or g++ (the program you execute directly) as the compiler then you could claim the compiler does implicit linking. When being used to create an executable, both execute the linker - and provide it information needed (e.g. names of libraries). gcc automatically links in libraries needed by C programs (e.g. the C standard library) while g++ automatically links in libraries needed by C++ programs (e.g. parts of the C++ standard library as well as the C standard library).

However, if you take a narrower view of the compiler - it is the program that only translates source files into object files - then there is no implicit linking of libraries by the compiler. It is the driver program that orchestrates compiling and linking, not the compiler that orchestrates linking.

If you read documentation for your favourite toolchain, it will describe various means (extensions of source files, settings, command line options, values of environment variables, etc) to control what it does. There is typically flexibility to do preprocessing only, compilation only, output assembler, linking only, or a complete "compile multiple source files then link them together to produce an executable" process.

Peter
  • 35,646
  • 4
  • 32
  • 74
2

The linker search libraries in some oreder in which the standard libs folder is searched first. There are somed default libraries that gets loaded by default like glibc. this way you dont need to specify to the linker to link with standard libs. Gcc even have flags for not linking with some standard libs

https://docs.oracle.com/cd/E19205-01/819-5262/auto29/index.html

kobi
  • 101
  • 7
  • Yap, this will do, but I need a kind of linking at write-time. So that we would not explicitly link it as an arguments/options with the compiler on terminal. – Liveon Phoenix Nov 17 '21 at 05:01
  • 1
    I havent try thus out, but you can try to read: https://unix.stackexchange.com/a/425349 – kobi Nov 17 '21 at 05:37
  • 1
    @kobi: That question is about adding library *paths* (`-L` option), as is your answer. If I understand correctly, OP is asking about adding libraries (`-l` option). Of course, I might be wrong. – rici Nov 17 '21 at 06:07
  • The comment which i linked to suggests to add to `/etc/ld.so.con` the libs and than run ldconfig. It also suggests -L option. – kobi Nov 17 '21 at 07:13
1

Note that while it is not standard Microsoft's Visual C++ has a #pragma based language extension that allows specifying files to link in the source:

#pragma comment(lib, "yourfile.lib") // or yourfile.obj

The comment pragma can also be used to specify a few other linker command line options, for example:

#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='' publicKeyToken='6595b64144ccf1df' language=''"")

Note that the list of linker options that can be specified this way is fairly limited and that while there are a few other legal 'comment' types only lib and linker really have meaning.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

In C or C++, does the compiler do implicit linking?

As the "compiler" (understood as the whole group of tools in the chain that generate the final executable) has whole control over creating that final executable, it does everything related to every stage of compilation, including implicit linking.

How does some std-lib, external-libs or any other pre-compiled src code such as the well-known header file with its corresponding object file or static/dll lib get linked into my own application automatically?

The same as any other library is linked - linker searches the library for symbols and uses them.

Does the compiler do it implicitly/under-the-hood or something like a compiler pre-linked list operation?

Yes (for the compilers I worked with).

But it's very specific to the compiler. From the point of C++ language, there is no requirement on compiler command line options. If the compiler -needs-this-option-to-link-with-standard-library, it's fine and specific to that compiler. It's a quality of implementation issue. Surely users would want some things to be done implicitly with sane defaults for that compiler.

how do we use its functionality in our accord, Is there a way to put my own obj/dll/static-lib/src file into that ideal list via writing some special syntax without changing the initial directories of each of i

Because the compiler does it implicitly, you have to modify the compiler. That strongly depends on the compiler, and specific system and specific compiler own very specific configuration and build settings.

For example on Linux with gcc you can use the method in Enable AddressSanitizer by default in gcc . You can also use the method in Custom gcc preprocessor but overwrite collect2 stage.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111