/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
// src1.cppvoid print();
static int local_var_name; // 'static' makes variable not visible for other modulesint global_var_name = 123;
int main(){print();return 0;}
和
// src2.cppextern "C" int printf (const char*, ...);
extern int global_var_name;//extern int local_var_name;
void print (){// printf("%d%d\n", global_var_name, local_var_name);printf("%d\n", global_var_name);}
$ readelf --symbols src1.oNum: Value Size Type Bind Vis Ndx Name5: 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
src2.cpp什么也没有出口,我们也没有看到它的符号
链接我们的目标文件
$ g++ src1.o src2.o -o prog
并运行它
$ ./prog123
链接器看到导出的符号并链接它。现在我们尝试取消注释行在src2.cpp像这样
// src2.cppextern "C" int printf (const char*, ...);
extern int global_var_name;extern int local_var_name;
void print (){printf("%d%d\n", global_var_name, local_var_name);}
并重建一个目标文件
$ g++ -c src2.cpp -o src2.o
OK(没有错误),因为我们只构建对象文件,链接还没有完成。点击链接
$ g++ src1.o src2.o -o progsrc2.o: In function `print()':src2.cpp:(.text+0x6): undefined reference to `local_var_name'collect2: error: ld returned 1 exit status
我通过找到packagename\build\native\packagename.targets并在该文件中复制所有v110部分来编辑包(在解决方案目录内的packages文件夹中)。我将v110更改为条件字段只中的v120,非常小心地将文件名路径全部保留为v110。这只是允许Visual Studio 2013链接到2012年的库,在这种情况下,它起作用了。
Note, when the declaration of the operator (or function) only appears in the class, the name is not available for "normal" lookup, only for argument dependent lookup, from cppreference;
A name first declared in a friend declaration within class or class template X becomes a member of the innermost enclosing namespace of X, but is not accessible for lookup (except argument-dependent lookup that considers X) unless a matching declaration at the namespace scope is provided...
There is further reading on template friends at cppreference and the C++ FAQ.
Project Properties > General > Project Defaults > Character Set
Or on the command line;
/DUNICODE /D_UNICODE
The alternative is applicable as well, if UNICODE is not intended to be used, make sure the defines are not set, and/or the multi-character setting is used in the projects and consistently applied.
Do not forget to be consistent between the "Release" and "Debug" builds as well.
$ 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
您尝试将其与libmy_lib.a链接并失败:
$ gcc -o eg1 -L. -lmy_lib eg1.oeg1.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
一个涉及共享系统库的最小示例,压缩库libz
eg2. c
#include <zlib.h>#include <stdio.h>
int main(){printf("%s\n",zlibVersion());return 0;}
编译您的程序:
$ gcc -c -o eg2.o eg2.c
尝试将您的程序与libz链接并失败:
$ gcc -o eg2 -lz eg2.oeg2.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
示例2的变体涉及pkg-config:
$ gcc -o eg2 $(pkg-config --libs zlib) eg2.oeg2.o: In function `main':eg2.c:(.text+0x5): undefined reference to `zlibVersion'
// file1.cppconst int test = 5; // in C++ same as "static const int test = 5"int test2 = 5;
// file2.cppextern const int test;extern int test2;
void foo(){int x = test; // linker error in C++ , no error in Cint y = test2; // no problem}
>>> objdump -t XXXX.o | grep hidden0000000000000000 g F .text 000000000000000b .hidden HIDDEN_SYMBOL1000000000000000b g F .text 000000000000000b .hidden HIDDEN_SYMBOL2
$ g++ -o prog main.o foo.o gum.omain.o: In function `main':main.cpp:(.text+0x18): undefined reference to `gum()'main.cpp:(.text+0x24): undefined reference to `foo::bar() const'collect2: error: ld returned 1 exit status
//class A header fileclass ClassB; // FORWARD DECLERATIONclass ClassA{public:ClassB* bObj;ClassA(HINSTANCE hDLL) ;// member functions}--------------------------------//class A cpp fileClassA::ClassA(HINSTANCE hDLL){bObj = new ClassB();// LNK2019 occures herebObj ->somefunction();// LNK2019 occures here}/*************************/
//classB Header filestruct mystruct{}class ClassB{public:ClassB();mystruct somefunction();}
------------------------------//classB cpp file/* This is the file with the wrong property item type in visual studio --C/C++ Header-*/ClassB::somefunction(){}ClassB::ClassB(){}
$ gcc -o test test.cpp/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccPv7MvI.o: warning: relocation against `_ZSt4cout' in read-only section `.text'/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccPv7MvI.o: in function `main': test.cpp:(.text+0xe): undefined reference to `std::cout'/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: test.cpp:(.text+0x13): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccPv7MvI.o: in function `__static_initialization_and_destruction_0(int, int)':test.cpp:(.text+0x43): undefined reference to `std::ios_base::Init::Init()'/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: test.cpp:(.text+0x58): undefined reference to `std::ios_base::Init::~Init()'/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIEcollect2: error: ld returned 1 exit status