简体   繁体   中英

How does a function caller use a header file to determine what to do with a compiled binary?

My understanding is that C++ (and C, I guess) header files are never compiled, and simply act as an explanation of the interface of the C++ file they describe.

So if my header file describes a hello() function, some program that includes the header will know about hello() and how to call it and what arguments to give it, etc.

However, after compilation (and before linking, I guess? I'm not sure), when the hello.c file is binary machine code, and hello.h is still C++, how does the compiler/linker know how to call a function in the binary blob based on the presence of its declaration in the header file?

I understand concepts such as symbol tables, abstract syntax trees, etc (ie, I have taken a compiler class in the past), but this is a gap in my knowledge).

The implementation of hello() assumes a certain calling convention (where are the parameters on the stack, who cleans up the stack the caller or the callee, etc).

The compiler generates code with the correct calling convention. It may use information from the header file to do this (eg the function is marked __stdcall in Windows program) or it may use it's default calling convention. The compiler will also use the header file to make sure your are calling the routine with the right number and types of parameters. Once the code is generated by the compiler the header file is not used again.

The linker is not concerned with calling convention it's primary responsibility is to patch together the binaries you've compiled by fixing up references among your modules and any libraries it calls.

AC/C++ compilation unit (cpp file / c file) includes all the header files (as text) and the code.

The header file helps explain how to produce the call instruction

 push arg1
 push arg2
 call _some_function

If the compilation unit includes _some_function then this will be resolved at compile time.

Otherwise it becomes an undefined symbol . If so, when the linker comes along, it looks through all the object files and libraries to resolve all the undefined symbols .

  • So the header file helps code the assembly correctly.
  • Object and library files provide implementations.

The library files are optional. When a linker looks in a library file, it only gets added if it satisfies some symbol, otherwise it is not added to the binary.

Object files (ignoring optimization) will get added to the binary completely.

Building a C++ program is a two-step process: compile and link. The header is for compilation of the module you are writing. The binary is for linking: it contains the compiled code for the method defined in the correspnding header. The header has to match what's already been compiled. At link time you will learn if your header has a method signature that matches what was compiled in the binary.

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