Created
May 20, 2021 19:43
-
-
Save jgerityneurala/a9fdfad13d01ebac19d7b0d7e383789e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15:37 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ cat foo.h | |
int foo(); | |
15:37 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ cat foo.cpp | |
int foo() | |
{ | |
return -1; | |
} | |
15:37 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ cat bar.cpp | |
#include "foo.h" // at this point we know `int foo()` exists, but not how to execute it | |
int bar() | |
{ | |
return 42 + foo(); | |
} | |
int main(int argc, char** argv) | |
{ | |
return bar(); | |
} | |
15:37 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ gcc -c foo.cpp bar.cpp # -c means compile only, producing object files (.o) | |
15:37 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ nm foo.o # show the symbols in this object file, which is just foo | |
0000000000000000 T _Z3foov | |
15:38 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ nm bar.o # note: this object file *knows about* the symbol foo (because of the #include) but not how to execute it (the difference between declaration and definition) | |
U _GLOBAL_OFFSET_TABLE_ | |
0000000000000000 T _Z3barv | |
U _Z3foov | |
0000000000000012 T main | |
15:39 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ ld --shared -o mylib.so foo.o bar.o # if we link the two into a shared library... | |
15:39 [snoopjedi@denton ~/playground/cpp/linking_sandbox (master *)] | |
$ nm mylib.so # ...we see that both symbols are defined | |
0000000000003f00 d _DYNAMIC | |
0000000000004000 d _GLOBAL_OFFSET_TABLE_ | |
000000000000105f T _Z3barv | |
0000000000001050 T _Z3foov | |
0000000000001071 T main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment