Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:14+00:00 2026-05-24T00:25:14+00:00

I am following an intro tutorial http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf . I have created the source files

  • 0

I am following an intro tutorial http://www.linuxinsight.com/files/alp/alp-ch01-getting-started.pdf.

I have created the source files main.c, reciprocal.cpp and reciprocal.hpp. I have been able to successfully compile these files. The problem comes when I go to link them and I receive the following error message:

main.o: In function `main':
main.c:(.text+0x30): undefined reference to `reciprocal'
collect2: ld returned 1 exit status

I gather that this is somehow not using the header files correctly, however I do not really know what to try as I’ve followed the steps quite thoroughly.

If anyone has an idea why its throwing that error I would appreciate it very much.

Thanks

**UPDATE
Here is the code for the three source files:

main.c

#include <stdio.h>
#include "reciprocal.hpp"

int main (int argc, char **argv) {
    int i;
    i = atoi(argv[1]);
    printf("The reciprocal of %d is %g\n", i, reciprocal(i));

    return 0;
}

reciprocal.cpp

#include <cassert>
#include "reciprocal.hpp"

double reciprocal(int i) {
    assert(i != 0);
    return 1.0/i;
}

reciprocal.hpp

#ifdef __cplusplus
extern "C" {
#endif

double reciprocal(int i);

#ifdef __cplusplus
}
#endif

I have actually modified reciprocal.hpp since originally posting. The only change I’ve made was to remove the extern keyword from the function signature. It previously read

extern double reciprocal(int i);

This change allowed me to link the program and I can now run it. I think what may have been occurring is that the second extern was overriding the first one. If anyone has any insight on this, I would be interested in knowing.

Thanks for your assistance everyone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:25:15+00:00Added an answer on May 24, 2026 at 12:25 am

    When you mix C and C++ code together, you can run into trouble because the two languages have different linkages. That is, if you were to crack open the object files for a compiled C and C++ file, you would find that the names inside of those object files were different.

    In particular, C++ compilers tend to use a technique called “name mangling” in which function names are mixed with extra information about the types of their arguments. For example, a function

    char foo(int);
    

    might actually have the internal name

    char@foo@int
    

    inside of the generated object file. The problem is that C code does not do this, so if you compiled a C file and tried to make a reference to the name foo, the linker wouldn’t find it, because the name of the function in the generated C++ file is char@foo@int instead of foo.

    To fix this, C++ has a feature that allows you to explicitly tell the linker not to mangle the name and to make the generated code look like it was intended for a C program. To do this, you can declare C++ functions like this:

    extern "C" char foo(int);
    

    Now, the generated object file will contain the name foo without adornment in a way that is compatible with what the C code expects.

    To fix your problem, try adding one of these extern "C" declarations to your C++ source file that defines reciprocal.

    A note: if you have a project that mixes C and C++ code, you should always have the main function compiled as C++ code. C++ introduces extra initialization and cleanup code into its executables that are not present in C code. If you compile main as C code, this extra logic might not get added into the program, and so you may get inexplicable crashes at runtime.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following this tutorial: http://www.csharp-station.com/Tutorials/Lesson01.aspx I pasted this into a text file, named
I was following this tutorial: http://www.marcofolio.net/webdesign/a_fancy_apple.com-style_search_suggestion.html And checking out the demo here: http://qpoit.com/marcofolio_demo/apple_search/ I
i have a grails application and i'm following the tutorial here: http://www.grails.org/Services i have
I am using following a tutorial from here: http://www.shopdev.co.uk/blog/cookies-with-jquery-designing-collapsible-layouts/ This is the script I
I am currently reading on Web Services. There is a SOAP tutorial at http://www.w3schools.com/soap/soap_intro.asp
I have read some tutorials for bezier curve such as this one http://www.codeproject.com/KB/recipes/BezirCurves.aspx .
When you paste the following URL into IE: http://technet.microsoft.com/en-us/sysinternals/bb897434.aspx , the link on the
I've been following the polls tutorial up until the point where I should have
Scientific or market research questionnaires mainly consist of questions in the following form: INTRO:
I currently running into trouble with spring security, I have been following the two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.