Guest User

Untitled

a guest
Jul 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.30 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. extern int x;
  117.  
  118. int x;
  119.  
  120. extern int x;
  121. int main()
  122. {
  123. x = 0;
  124. }
  125. //int x; // uncomment this line for successful definition
  126.  
  127. void foo(); // declaration only
  128. int main()
  129. {
  130. foo();
  131. }
  132. //void foo() {} //uncomment this line for successful definition
  133.  
  134. void foo(int& x);
  135. int main()
  136. {
  137. int x;
  138. foo(x);
  139. }
  140. void foo(const int& x) {} //different function, doesn't provide a definition
  141. //for void foo(int& x)
  142.  
  143. g++ -o test objectFile1.o objectFile2.o -lLibraryName
  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. void foo();
  198. int main()
  199. {
  200. foo();
  201. }
  202.  
  203. extern "C" void foo();
  204. int main()
  205. {
  206. foo();
  207. }
  208.  
  209. extern "C" void foo();
  210.  
  211. extern "C" {
  212. #include "cheader.h"
  213. }
  214.  
  215. #ifdef THIS_MODULE
  216. #define DLLIMPEXP __declspec(dllexport)
  217. #else
  218. #define DLLIMPEXP __declspec(dllimport)
  219. #endif
  220.  
  221. DLLIMPEXP void foo();
  222.  
  223. __declspec(dllexport) void foo();
  224.  
  225. __declspec(dllimport) void foo();
  226.  
  227. class DLLIMPEXP X
  228. {
  229. };
  230.  
  231. template<class T>
  232. struct X
  233. {
  234. void foo();
  235. };
  236.  
  237. int main()
  238. {
  239. X<int> x;
  240. x.foo();
  241. }
  242.  
  243. //differentImplementationFile.cpp
  244. template<class T>
  245. void X<T>::foo()
  246. {
  247. }
  248.  
  249. // src1.cpp
  250. void print();
  251.  
  252. static int local_var_name; // 'static' makes variable not visible for other modules
  253. int global_var_name = 123;
  254.  
  255. int main()
  256. {
  257. print();
  258. return 0;
  259. }
  260.  
  261. // src2.cpp
  262. extern "C" int printf (const char*, ...);
  263.  
  264. extern int global_var_name;
  265. //extern int local_var_name;
  266.  
  267. void print ()
  268. {
  269. // printf("%d%dn", global_var_name, local_var_name);
  270. printf("%dn", global_var_name);
  271. }
  272.  
  273. $ g++ -c src1.cpp -o src1.o
  274. $ g++ -c src2.cpp -o src2.o
  275.  
  276. $ readelf --symbols src1.o
  277. Num: Value Size Type Bind Vis Ndx Name
  278. 5: 0000000000000000 4 OBJECT LOCAL DEFAULT 4 _ZL14local_var_name # [1]
  279. 9: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 global_var_name # [2]
  280.  
  281. [1] - this is our static (local) variable (important - Bind has a type "LOCAL")
  282. [2] - this is our global variable
  283.  
  284. $ g++ src1.o src2.o -o prog
  285.  
  286. $ ./prog
  287. 123
  288.  
  289. // src2.cpp
  290. extern "C" int printf (const char*, ...);
  291.  
  292. extern int global_var_name;
  293. extern int local_var_name;
  294.  
  295. void print ()
  296. {
  297. printf("%d%dn", global_var_name, local_var_name);
  298. }
  299.  
  300. $ g++ -c src2.cpp -o src2.o
  301.  
  302. $ g++ src1.o src2.o -o prog
  303. src2.o: In function `print()':
  304. src2.cpp:(.text+0x6): undefined reference to `local_var_name'
  305. collect2: error: ld returned 1 exit status
  306.  
  307. $ g++ -S src1.cpp -o src1.s
  308.  
  309. // src1.s
  310. look src1.s
  311.  
  312. .file "src1.cpp"
  313. .local _ZL14local_var_name
  314. .comm _ZL14local_var_name,4,4
  315. .globl global_var_name
  316. .data
  317. .align 4
  318. .type global_var_name, @object
  319. .size global_var_name, 4
  320. global_var_name:
  321. .long 123
  322. .text
  323. .globl main
  324. .type main, @function
  325. main:
  326. ; assembler code, not interesting for us
  327. .LFE0:
  328. .size main, .-main
  329. .ident "GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2"
  330. .section .note.GNU-stack,"",@progbits
  331.  
  332. .local _ZL14local_var_name
  333. .comm _ZL14local_var_name,4,4
  334.  
  335. .globl local_var_name
  336. .data
  337. .align 4
  338. .type local_var_name, @object
  339. .size local_var_name, 4
  340. local_var_name:
  341. .long 456789
  342.  
  343. .file "src1.cpp"
  344. .globl local_var_name
  345. .data
  346. .align 4
  347. .type local_var_name, @object
  348. .size local_var_name, 4
  349. local_var_name:
  350. .long 456789
  351. .globl global_var_name
  352. .align 4
  353. .type global_var_name, @object
  354. .size global_var_name, 4
  355. global_var_name:
  356. .long 123
  357. .text
  358. .globl main
  359. .type main, @function
  360. main:
  361. ; ...
  362.  
  363. $ g++ -c src1.s -o src2.o
  364.  
  365. $ readelf --symbols src1.o
  366. 8: 0000000000000000 4 OBJECT GLOBAL DEFAULT 3 local_var_name
  367.  
  368. $ g++ src1.o src2.o -o prog
  369.  
  370. $ ./prog
  371. 123456789
  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. #define UNICODE
  392. #define _UNICODE
  393.  
  394. /DUNICODE /D_UNICODE
  395.  
  396. template <typename T>
  397. class Foo {
  398. friend std::ostream& operator<< (std::ostream& os, const Foo<T>& a);
  399. };
  400.  
  401. std::ostream& operator<< (std::ostream& os, const Foo<int>& a) {/*...*/}
  402.  
  403. // forward declare the Foo
  404. template <typename>
  405. class Foo;
  406.  
  407. // forward declare the operator <<
  408. template <typename T>
  409. std::ostream& operator<<(std::ostream&, const Foo<T>&);
  410.  
  411. template <typename T>
  412. class Foo {
  413. friend std::ostream& operator<< <>(std::ostream& os, const Foo<T>& a);
  414. // note the required <> ^^^^
  415. // ...
  416. };
  417.  
  418. template <typename T>
  419. std::ostream& operator<<(std::ostream&, const Foo<T>&)
  420. {
  421. // ... implement the operator
  422. }
  423.  
  424. template <typename T>
  425. class Foo {
  426. template <typename T1>
  427. friend std::ostream& operator<<(std::ostream& os, const Foo<T1>& a);
  428. // ...
  429. };
  430.  
  431. template <typename T>
  432. class Foo {
  433. friend std::ostream& operator<<(std::ostream& os, const Foo& a)
  434. { /*...*/ }
  435. // ...
  436. };
  437.  
  438. #ifdef WIN32PROJECT1_EXPORTS
  439. #define WIN32PROJECT1_API __declspec(dllexport)
  440. #else
  441. #define WIN32PROJECT1_API __declspec(dllimport)
  442. #endif
  443.  
  444. // This class is exported from the Win32Project1.dll
  445. class WIN32PROJECT1_API CWin32Project1 {
  446. public:
  447. CWin32Project1(void);
  448.  
  449. bool Foo();
  450. };
  451.  
  452. extern WIN32PROJECT1_API int nWin32Project1;
  453.  
  454. WIN32PROJECT1_API int fnWin32Project1(void);
  455.  
  456. #include "stdafx.h"
  457. #include "Win32Project1.h"
  458.  
  459.  
  460. // This is an example of an exported variable
  461. WIN32PROJECT1_API int nWin32Project1=0;
  462.  
  463. // This is an example of an exported function.
  464. WIN32PROJECT1_API int fnWin32Project1(void)
  465. {
  466. return 42;
  467. }
  468.  
  469. // This is the constructor of a class that has been exported.
  470. // see Win32Project1.h for the class definition
  471. CWin32Project1::CWin32Project1()
  472. {
  473. return;
  474. }
  475.  
  476. bool CWin32Project1::Foo() {return true;}
  477.  
  478. #include "stdafx.h"
  479. #include "CppUnitTest.h"
  480. #include "..Win32Project1Win32Project1.h"
  481.  
  482. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  483.  
  484. namespace UnitTest1
  485. {
  486. TEST_CLASS(UnitTest1)
  487. {
  488. public:
  489.  
  490. TEST_METHOD(TestMethod1)
  491. {
  492. auto bar = new CWin32Project1();
  493. Assert::AreEqual(true, bar->Foo());
  494. }
  495.  
  496. };
  497. }
  498.  
  499. 1>unittest1.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall CWin32Project1::CWin32Project1(void)" ... referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)" (?TestMethod1@UnitTest1@1@QAEXXZ)
  500. 1>unittest1.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall CWin32Project1::Foo(void)" ... referenced in function "public: void __thiscall UnitTest1::UnitTest1::TestMethod1(void)"
  501.  
  502. <AdditionalLibraryDirectories>$(SolutionDir)Debug;$(VCInstallDir)UnitTestlib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
  503. <AdditionalDependencies>Win32Project1.lib;%(AdditionalDependencies)</AdditionalDependencies>
Add Comment
Please, Sign In to add comment