Conclusion: porting solves undefined reference to errors and unresovled errors.

Source: Internet
Author: User

By sjyuhusun

 

In view of the detours and explorations I have made here, and for the purpose of reducing the detours, I will summarize the General Manager Lin of porting for your reference. I hope my predecessors will give you more advice on the shortcomings.
For example, porting C code to Symbian: due to the limitations of the functions of the original C program, I used the Wizard to generate an EXE program in vc6.
1. Put all. c files of the original C program under the/src/directory, and put all. H files under the/INC/directory.
2. Edit the MMP file. First, add/epoc32/include/libc to systeminclude, that is, include the C library. Next, add the C files in Souce. Make sure to add all the C files used. Many errors, such as the undefined reference to error, will be reported if the C files are missing. last added: Library estlib. lib // This Library supports Standard C and must be added
Staticlibrary ecrt0.lib // This is required by the EXE program. It provides the e32main () entry for the EXE program.
3. Import. C and. H files under vc6.
4. Modify the. h file of the original C program and add it in the file:
# Ifdef _ cplusplus
Extern "C "{
# Endif
Add at the end:
# Ifdef _ cplusplus
}
# Endif
The general structure is as follows:
# Ifdef _ cplusplus
Extern "C "{
# Endif
/* ...... Mycode ..........*/
/* Here we mainly use extern to declare some functions or variables */
# Ifdef _ cplusplus
}
# Endif
5. Add extern "C" {} to the CPP file of exe "{}
For example, extern "C"
{
/* Include the header file referenced in the C ++ file.
For example: # include <stdlib. h>
# Include <e32def. h>
# Include <string. h>
# Include <stdio. h>
Or add the declaration of the function in C used, and use extern declaration, such as extern void F1 ()
The extern can be compiled here, but the original C library file must be linked during the link, otherwise an error will occur:
Test. O (. Test + 0x1f): Test. cpp: Undefined reference to error
*/
}

To call the C library file in the C ++ code, you must add extern "c" to inform the compiler that this is a library file written in C. Use C to connect them.
Undefined reference to error: This type of error occurs during the connection process. There may be two reasons: first, the source code file where the user-defined function or global variable is located, the program has not been compiled, connected, or is not defined yet. You need to modify the source program based on the actual situation and give the definition body of the global variable or function; second, the undefined symbol is a standard library function. It is used in the source program, and the name of the corresponding library is not given during the connection, or the directory name of the file library is incorrect.

Generally, arm generation is more difficult than wins generation, and GCC usually generates additional Compiler Errors and warnings during the first attempt, because GCC is generally stricter than Microsoft compiler.

