c+ - Qu'est-ce référence/erreur de symbole externe non résolue et comment le corriger

Mots clés : c++linker-errorsundefined-referencec++-faqunresolved-externalvisual-studioc++

meilleur 5 Réponses c+ - Qu'est-ce référence/erreur de symbole externe non résolue et comment le corriger

vote vote

98

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 
vote vote

84

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 
vote vote

71

g++ -o test objectFile1.o objectFile2.o -lLibraryName 
vote vote

70

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)                            
vote vote

58

// 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 

Questions similaires