Find the answer to your Linux question:
Results 1 to 3 of 3
Hi All, I am trying to add some function to the c++ project (calling function) and the called function are in C. And i am getting error "undefined reference to ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    12

    g++ linking problem: undefined reference to Function

    Hi All,

    I am trying to add some function to the c++ project (calling function) and the called function are in C. And i am getting error "undefined reference to "Hello(int,int)" " while linking. It is compling correctly. It is linking to header file mention in the calling function. This header file has definition to the c - called function.


    Do you thinking having c files into C++ project will be a problem ?
    Should I remove <stdio.h> in c files ?

    Please let me know your suggestions.

    Thanks
    fido

  2. #2
    Linux Newbie
    Join Date
    Apr 2007
    Posts
    119
    Calling c functions from a c++ program is no problem at all. Post the code and we might be able to spot what is wrong.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    In your C++ code, you need to indicate to the compiler that the external function uses C-linkage. That is the purpose of the compiler directive 'extern "C"'. Example:
    Code:
    // C++ source file, xyz.cpp
    extern "C" void cfunc(void);
    void xyz::somefunc(int)
    {
        // Do some stuff
    .
    .
    .
        cfunc(intval);
    }
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

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