Monday, January 18, 2021

LNK2019 unresolved external symbol "void __cdecl myCFunc(void)" (?myCFunc@@YAXXZ) referenced in function _main extern05

Error LNK2019 unresolved external symbol "void __cdecl myCFunc(void)" (?myCFunc@@YAXXZ) referenced in function _main extern05 C:\Demo_CPP\extern05\extern05.obj 1

when I tested extern keyword in C++, I got this message:

    high level explanations

  1. What is an undefined reference/unresolved external symbol error and how do I fix it?
  2. What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?.

    .....

    Compiling a C++ program takes place in several steps, as specified by 2.2 (credits to Keith Thompson for the reference):

    The specified errors occur during this last stage of compilation, most commonly referred to as linking. It basically means that you compiled a bunch of implementation files into object files or libraries and now you want to get them to work together.

    Say you defined symbol a in a.cpp. Now, b.cpp declared that symbol and used it. Before linking, it simply assumes that that symbol was defined somewhere, but it doesn't yet care where. The linking phase is responsible for finding the symbol and correctly linking it to b.cpp (well, actually to the object or library that uses it).

    If you're using Microsoft Visual Studio, you'll see that projects generate .lib files. These contain a table of exported symbols, and a table of imported symbols. The imported symbols are resolved against the libraries you link against, and the exported symbols are provided for the libraries that use that .lib (if any).

    Similar mechanisms exist for other compilers/ platforms.

    Common error messages are error LNK2001, error LNK1120, error LNK2019 for Microsoft Visual Studio and undefined reference to symbolName for GCC.

    Detailed Answers

  3. What is an undefined reference/unresolved external symbol error and how do I fix it?
  4. What are undefined reference/unresolved external symbol errors? What are common causes and how to fix/prevent them?.

  5. Unresolved external symbol “*” referenced in function _main
  6. You're including the header file once in a .cpp file, thus mangling the name as expected by standard C++ implementations, and then including the same header file in a .c file when actually defining your class, thus defining it with the name unmangled.

    Nope, it's a problem with your understanding of C and C++ both. extern "C" will declare the type name unmangled in a .cpp unit if that's what you wish.

  7. What is the effect of extern “C” in C++?
  8. What exactly does putting extern "C" into C++ code do?

    For example:
    extern "C" {
    void foo();
    }

    Good Answer is:

    extern "C" makes a function-name in C++ have C linkage (compiler does not mangle the name) so that client C code can link to (use) your function using a C compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client C linker will then link to using the C name.

    Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.

    Just so you know, you can specify extern "C" linkage to each individual declaration/definition explicitly or use a block to group a sequence of declarations/definitions to have a certain linkage:

    Just wanted to add a bit of info, since I haven't seen it posted yet.

    You'll very often see code in C headers like so:

    #ifdef __cplusplus
    extern "C" {
    #endif
    // all of your legacy C code here
    #ifdef __cplusplus
    }
    #endif

    What this accomplishes is that it allows you to use that C header file with your C++ code, because the macro "__cplusplus" will be defined. But you can also still use it with your legacy C code, where the macro is NOT defined, so it won't see the uniquely C++ construct.

    C++ mangles function names to create an object-oriented language from a procedural language

    Most programming languages aren't built on-top of existing programming languages. C++ is built on-top of C, and furthermore it's an object-oriented programming language built from a procedural programming language, and for that reason there are C++ expressions like extern "C" which provide backwards compatibility with C.

    extern "C" says "don't mangle C function names"

    However, imagine we have a legacy C file named "parent.c" that includes function names from other legacy C files, "parent.h", "child.h", etc. If the legacy "parent.c" file is run through a C++ compiler, then the function names will be mangled, and they will no longer match the function names specified in "parent.h", "child.h", etc - so the function names in those external files would also need to be mangled. Mangling function names across a complex C program, those with lots of dependencies, can lead to broken code; so it might be convenient to provide a keyword which can tell the C++ compiler not to mangle a function name.

    The extern "C" keyword tells a C++ compiler not to mangle (rename) C function names.

    For example:

    extern "C" void printMe(int a);

  9. How to trigger the __cplusplus (C++) #ifdef?
  10. This is a paragraph.

  11. What are the stages of compilation of a C++ program?
  12. This is a paragraph.

  13. Phases of translation
  14. C and C++ programs consist of one or more source files, each of which contains some of the text of the program. A source file, together with its include files, files that are included using the #include preprocessor directive, but not including sections of code removed by conditional-compilation directives such as #if, is called a translation unit.

    Source files can be translated at different times. In fact, it's common to translate only out-of-date files. The translated translation units can be processed into separate object files or object-code libraries. These separate, translated translation units are then linked to form an executable program or a dynamic-link library (DLL). For more information about files that can be used as input to the linker, see LINK input files..

  15. What are the stages of compilation of a C++ program?
  16. Are the stages of compilation of a C++ program specified by the standard?

    If so, what are they?

    If not, an answer for a widely-used compiler (I'd prefer MSVS) would be great.

    I'm talking about preprocessing, tokenization, parsing and such. What is the order in which they are executed and what do they do in particular?

  17. What are the stages of compilation of a C++ program?

No comments:

Post a Comment