C + + calls library functions undefined reference to__jquery

Source: Internet
Author: User
C + + calls library functions, if the header file is not properly defined, there may be clearly a function in the obj file exists, but the link failed, the following error occurred:

Undefined reference to ' xxx '

The problem occurs because the C library function is compiled into an obj file and differs from C + + for the processing of the function symbols. Because C + + functions support overloading, the processing of function symbols is more complex, and c is often not decorated.

For example, there are functions:

* DOFUNC.C * *

#include <stdio.h>
int Dofunc ()
{
printf ("dofunc\n");
}

After using GCC to compile into obj
Gcc-c DOFUNC.C
#生成 DOFUNC.O

Objdump-x DOFUNC.O

[0] (sec-2) (fl 0x00) (Ty 0) (SCL 103) (NX 1) 0x00000000 DOFUNC.C
File
[2] (sec 1) (fl 0x00) (Ty 20) (SCL 2) (NX 1) 0x00000000 _dofunc
AUX tagndx 0 ttlsiz 0x0 Lnnos 0 Next 0
[4] (sec 1) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. Text
AUX Scnlen 0x14 Nreloc 2 Nlnno 0
[6] (sec 2) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. Data
AUX Scnlen 0x0 Nreloc 0 Nlnno 0
[8] (sec 3) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. BSS
AUX Scnlen 0x0 Nreloc 0 Nlnno 0
[10] (sec 4) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. rdata
AUX Scnlen 0x8 nreloc 0 Nlnno 0
[12] (sec 0) (fl 0x00) (Ty 20) (SCL 2) (NX 0) 0x00000000 _printf



The Dofunc function of C is _dofunc in obj file

Then look at the compiled code using g++:

g++-C DOFUNC.C

Objdump-x DOFUNC.O

SYMBOL TABLE:
[0] (sec-2) (fl 0x00) (Ty 0) (SCL 103) (NX 1) 0x00000000 DOFUNC.C
File
[2] (sec 1) (fl 0x00) (Ty 20) (SCL 2) (NX 1) 0x00000000 __Z6DOFUNCV
AUX tagndx 0 ttlsiz 0x0 Lnnos 0 Next 0
[4] (sec 1) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. Text
AUX Scnlen 0x14 Nreloc 2 Nlnno 0
[6] (sec 2) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. Data
AUX Scnlen 0x0 Nreloc 0 Nlnno 0
[8] (sec 3) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. BSS
AUX Scnlen 0x0 Nreloc 0 Nlnno 0
[10] (sec 4) (fl 0x00) (Ty 0) (SCL 3) (NX 1) 0x00000000. rdata
AUX Scnlen 0x8 nreloc 0 Nlnno 0
[12] (sec 0) (fl 0x00) (Ty 20) (SCL 2) (NX 0) 0x00000000 _printf
g++ the compiled function symbol name is more bizarre: __Z6DOFUNCV

Visible C and C + + in the processing function name is very different.

If there is a C + + program to use DOFUNC.O, the following program's function declaration is wrong

Main_dev.cpp

int Dofunc ();

int main (int argc, char* args[])
{
Dofunc ();
System ("pause");
}

g++-o main_dev main_dev.cpp DOFUNC.O
main_dev.cpp:undefined reference to ' Dofunc () '
Collect2:ld returned 1 exit status

The reason is that the function name of the Dofunc function should be __Z6DOFUNCV, DOFUNC.O file inside is _dofunc, so can't find.

If there is Dofunc source code, the solution is very simple, will dofunc.c use C + + to compile.
If unfortunately dofunc function in someone else's library, and this library is written in C and GCC compiled, the source code is not visible, then how to do it.
Thanks to the designers of C + + and compilers already anticipating this problem, it provides a common solution: use extern "C" to modify the external function declaration of the old C library.

extern "C" {
int Dofunc ();
}

int main (int<
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.