unresolved external symbol, c (not c++)

VooDooChicken 40 Reputation points
2023-01-17T02:34:13.1866667+00:00

hi, I am trying to convert some code from c++ to pure c, or at least most of it

When I had things in c++ I could make a .h file, then a .cpp file and simply type the implementation of functions declared in the .h in the .cpp; I did not have to edit linker files or anything like that, and the ide would keep track of it all, and I had no linker problems even if a function was not a member of a class

Now I have a .h, but when I try to use a function defined in the .c, which is declared in the .h, I get a LNK2019 unresolved external symbol "void __cdecl LC_iniciar_trace(char const *)" (?LC_iniciar_trace@@YAXPBD@Z) referenced in function _wWinMain@16 LC 1 C:\Users\xxxxx\Documents\Proyectos\plataforma multimedia c puro v1\Visual Studio\LC 1\LC 1.obj

Why is in not able to see the function implemented in a .c file in the same project? If I make a syntax error in the function the compiler would give an error, so is not as if the compiler does not see the .c file or anything like that

tnx

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,636 questions
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2023-01-17T04:23:20.09+00:00

    Maybe the name of the function in .c file is different.

    Or, if you are trying to use the function in .cpp files, then try this form of #include (in .cpp files only):

    extern "C"
    {
       #include "MyHeaderFile.h"
    }
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. VooDooChicken 40 Reputation points
    2023-01-17T12:12:37.4833333+00:00

    Hi, thank you both. By just changing the extension from .c to .cpp the errors stopped, but I willl use the extern c suggestion, thanks

    0 comments No comments

  2. WayneAKing 4,921 Reputation points
    2023-01-18T10:15:25.5866667+00:00

    In case you haven't discovered why you needed the changes to eliminate the link error(s), the following references should help clarify the issue.

    Name Decoration

    https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/name-decoration?view=msvc-170

    "Use extern "C" to call a C function from C++. extern "C" forces use

    of the C naming convention for non-class C++ functions. Be aware of

    compiler switches /Tc or /Tp, which tell the compiler to ignore the

    filename extension and compile the file as C or C++, respectively.

    These options may cause linker names you don't expect."

    Decorated names

    https://learn.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170

    "Functions, data, and objects in C and C++ programs are represented

    internally by their decorated names. A decorated name is an encoded

    string created by the compiler during compilation of an object, data,

    or function definition. It records calling conventions, types, function

    parameters and other information together with the name. This name

    decoration, also known as name mangling, helps the linker find the

    correct functions and objects when linking an executable."

    extern (C++)

    https://learn.microsoft.com/en-us/cpp/cpp/extern-cpp?view=msvc-170

    "extern "C" specifies that the function is defined elsewhere and uses

    the C-language calling convention. The extern "C" modifier may also

    be applied to multiple function declarations in a block."

    Advanced detailed explanation:

    Visual C++ name mangling

    https://en.wikiversity.org/wiki/Visual_C%2B%2B_name_mangling

    • Wayne
    0 comments No comments