Thread: Linking: undefined reference from same library

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    4

    Linking: undefined reference from same library

    Hi,

    I'm migrating a C-project to C++. In C everything worked fine, and in moving to C++ I've reviewed all the code and corrected it where necessary (ie. more strict types required for pointers).

    I'm at the point now where everything compiles and I'm having linking issues. For example, the message as I see it.

    <path>/src/bin/CentOS_6.8_4.4.7_x64/lib/libcore.a(vsa_lib.o): In function 'vsa_type':
    <path>/src/core/vsa_lib.c:56: undefined reference to 'inst_get'
    <path>/src/core/vsa_lib.c:58: undefined reference to 'inst_get'

    Before now I'd say I thought I understood the rules of linking, library order and what is invovled - what's really confusing me is that the function 'inst_get()' is defined in an object called 'inst.o' which is ALSO in the library libcore.a.

    Incidicently some of the other executables in this project that do not include libcore.a do link properly.

    I'm sorry the code is not something I can post in entirety (nor would anyone care to read it all) .. and I am unable to re-create a trival test case with the same problem.

    Specifics:
    OS: Cent OS 6.8
    Compiler: Gcc/G++ 4.47
    Reason for change: I need to re-use the functionality within a C++ project involving Qt dialogs.

    What I've already looked it:
    * Changing library order (no effect)
    * Looking for possible name collisions (none)

    nm -a libcore.a | c++filt <showing reduced output below>

    inst.o:
    0000000000015dcc T inst_get(char const *)
    0000000000001a4e r inst_get(char const *)::__func__

    vsa_lib.o:
    U inst_get

    Any help would be appreciated.
    Thanks,
    Brian

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I noticed vsa_lib.c rather than vsa_lib.cpp. Are you combining C and C++ code (rather than compiling C code as C++) in the same project?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    4
    Yes, you are correct. It is using a C++ compiler to compile family of .c/.h files -- eventually to combine them with C++ code, but for now just trying to recompile with a different compiler.
    Dare I ask a question that is probably obvious to others ... If I'm not supposed to do this, is there a more recommended way to reusing code from a C project in a C++ project - aside from copy/paste and maintaining two 'branches'.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    4
    Granted ... most of which I've found already - but are you including this to say "beware ... proceed with caution" or "danger .. do not enter"?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That sounds like you are compiling C code as C++. To be sure, I'd suggest changing those .c files to .cpp.

    If you were combining C and C++ code in the same project, then you would need to worry about appropriate use of extern "C" to avoid ABI problems that could manifest as link errors.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    4
    Hi,
    Thank you - your last post/question pointed me in the direction of 'issues compiling c with a c++ compiler' and I found all sort of information about 'extern C'. I've just tried and it works. Thanks for the guidance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. undefined reference to std list during linking
    By dayalsoap in forum C++ Programming
    Replies: 22
    Last Post: 06-15-2011, 08:41 PM
  2. Linking error: undefined reference
    By somedude in forum C++ Programming
    Replies: 16
    Last Post: 05-21-2009, 03:55 PM
  3. undefined Reference when linking a library
    By steve1_rm in forum C Programming
    Replies: 7
    Last Post: 03-12-2008, 05:34 PM
  4. undefined reference error while linking..help
    By rapture in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2007, 04:54 AM
  5. Linking problem - undefined reference to load_parameters
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:28 AM

Tags for this Thread