Guest User

Untitled

a guest
Jan 19th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.72 KB | None | 0 0
  1. struct X
  2. {
  3. virtual void foo();
  4. };
  5. struct Y : X
  6. {
  7. void foo() {}
  8. };
  9. struct A
  10. {
  11. virtual ~A() = 0;
  12. };
  13. struct B: A
  14. {
  15. virtual ~B(){}
  16. };
  17. extern int x;
  18. void foo();
  19. int main()
  20. {
  21. x = 0;
  22. foo();
  23. Y y;
  24. B b;
  25. }
  26.  
  27. /home/AbiSfw/ccvvuHoX.o: In function `main':
  28. prog.cpp:(.text+0x10): undefined reference to `x'
  29. prog.cpp:(.text+0x19): undefined reference to `foo()'
  30. prog.cpp:(.text+0x2d): undefined reference to `A::~A()'
  31. /home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
  32. prog.cpp:(.text._ZN1BD1Ev[B::~B()]+0xb): undefined reference to `A::~A()'
  33. /home/AbiSfw/ccvvuHoX.o: In function `B::~B()':
  34. prog.cpp:(.text._ZN1BD0Ev[B::~B()]+0x12): undefined reference to `A::~A()'
  35. /home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1Y[typeinfo for Y]+0x8): undefined reference to `typeinfo for X'
  36. /home/AbiSfw/ccvvuHoX.o:(.rodata._ZTI1B[typeinfo for B]+0x8): undefined reference to `typeinfo for A'
  37. collect2: ld returned 1 exit status
  38.  
  39. 1>test2.obj : error LNK2001: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ)
  40. 1>test2.obj : error LNK2001: unresolved external symbol "int x" (?x@@3HA)
  41. 1>test2.obj : error LNK2001: unresolved external symbol "public: virtual __thiscall A::~A(void)" (??1A@@UAE@XZ)
  42. 1>test2.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall X::foo(void)" (?foo@X@@UAEXXZ)
  43. 1>...test2.exe : fatal error LNK1120: 4 unresolved externals
  44.  
  45. struct X
  46. {
  47. virtual ~X() = 0;
  48. };
  49. struct Y : X
  50. {
  51. ~Y() {}
  52. };
  53. int main()
  54. {
  55. Y y;
  56. }
  57. //X::~X(){} //uncomment this line for successful definition
  58.  
  59. struct X
  60. {
  61. virtual void foo();
  62. };
  63. struct Y : X
  64. {
  65. void foo() {}
  66. };
  67. int main()
  68. {
  69. Y y; //linker error although there was no call to X::foo
  70. }
  71.  
  72. struct X
  73. {
  74. virtual void foo() = 0;
  75. };
  76.  
  77. struct A
  78. {
  79. ~A();
  80. };
  81.  
  82. A a; //destructor undefined
  83.  
  84. struct A
  85. {
  86. ~A() {}
  87. };
  88.  
  89. A::~A() {}
  90.  
  91. struct A
  92. {
  93. void foo();
  94. };
  95.  
  96. void foo() {}
  97.  
  98. int main()
  99. {
  100. A a;
  101. a.foo();
  102. }
  103.  
  104. void A::foo() {}
  105.  
  106. struct X
  107. {
  108. static int x;
  109. };
  110. int main()
  111. {
  112. int x = X::x;
  113. }
  114. //int X::x; //uncomment this line to define X::x
  115.  
  116. g++ -o test objectFile1.o objectFile2.o -lLibraryName
  117.  
  118. extern int x;
  119.  
  120. int x;
  121.  
  122. extern int x;
  123. int main()
  124. {
  125. x = 0;
  126. }
  127. //int x; // uncomment this line for successful definition
  128.  
  129. void foo(); // declaration only
  130. int main()
  131. {
  132. foo();
  133. }
  134. //void foo() {} //uncomment this line for successful definition
  135.  
  136. void foo(int& x);
  137. int main()
  138. {
  139. int x;
  140. foo(x);
  141. }
  142. void foo(const int& x) {} //different function, doesn't provide a definition
  143. //for void foo(int& x)
  144.  
  145. // B.h
  146. #ifndef B_H
  147. #define B_H
  148.  
  149. struct B {
  150. B(int);
  151. int x;
  152. };
  153.  
  154. #endif
  155.  
  156. // B.cpp
  157. #include "B.h"
  158. B::B(int xx) : x(xx) {}
  159.  
  160. // A.h
  161. #include "B.h"
  162.  
  163. struct A {
  164. A(int x);
  165. B b;
  166. };
  167.  
  168. // A.cpp
  169. #include "A.h"
  170.  
  171. A::A(int x) : b(x) {}
  172.  
  173. // main.cpp
  174. #include "A.h"
  175.  
  176. int main() {
  177. A a(5);
  178. return 0;
  179. };
  180.  
  181. $ g++ -c A.cpp
  182. $ g++ -c B.cpp
  183. $ ar rvs libA.a A.o
  184. ar: creating libA.a
  185. a - A.o
  186. $ ar rvs libB.a B.o
  187. ar: creating libB.a
  188. a - B.o
  189.  
  190. $ g++ main.cpp -L. -lB -lA
  191. ./libA.a(A.o): In function `A::A(int)':
  192. A.cpp:(.text+0x1c): undefined reference to `B::B(int)'
  193. collect2: error: ld returned 1 exit status
  194. $ g++ main.cpp -L. -lA -lB
  195. $ ./a.out
  196.  
  197. // src1.cpp
  198. void print();
  199.  
  200. static int local_var_name; // 'static' makes variable not visible for other modules
  201. int global_var_name = 123;
  202.  
  203. int main()
  204. {
  205. print();
  206. return 0;
  207. }
  208.  
  209. // src2.cpp
  210. extern "C" int printf (const char*, ...);
  211.  
  212. extern int global_var_name;
  213. //extern int local_var_name;
  214.  
  215. void print ()
  216. {
  217. // printf("%d%dn", global_var_name, local_var_name);
  218. printf("%dn", global_var_name);
  219. }
  220.  
  221. $ g++ -c src1.cpp -o src1.o
  222. $ g++ -c src2.cpp -o src2.o
  223.  
  224. $ readelf --symbols src1.o
  225. Num: Value Size Type Bind Vis Ndx Name
  226. 5: 0000000000000000 4 OBJECT LOCAL DEFAULT 4 _ZL14local_var_name # [1]
  227. 9: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 global_var_name # [2]
  228.  
  229. [1] - this is our static (local) variable (important - Bind has a type "LOCAL")
  230. [2] - this is our global variable
  231.  
  232. $ g++ src1.o src2.o -o prog
  233.  
  234. $ ./prog
  235. 123
  236.  
  237. // src2.cpp
  238. extern "C" int printf (const char*, ...);
  239.  
  240. extern int global_var_name;
  241. extern int local_var_name;
  242.  
  243. void print ()
  244. {
  245. printf("%d%dn", global_var_name, local_var_name);
  246. }
  247.  
  248. $ g++ -c src2.cpp -o src2.o
  249.  
  250. $ g++ src1.o src2.o -o prog
  251. src2.o: In function `print()':
  252. src2.cpp:(.text+0x6): undefined reference to `local_var_name'
  253. collect2: error: ld returned 1 exit status
  254.  
  255. $ g++ -S src1.cpp -o src1.s
  256.  
  257. // src1.s
  258. look src1.s
  259.  
  260. .file "src1.cpp"
  261. .local _ZL14local_var_name
  262. .comm _ZL14local_var_name,4,4
  263. .globl global_var_name
  264. .data
  265. .align 4
  266. .type global_var_name, @object
  267. .size global_var_name, 4
  268. global_var_name:
  269. .long 123
  270. .text
  271. .globl main
  272. .type main, @function
  273. main:
  274. ; assembler code, not interesting for us
  275. .LFE0:
  276. .size main, .-main
  277. .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
  278. .section .note.GNU-stack,"",@progbits
  279.  
  280. .local _ZL14local_var_name
  281. .comm _ZL14local_var_name,4,4
  282.  
  283. .globl local_var_name
  284. .data
  285. .align 4
  286. .type local_var_name, @object
  287. .size local_var_name, 4
  288. local_var_name:
  289. .long 456789
  290.  
  291. .file "src1.cpp"
  292. .globl local_var_name
  293. .data
  294. .align 4
  295. .type local_var_name, @object
  296. .size local_var_name, 4
  297. local_var_name:
  298. .long 456789
  299. .globl global_var_name
  300. .align 4
  301. .type global_var_name, @object
  302. .size global_var_name, 4
  303. global_var_name:
  304. .long 123
  305. .text
  306. .globl main
  307. .type main, @function
  308. main:
  309. ; ...
  310.  
  311. $ g++ -c src1.s -o src2.o
  312.  
  313. $ readelf --symbols src1.o
  314. 8: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 local_var_name
  315.  
  316. $ g++ src1.o src2.o -o prog
  317.  
  318. $ ./prog
  319. 123456789
  320.  
  321. void foo();
  322. int main()
  323. {
  324. foo();
  325. }
  326.  
  327. extern "C" void foo();
  328. int main()
  329. {
  330. foo();
  331. }
  332.  
  333. extern "C" void foo();
  334.  
  335. extern "C" {
  336. #include "cheader.h"
  337. }
  338.  
  339. #ifdef THIS_MODULE
  340. #define DLLIMPEXP __declspec(dllexport)
  341. #else
  342. #define DLLIMPEXP __declspec(dllimport)
  343. #endif
  344.  
  345. DLLIMPEXP void foo();
  346.  
  347. __declspec(dllexport) void foo();
  348.  
  349. __declspec(dllimport) void foo();
  350.  
  351. class DLLIMPEXP X
  352. {
  353. };
  354.  
  355. template<class T>
  356. struct X
  357. {
  358. void foo();
  359. };
  360.  
  361. int main()
  362. {
  363. X<int> x;
  364. x.foo();
  365. }
  366.  
  367. //differentImplementationFile.cpp
  368. template<class T>
  369. void X<T>::foo()
  370. {
  371. }
  372.  
  373. int foo()
  374. {
  375. return 0;
  376. }
  377.  
  378. void foo();
  379.  
  380. void bar()
  381. {
  382. foo();
  383. }
  384.  
  385. #pragma comment(lib, "libname.lib")
  386.  
  387. INPUT (libtbb.so.2)
  388.  
  389. cp libtbb.so.2 libtbb.so
  390.  
  391. template <typename T>
  392. class Foo {
  393. friend std::ostream& operator<< (std::ostream& os, const Foo<T>& a);
  394. };
  395.  
  396. std::ostream& operator<< (std::ostream& os, const Foo<int>& a) {/*...*/}
  397.  
  398. // forward declare the Foo
  399. template <typename>
  400. class Foo;
  401.  
  402. // forward declare the operator <<
  403. template <typename T>
  404. std::ostream& operator<<(std::ostream&, const Foo<T>&);
  405.  
  406. template <typename T>
  407. class Foo {
  408. friend std::ostream& operator<< <>(std::ostream& os, const Foo<T>& a);
  409. // note the required <> ^^^^
  410. // ...
  411. };
  412.  
  413. template <typename T>
  414. std::ostream& operator<<(std::ostream&, const Foo<T>&)
  415. {
  416. // ... implement the operator
  417. }
  418.  
  419. template <typename T>
  420. class Foo {
  421. template <typename T1>
  422. friend std::ostream& operator<<(std::ostream& os, const Foo<T1>& a);
  423. // ...
  424. };
  425.  
  426. template <typename T>
  427. class Foo {
  428. friend std::ostream& operator<<(std::ostream& os, const Foo& a)
  429. { /*...*/ }
  430. // ...
  431. };
  432.  
  433. #include "my_lib.h"
  434. #include <stdio.h>
  435.  
  436. void hw(void)
  437. {
  438. puts("Hello World");
  439. }
  440.  
  441. #ifndef MY_LIB_H
  442. #define MT_LIB_H
  443.  
  444. extern void hw(void);
  445.  
  446. #endif
  447.  
  448. #include <my_lib.h>
  449.  
  450. int main()
  451. {
  452. hw();
  453. return 0;
  454. }
  455.  
  456. $ gcc -c -o my_lib.o my_lib.c
  457. $ ar rcs libmy_lib.a my_lib.o
  458.  
  459. $ gcc -I. -c -o eg1.o eg1.c
  460.  
  461. $ gcc -o eg1 -L. -lmy_lib eg1.o
  462. eg1.o: In function `main':
  463. eg1.c:(.text+0x5): undefined reference to `hw'
  464. collect2: error: ld returned 1 exit status
  465.  
  466. $ gcc -o eg1 -I. -L. -lmy_lib eg1.c
  467. /tmp/ccQk1tvs.o: In function `main':
  468. eg1.c:(.text+0x5): undefined reference to `hw'
  469. collect2: error: ld returned 1 exit status
  470.  
  471. #include <zlib.h>
  472. #include <stdio.h>
  473.  
  474. int main()
  475. {
  476. printf("%sn",zlibVersion());
  477. return 0;
  478. }
  479.  
  480. $ gcc -c -o eg2.o eg2.c
  481.  
  482. $ gcc -o eg2 -lz eg2.o
  483. eg2.o: In function `main':
  484. eg2.c:(.text+0x5): undefined reference to `zlibVersion'
  485. collect2: error: ld returned 1 exit status
  486.  
  487. $ gcc -o eg2 -I. -lz eg2.c
  488. /tmp/ccxCiGn7.o: In function `main':
  489. eg2.c:(.text+0x5): undefined reference to `zlibVersion'
  490. collect2: error: ld returned 1 exit status
  491.  
  492. $ gcc -o eg2 $(pkg-config --libs zlib) eg2.o
  493. eg2.o: In function `main':
  494. eg2.c:(.text+0x5): undefined reference to `zlibVersion'
  495.  
  496. $ gcc -o eg1 eg1.o -L. -lmy_lib
  497.  
  498. $ ./eg1
  499. Hello World
  500.  
  501. $ gcc -o eg2 eg2.o -lz
  502.  
  503. $ ./eg2
  504. 1.2.8
  505.  
  506. $ gcc -o eg2 eg2.o $(pkg-config --libs zlib)
  507. $ ./eg2
  508. 1.2.8
  509.  
  510. $ gcc -o eg1 -L. -lmy_lib eg1.o
  511.  
  512. gcc -o eg2 -lz eg2.o
  513.  
  514. gcc -o eg2 $(pkg-config --libs zlib) eg2.o
  515.  
  516. gcc -o eg2 -lz eg2.o
  517.  
  518. gcc -o eg2 -lz eg2.o
  519.  
  520. $ gcc -o eg1 -I. -L. -lmy_lib eg1.c
  521.  
  522. $ gcc -I. -c -o eg1.o eg1.c
  523. $ gcc -o eg1 -L. -lmy_lib eg1.o
  524.  
  525. /tmp/ccQk1tvs.o: In function `main'
  526.  
  527. eg1.o: In function `main':
  528.  
  529. #define UNICODE
  530. #define _UNICODE
  531.  
  532. /DUNICODE /D_UNICODE
  533.  
  534. // header1.h
  535. typedef int Number;
  536. void foo(Number);
  537.  
  538. // header2.h
  539. typedef float Number;
  540. void foo(Number); // this only looks the same lexically
  541.  
  542. // graphics.lib
  543. #include "common_math.h"
  544.  
  545. void draw(vec3 p) { ... } // vec3 comes from common_math.h
  546.  
  547. // main.exe
  548. #include "other/common_math.h"
  549. #include "graphics.h"
  550.  
  551. int main() {
  552. draw(...);
  553. }
  554.  
  555. // file1.cpp
  556. const int test = 5; // in C++ same as "static const int test = 5"
  557. int test2 = 5;
  558.  
  559. // file2.cpp
  560. extern const int test;
  561. extern int test2;
  562.  
  563. void foo()
  564. {
  565. int x = test; // linker error in C++ , no error in C
  566. int y = test2; // no problem
  567. }
  568.  
  569. extern const int test;
  570. extern int test2;
  571.  
  572. #if (defined _GLIBCXX_EXPERIMENTAL_FILESYSTEM) //is the included filesystem library experimental? (C++14 and newer: <experimental/filesystem>)
  573. using path_t = std::experimental::filesystem::path;
  574. #elif (defined _GLIBCXX_FILESYSTEM) //not experimental (C++17 and newer: <filesystem>)
  575. using path_t = std::filesystem::path;
  576. #endif
  577.  
  578. # -D shows (global) dynamic symbols that can be used from the outside of XXX.so
  579. nm -D XXX.so | grep MY_SYMBOL
  580.  
  581. nm XXX.so
  582. 00000000000005a7 t HIDDEN_SYMBOL
  583. 00000000000005f8 T VISIBLE_SYMBOL
  584.  
  585. #define DLL_PUBLIC __attribute__ ((visibility ("default")))
  586.  
  587. DLL_PUBLIC int my_public_function(){
  588. ...
  589. }
  590.  
  591. #ifdef BUILDING_DLL
  592. #define DLL_PUBLIC __declspec(dllexport)
  593. #else
  594. #define DLL_PUBLIC __declspec(dllimport)
  595. #endif
  596.  
  597. >>> objdump -t XXXX.o | grep hidden
  598. 0000000000000000 g F .text 000000000000000b .hidden HIDDEN_SYMBOL1
  599. 000000000000000b g F .text 000000000000000b .hidden HIDDEN_SYMBOL2
  600.  
  601. library machine type 'x64' conflicts with target machine type 'X86'
Add Comment
Please, Sign In to add comment