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
- 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)
- g++ -o test objectFile1.o objectFile2.o -lLibraryName
- // 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
- 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()
- {
- }
- // 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
- 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)
- { /*...*/ }
- // ...
- };
- #define UNICODE
- #define _UNICODE
- /DUNICODE /D_UNICODE
- #ifdef WIN32PROJECT1_EXPORTS
- #define WIN32PROJECT1_API __declspec(dllexport)
- #else
- #define WIN32PROJECT1_API __declspec(dllimport)
- #endif
- // This class is exported from the Win32Project1.dll
- class WIN32PROJECT1_API CWin32Project1 {
- public:
- CWin32Project1(void);
- bool Foo();
- };
- extern WIN32PROJECT1_API int nWin32Project1;
- WIN32PROJECT1_API int fnWin32Project1(void);
- #include "stdafx.h"
- #include "Win32Project1.h"
- // This is an example of an exported variable
- WIN32PROJECT1_API int nWin32Project1=0;
- // This is an example of an exported function.
- WIN32PROJECT1_API int fnWin32Project1(void)
- {
- return 42;
- }
- // This is the constructor of a class that has been exported.
- // see Win32Project1.h for the class definition
- CWin32Project1::CWin32Project1()
- {
- return;
- }
- bool CWin32Project1::Foo() {return true;}
- #include "stdafx.h"
- #include "CppUnitTest.h"
- #include "..Win32Project1Win32Project1.h"
- using namespace Microsoft::VisualStudio::CppUnitTestFramework;
- namespace UnitTest1
- {
- TEST_CLASS(UnitTest1)
- {
- public:
- TEST_METHOD(TestMethod1)
- {
- auto bar = new CWin32Project1();
- Assert::AreEqual(true, bar->Foo());
- }
- };
- }
- 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)
- 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)"
- <AdditionalLibraryDirectories>$(SolutionDir)Debug;$(VCInstallDir)UnitTestlib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
- <AdditionalDependencies>Win32Project1.lib;%(AdditionalDependencies)</AdditionalDependencies>
Add Comment
Please, Sign In to add comment