Thread: undefined reference to defined function

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    116

    undefined reference to defined function

    hello

    I'm facing a problem with my C++ program as it constantly returns the error

    undefined reference to function

    I saw that the function takes an argument const int
    I was sending just int,so I thought that was the problem.
    So I did the following but it still returns the same error.

    Code:
    int b;
    const int a = b;
    function(a);
    How can I convert b to const int in order to send it as an argument to the function?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That is probably not the problem.
    Where is your function prototype and definition located; w.r.t the line in which the error is shown ?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    That is probably not the problem.
    Where is your function prototype and definition located; w.r.t the line in which the error is shown ?
    The function prototype is in a .h file ,the definition is in a .o file (linux)
    I include the .h file in my .cpp file
    (.text+0x839): undefined reference to `close(int)'

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Oh, the solution is simple..
    You need to give the compiler both the .cpp files together. (" g++ foo.cpp main.cpp -o output")
    (... or compile them separately with the -c flag and invoke the linker manually. )

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Oh, the solution is simple..
    You need to give the compiler both the .cpp files together. (" g++ foo.cpp main.cpp -o output")
    (... or compile them separately with the -c flag and invoke the linker manually. )
    actually I tried that by doing: g++ main.cpp foo.o -o output
    but it's still the same...

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quo View Post
    actually I tried that by doing: g++ main.cpp foo.o -o output
    but it's still the same...
    Read my post again.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Read my post again.
    I have.There's no foo.cpp file...I only have the foo.o..
    I have compiled separately all my .cpp files and the linking is my problem,
    because I want the linker to see the foo.o and link it with the others to one ouput.
    When you say manually what do you mean?

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quo View Post
    When you say manually what do you mean?
    Then compile all the files separately, and give g++ only the object files.
    Or
    Type "man ld" in the terminal and read the instructions thoroughly.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Then compile all the files separately, and give g++ only the object files.
    Or
    Type "man ld" in the terminal and read the instructions thoroughly.
    I have all my .o files but when I use g++ -o main A.o B.o C.o
    returns the error of undefined reference
    I have never used ld before .
    Could you please give me an example?

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by quo View Post
    I have all my .o files but when I use g++ -o main A.o B.o C.o
    returns the error of undefined reference
    Change the order of the object files.
    I though the gcc can looks backwards, but that seems not to be.

    I have never used ld before .
    Could you please give me an example?
    That is what the man page is for.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    116
    Quote Originally Posted by manasij7479 View Post
    Change the order of the object files.
    I though the gcc can looks backwards, but that seems not to be.


    That is what the man page is for.
    I used ld and it returns the same ,even more undefined references ..I also changed the order of the.o but nothing..
    Since A.o contains the definitions of functions that every other .o needs and that's why they included all of them the same .h file
    is there a way to tell the linker to link against A.o as well?
    Maybe linker isn't doing this
    I don't know,seems so confusing

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    116
    maybe the problem I mentioned is the beginning is causing it?

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by quo View Post
    I used ld and it returns the same ,even more undefined references ..I also changed the order of the.o but nothing..
    Since A.o contains the definitions of functions that every other .o needs and that's why they included all of them the same .h file
    is there a way to tell the linker to link against A.o as well?
    Maybe linker isn't doing this
    I don't know,seems so confusing
    If A.o contains the object file needed by B.o and C.o, I think A.o must be after B.o and C.o.

    Example command of what I am suggesting this "-l" (lower case L)
    is for needed library.
    Code:
    g++ B.o C.o A.o -o main -lLibName
    Link Options - Using the GNU Compiler Collection (GCC)

    I think the link below is correct for ld command; I did not find the link I used last time.
    http://linux.die.net/man/1/ld

    From above I found start-group/end-group this might work for you below.
    Code:
    g++ -Wl,--start-group A.o B.o C.o -Wl,--end-group -o exename
    Tim S.
    Last edited by stahta01; 05-11-2012 at 05:44 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    116
    Actually in A.o are functions definitions used by every other file in my program.
    Their declaration was in A.h
    All of my files(.c and .cpp as well as .h) have #include "A.h"
    The undefined references I get are ALL about these functions (in A.o)

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by quo View Post
    All of my files(.c and .cpp as well as .h) have #include "A.h"
    Looks like you are mixing c and c++.
    A c compiler uses a different name for the same function then a c++ compiler and that might confuse the linker.

    What you exactly have to do depends on what A.o contains

    Looks like the header "A.h" needs needs to have conditions like

    Code:
    #ifdef __cplusplus
       extern "C" {
    #endif
    
    // the functions defined in a c module
    void a_c_function();  
    
    #ifdef __cplusplus
    }
    #endif
    This is the way to enable c++-code to call c function.

    if A.o contains c++ code then you have to conditionally exclude things like class definitions for the c compiler.


    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to (function)
    By django in forum C Programming
    Replies: 6
    Last Post: 09-04-2011, 12:06 PM
  2. Replies: 5
    Last Post: 03-07-2011, 01:01 AM
  3. undefined reference to a function
    By ChoCo in forum C Programming
    Replies: 1
    Last Post: 10-04-2009, 12:08 AM
  4. Undefined reference - however they ARE defined
    By scarecrow in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2008, 05:14 PM
  5. undefined reference to a function
    By rodrigouy in forum C Programming
    Replies: 7
    Last Post: 01-17-2006, 06:47 AM