Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct X
- {
- virtual void foo();
- };
- struct Y : X
- {
- void foo() {}
- };
- struct A
- {
- virtual ~A() = 0;
- };
- struct B: A
- {
- virtual ~B(){}
- };
- extern int x;
- void foo();
- int main()
- {
- x = 0;
- foo();
- Y y;
- B b;
- }
- /home/AbiSfw/ccvvuHoX.o: In function `main':
- prog.cpp:(.text+0x10): undefined reference to `x'
- prog.cpp:(.text+0x19): undefined reference to `foo()'
- prog.cpp:(.text+0x2d): undefined reference to `A::~A()'
- /home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
- prog.cpp:(.text._ZN1BD1Ev[B::~B()]+0xb): undefined reference to `A::~A()'
- /home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
- prog.cpp:(.text._ZN1BD0Ev[B::~B()]+0x12): undefined reference to `A::~A()'
- /home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1Y[typeinfo for Y]+0x8): undefined reference to `typeinfo for X'
- /home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1B[typeinfo for B]+0x8): undefined reference to `typeinfo for A'
- collect2: ld returned 1 exit status
- 1>test2.obj : error LNK2001: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ)
- 1>test2.obj : error LNK2001: unresolved external symbol "int x" (?x@@3HA)
- 1>test2.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall A::~A(void)" (??1A@@UAE@XZ)
- 1>test2.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall X::foo(void)" (?foo@X@@UAEXXZ)
- 1>...test2.exe : fatal error LNK1120: 4 unresolved externals
- struct X
- {
- virtual ~X() = 0;
- };
- struct Y : X
- {
- ~Y() {}
- };
- int main()
- {
- Y y;
- }
- //X::~X(){} //uncomment this line for successful definition
- struct X
- {
- virtual void foo();
- };
- struct Y : X
- {
- void foo() {}
- };
- int main()
- {
- Y y; //linker error although there was no call to X::foo
- }
- struct X
- {
- virtual void foo() = 0;
- };
- struct A
- {
- ~A();
- };
- A a; //destructor undefined
- struct A
- {
- ~A() {}
- };
- A::~A() {}
- struct A
- {
- void foo();
- };
- void foo() {}
- int main()
- {
- A a;
- a.foo();
- }
- void A::foo() {}
- struct X
- {
- static int x;
- };
- int main()
- {
- int x = X::x;
- }
- //int X::x; //uncomment this line to define X::x
- g++ -o test objectFile1.o objectFile2.o -lLibraryName
- extern int x;
- int x;
- extern int x;
- int main()
- {
- x = 0;
- }
- //int x; // uncomment this line for successful definition
- void foo(); // declaration only
- int main()
- {
- foo();
- }
- //void foo() {} //uncomment this line for successful definition
- void foo(int& x);
- int main()
- {
- int x;
- foo(x);
- }
- void foo(const int& x) {} //different function, doesn't provide a definition
- //for void foo(int& x)
- // B.h
- #ifndef B_H
- #define B_H
- struct B {
- B(int);
- int x;
- };
- #endif
- // B.cpp
- #include "B.h"
- B::B(int xx) : x(xx) {}
- // A.h
- #include "B.h"
- struct A {
- A(int x);
- B b;
- };
- // A.cpp
- #include "A.h"
- A::A(int x) : b(x) {}
- // main.cpp
- #include "A.h"
- int main() {
- A a(5);
- return 0;
- };
- $ g++ -c A.cpp
- $ g++ -c B.cpp
- $ ar rvs libA.a A.o
- ar: creating libA.a
- a - A.o
- $ ar rvs libB.a B.o
- ar: creating libB.a
- a - B.o
- $ g++ main.cpp -L. -lB -lA
- ./libA.a(A.o): In function `A::A(int)':
- A.cpp:(.text+0x1c): undefined reference to `B::B(int)'
- collect2: error: ld returned 1 exit status
- $ g++ main.cpp -L. -lA -lB
- $ ./a.out
- // src1.cpp
- void print();
- static int local_var_name; // 'static' makes variable not visible for other modules
- int global_var_name = 123;
- int main()
- {
- print();
- return 0;
- }
- // src2.cpp
- extern "C" int printf (const char*, ...);
- extern int global_var_name;
- //extern int local_var_name;
- void print ()
- {
- // printf("%d%dn", global_var_name, local_var_name);
- printf("%dn", global_var_name);
- }
- $ g++ -c src1.cpp -o src1.o
- $ g++ -c src2.cpp -o src2.o
- $ readelf --symbols src1.o
- Num: Value Size Type Bind Vis Ndx Name
- 5: 0000000000000000 4 OBJECT LOCAL DEFAULT 4 _ZL14local_var_name # [1]
- 9: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 global_var_name # [2]
- [1] - this is our static (local) variable (important - Bind has a type "LOCAL")
- [2] - this is our global variable
- $ g++ src1.o src2.o -o prog
- $ ./prog
- 123
- // src2.cpp
- extern "C" int printf (const char*, ...);
- extern int global_var_name;
- extern int local_var_name;
- void print ()
- {
- printf("%d%dn", global_var_name, local_var_name);
- }
- $ g++ -c src2.cpp -o src2.o
- $ g++ src1.o src2.o -o prog
- src2.o: In function `print()':
- src2.cpp:(.text+0x6): undefined reference to `local_var_name'
- collect2: error: ld returned 1 exit status
- $ g++ -S src1.cpp -o src1.s
- // src1.s
- look src1.s
- .file "src1.cpp"
- .local _ZL14local_var_name
- .comm _ZL14local_var_name,4,4
- .globl global_var_name
- .data
- .align 4
- .type global_var_name, @object
- .size global_var_name, 4
- global_var_name:
- .long 123
- .text
- .globl main
- .type main, @function
- main:
- ; assembler code, not interesting for us
- .LFE0:
- .size main, .-main
- .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
- .section .note.GNU-stack,"",@progbits
- .local _ZL14local_var_name
- .comm _ZL14local_var_name,4,4
- .globl local_var_name
- .data
- .align 4
- .type local_var_name, @object
- .size local_var_name, 4
- local_var_name:
- .long 456789
- .file "src1.cpp"
- .globl local_var_name
- .data
- .align 4
- .type local_var_name, @object
- .size local_var_name, 4
- local_var_name:
- .long 456789
- .globl global_var_name
- .align 4
- .type global_var_name, @object
- .size global_var_name, 4
- global_var_name:
- .long 123
- .text
- .globl main
- .type main, @function
- main:
- ; ...
- $ g++ -c src1.s -o src2.o
- $ readelf --symbols src1.o
- 8: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 local_var_name
- $ g++ src1.o src2.o -o prog
- $ ./prog
- 123456789
- void foo();
- int main()
- {
- foo();
- }
- extern "C" void foo();
- int main()
- {
- foo();
- }
- extern "C" void foo();
- extern "C" {
- #include "cheader.h"
- }
- #ifdef THIS_MODULE
- #define DLLIMPEXP __declspec(dllexport)
- #else
- #define DLLIMPEXP __declspec(dllimport)
- #endif
- DLLIMPEXP void foo();
- __declspec(dllexport) void foo();
- __declspec(dllimport) void foo();
- class DLLIMPEXP X
- {
- };
- template<class T>
- struct X
- {
- void foo();
- };
- int main()
- {
- X<int> x;
- x.foo();
- }
- //differentImplementationFile.cpp
- template<class T>
- void X<T>::foo()
- {
- }
- int foo()
- {
- return 0;
- }
- void foo();
- void bar()
- {
- foo();
- }
- #pragma comment(lib, "libname.lib")
- INPUT (libtbb.so.2)
- cp libtbb.so.2 libtbb.so
- template <typename T>
- class Foo {
- friend std::ostream& operator<< (std::ostream& os, const Foo<T>& a);
- };
- std::ostream& operator<< (std::ostream& os, const Foo<int>& a) {/*...*/}
- // forward declare the Foo
- template <typename>
- class Foo;
- // forward declare the operator <<
- template <typename T>
- std::ostream& operator<<(std::ostream&, const Foo<T>&);
- template <typename T>
- class Foo {
- friend std::ostream& operator<< <>(std::ostream& os, const Foo<T>& a);
- // note the required <> ^^^^
- // ...
- };
- template <typename T>
- std::ostream& operator<<(std::ostream&, const Foo<T>&)
- {
- // ... implement the operator
- }
- template <typename T>
- class Foo {
- template <typename T1>
- friend std::ostream& operator<<(std::ostream& os, const Foo<T1>& a);
- // ...
- };
- template <typename T>
- class Foo {
- friend std::ostream& operator<<(std::ostream& os, const Foo& a)
- { /*...*/ }
- // ...
- };
- #include "my_lib.h"
- #include <stdio.h>
- void hw(void)
- {
- puts("Hello World");
- }
- #ifndef MY_LIB_H
- #define MT_LIB_H
- extern void hw(void);
- #endif
- #include <my_lib.h>
- int main()
- {
- hw();
- return 0;
- }
- $ gcc -c -o my_lib.o my_lib.c
- $ ar rcs libmy_lib.a my_lib.o
- $ gcc -I. -c -o eg1.o eg1.c
- $ gcc -o eg1 -L. -lmy_lib eg1.o
- eg1.o: In function `main':
- eg1.c:(.text+0x5): undefined reference to `hw'
- collect2: error: ld returned 1 exit status
- $ gcc -o eg1 -I. -L. -lmy_lib eg1.c
- /tmp/ccQk1tvs.o: In function `main':
- eg1.c:(.text+0x5): undefined reference to `hw'
- collect2: error: ld returned 1 exit status
- #include <zlib.h>
- #include <stdio.h>
- int main()
- {
- printf("%sn",zlibVersion());
- return 0;
- }
- $ gcc -c -o eg2.o eg2.c
- $ gcc -o eg2 -lz eg2.o
- eg2.o: In function `main':
- eg2.c:(.text+0x5): undefined reference to `zlibVersion'
- collect2: error: ld returned 1 exit status
- $ gcc -o eg2 -I. -lz eg2.c
- /tmp/ccxCiGn7.o: In function `main':
- eg2.c:(.text+0x5): undefined reference to `zlibVersion'
- collect2: error: ld returned 1 exit status
- $ gcc -o eg2 $(pkg-config --libs zlib) eg2.o
- eg2.o: In function `main':
- eg2.c:(.text+0x5): undefined reference to `zlibVersion'
- $ gcc -o eg1 eg1.o -L. -lmy_lib
- $ ./eg1
- Hello World
- $ gcc -o eg2 eg2.o -lz
- $ ./eg2
- 1.2.8
- $ gcc -o eg2 eg2.o $(pkg-config --libs zlib)
- $ ./eg2
- 1.2.8
- $ gcc -o eg1 -L. -lmy_lib eg1.o
- gcc -o eg2 -lz eg2.o
- gcc -o eg2 $(pkg-config --libs zlib) eg2.o
- gcc -o eg2 -lz eg2.o
- gcc -o eg2 -lz eg2.o
- $ gcc -o eg1 -I. -L. -lmy_lib eg1.c
- $ gcc -I. -c -o eg1.o eg1.c
- $ gcc -o eg1 -L. -lmy_lib eg1.o
- /tmp/ccQk1tvs.o: In function `main'
- eg1.o: In function `main':
- #define UNICODE
- #define _UNICODE
- /DUNICODE /D_UNICODE
- // header1.h
- typedef int Number;
- void foo(Number);
- // header2.h
- typedef float Number;
- void foo(Number); // this only looks the same lexically
- // graphics.lib
- #include "common_math.h"
- void draw(vec3 p) { ... } // vec3 comes from common_math.h
- // main.exe
- #include "other/common_math.h"
- #include "graphics.h"
- int main() {
- draw(...);
- }
- // file1.cpp
- const int test = 5; // in C++ same as "static const int test = 5"
- int test2 = 5;
- // file2.cpp
- extern const int test;
- extern int test2;
- void foo()
- {
- int x = test; // linker error in C++ , no error in C
- int y = test2; // no problem
- }
- extern const int test;
- extern int test2;
- #if (defined _GLIBCXX_EXPERIMENTAL_FILESYSTEM) //is the included filesystem library experimental? (C++14 and newer: <experimental/filesystem>)
- using path_t = std::experimental::filesystem::path;
- #elif (defined _GLIBCXX_FILESYSTEM) //not experimental (C++17 and newer: <filesystem>)
- using path_t = std::filesystem::path;
- #endif
- # -D shows (global) dynamic symbols that can be used from the outside of XXX.so
- nm -D XXX.so | grep MY_SYMBOL
- nm XXX.so
- 00000000000005a7 t HIDDEN_SYMBOL
- 00000000000005f8 T VISIBLE_SYMBOL
- #define DLL_PUBLIC __attribute__ ((visibility ("default")))
- DLL_PUBLIC int my_public_function(){
- ...
- }
- #ifdef BUILDING_DLL
- #define DLL_PUBLIC __declspec(dllexport)
- #else
- #define DLL_PUBLIC __declspec(dllimport)
- #endif
- >>> objdump -t XXXX.o | grep hidden
- 0000000000000000 g F .text 000000000000000b .hidden HIDDEN_SYMBOL1
- 000000000000000b g F .text 000000000000000b .hidden HIDDEN_SYMBOL2
- library machine type 'x64' conflicts with target machine type 'X86'
Add Comment
Please, Sign In to add comment