Q & A: (refer to Bruce's article here) the standard header file has a structure similar to the following:
# Ifndef _ test_h
# DEFINE _ test_h
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*...*/
# Ifdef _ cplusplus
}
# Endif
# Endif/* _ test_h */
Analysis: the compilation macro "# ifndef _ test_h, # DEFINE _ test_h, # endif" in the header file is used to prevent the header file from being repeatedly referenced.
Extern "C" contains a double meaning, which can be obtained literally: first, the target to be modified is "extern;
Secondly, the target to be modified is "C. Let's explain these two meanings in detail.
The function or variable specified by extern "C" is of the extern type;
Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. This keyword tells the compiler, the declared functions and variables can be used in this module or other modules. Remember, the following statements:
Extern int;
It is just a declaration of a variable. It is not defining variable A and does not allocate memory space for variable A (note: in fact, the general compiler will declare and process the above statements, however, if the connector does not find the definition of the variable during the link process, it will be automatically defined at the place where the variable declaration is first encountered ). Variable A can only be defined once as a global variable in all modules. Otherwise, a connection error occurs.
In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. For example, if Module B wants to reference the global variables and functions defined in module A, it only needs to include the header file of module. In this way, when Module B calls a function in module A, although Module B cannot find the function in the compilation phase, no error is reported; it will find this function from the target Code Compiled by module A in the connection phase.
The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module. Therefore, a function or variable can only be used by this module and cannot be modified by extern "C.
Variables and functions modified by extern "C" are compiled and connected in the C language;
The compilation method without the extern "C" declaration first looks at how C-like functions are compiled in C ++.
As an object-oriented language, C ++ supports function overloading, while Procedural Language C does not. The name of a function in the symbol library after being compiled by C ++ is different from that in C. For example, assume that the prototype of a function is:
Void Foo (int x, int y );
After the function is compiled by the C compiler, its name in the symbol library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int (different compilers may generate different names, however, they all adopt the same mechanism,
The new name is called "mangled name ").
A name such as _ foo_int_int contains the function name, number of function parameters, and type information. c ++ relies on this mechanism.
To implement function overloading. For example, in C ++, the void Foo (int x, int y) and void Foo (int x, float y) functions generate different symbols, the latter is _ foo_int_float. Similarly, variables in C ++ support both local variables and class member variables and global variables. The class member variables of the program written by the user may have the same name as the global variables, which are distinguished. In essence, when the compiler is compiling
The processing of numbers is similar, and a unique name is also used for the variables in the class. This name is different from the global variable name with the same name in the user program.
Connection method when extern "C" is not added
Suppose in C ++, the header file of module A is as follows:
// Module A header file modulea. h
# Ifndef module_a_h
# Define module_a_h
Int Foo (int x, int y );
# Endif
Reference this function in Module B:
// Module B implements the file moduleb. cpp
# I nclude "modulea. H"
Foo (2, 3 );
In fact, in the connection phase, the connector looks for symbols such as _ foo_int_int from the target file modulea. OBJ generated by module!
Compilation and Connection Methods After the extern "C" clause is added
After the extern "C" statement is added, the header file of module A is changed:
// Module A header file modulea. h
# Ifndef module_a_h
# Define module_a_h
Extern "C" int Foo (int x, int y );
# Endif
In Module B's implementation file, Foo (2, 3) is still called. The result is:
(1) When module A compiles the foo target code, it does not perform special processing on its name and uses the C language;
(2) When the connector looks for the Foo (2, 3) call for the target code of Module B, it looks for the unmodified symbol name _ Foo.
If the function in module A declares that foo is of the extern "C" type, and Module B contains the extern int Foo (INT X, int y ), module B cannot find the function in module A, and vice versa.
Therefore, we can summarize the true purpose of the statement "extern" C "in one sentence (the birth of any syntax feature in any language is not random and comes from the needs of the real world. When thinking about a problem, we should not just focus on how the language is made, but also ask why it is doing so and what the motivation is, so that we can better understand many problems ):
Realize mixed programming of C ++, C and other languages.
Understand the motivation for setting up extern "C" in C ++. Next we will analyze the common usage skills of extern "C.
Usage of extern "C"
(1) reference the functions and variables in C language in C ++ and include the header file of C language (for example, cexample. h)
The following operations are required:
Extern "C"
{
# I nclude "cexample. H"
}
In the header file of the C language, the external function can only be specified as the extern type, and the extern type is not supported in the C language.
"C" Declaration, when the. c file contains extern "C", a compilation syntax error occurs.
The source code of the three files in the example project of C ++ referenced by the author is as follows:
/* C header file: cexample. H */
# Ifndef c_example_h
# Define c_example_h
Extern int add (int x, int y );
# Endif
/* C implementation file: cexample. C */
# I nclude "cexample. H"
Int add (int x, int y)
{
Return X + Y;
}
// C ++ implementation file, call Add: cppfile. cpp
Extern "C"
{
# I nclude "cexample. H"
}
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}
If C ++ calls a. dll file written in C language, when it includes a. dll header file or declares an interface function
Extern "C "{}.
(2) When referencing functions and variables in C ++ in C, the header file of C ++ needs to add extern "C",
The header file that declares extern "C" cannot be directly referenced in the language. Only the extern defined in C ++ should be referenced
The "C" function is declared as the extern type.
The source code of the three files contained in the example project of C ++ is as follows:
// C ++ header file cppexample. h
# Ifndef cpp_example_h
# Define cpp_example_h
Extern "C" int add (int x, int y );
# Endif
// C ++ implementation file cppexample. cpp
# I nclude "cppexample. H"
Int add (int x, int y)
{
Return X + Y;
}
/* C implementation file cfile. c
/* Compilation errors: # I nclude "cexample. H "*/
Extern int add (int x, int y );
Int main (INT argc, char * argv [])
{
Add (2, 3 );
Return 0;
}

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.