CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    23

    Unhappy [SOLVED] unresolved external symbol "int __cdecl _.." between C and Cpp file

    Hi all, I am receiving a

    "error LNK2019: unresolved external symbol "int __cdecl _function(...)" error while trying to compile the following files:

    main.cpp
    --------------------------------------------
    #include "function.h"


    int main()
    {

    function(0, 0);

    return 0;
    }

    ---------------------------------------------


    function.h
    --------------------------------------------

    #ifndef FNC_H
    #define FNC_H

    extern void function(int a, int b);

    #endif
    ---------------------------------------------


    function.c
    --------------------------------------------

    #include "function.h"

    void function(int a, int b);
    {
    // do something here
    }
    ---------------------------------------------



    Seems like the program cannot find the function's definition. Why is that?
    Am I missing something with the association .h -> .c ?

    Please give me suggestions
    Last edited by leKoxn; April 20th, 2010 at 02:20 PM. Reason: problem solved

  2. #2
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: unresolved external symbol "int __cdecl _.." between C and Cpp file

    Remove the extern keyword from your function.h header file so that it becomes:

    Code:
    #ifndef FNC_H
    #define FNC_H
    
    void function(int a, int b);
    
    #endif

  3. #3
    Join Date
    Aug 2009
    Posts
    23

    Re: unresolved external symbol "int __cdecl _.." between C and Cpp file

    That does not solve my problem and I still get the same error.

    If I rename the file from .c to .cpp, I obtain something like 400 errors (obviously I made simple here the code for readability purpose) for syntax overloading stupid things..

    I don't know how to solve really

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: unresolved external symbol "int __cdecl _.." between C and Cpp file

    Sorry, I didn't notice that it was a main.cpp and not a main.c. The problem is that you basically need to put the function(a,b) declaration in your function.h file within an extern "C" statement, with the caveate that you will only want that extern "C" statement to be seen in the compilation with the main.cpp file, but not with the compilation of the function.c file.

    Thus the solution is to rewrite your function.h file as follows:

    Code:
    #ifndef FNC_H
    #define FNC_H
    
    #ifdef __cplusplus
    extern "C" 
    {
    #endif
    
    void function(int a, int b);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    Also, in the example you supplied you have a spurious ; at the end of your function signature in function.c.

  5. #5
    Join Date
    Aug 2009
    Posts
    23

    Re: unresolved external symbol "int __cdecl _.." between C and Cpp file

    You was right, thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured