Like Tree2Likes

undefined reference to defined function

This is a discussion on undefined reference to defined function within the C++ Programming forums, part of the General Programming Boards category; hello I'm facing a problem with my C++ program as it constantly returns the error undefined reference to function I ...

  1. #1
    quo
    quo is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    quo
    quo is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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. )
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    quo
    quo is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    quo
    quo is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    quo
    quo is offline
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    quo
    quo is offline
    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
    quo
    quo is offline
    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
    2,549
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    quo
    quo is offline
    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
    ZuK
    ZuK is offline
    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
    stahta01 and quo like this.

Page 1 of 2 12 LastLast
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, 12: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, 05:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21