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

  • SEARCH
  • Home
  • 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 764965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:48:49+00:00 2026-05-14T16:48:49+00:00

I have two C++ files, say file1.cpp and file2.cpp as //file1.cpp #include<cstdio> void fun(int

  • 0

I have two C++ files, say file1.cpp and file2.cpp as

//file1.cpp  
#include<cstdio>  
void fun(int i)  
{  
   printf("%d\n",i);  
}

//file2.cpp
void fun(double);
int main()
{
   fun(5);
}  

When I compile them and link them as c++ files, I get an error “undefined reference to fun(double)”.
But when I do this as C files, I don’t get error and 0 is printed instead of 5.
Please explain the reason.
Moreover I want to ask whether we need to declare a function before defining it because
I haven’t declared it in file1.cpp but no error comes in compilation.

  • 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-14T16:48:50+00:00Added an answer on May 14, 2026 at 4:48 pm

    This is most likely because of function overloading. When compiling with C, the call to fun(double) is translated into a call to the assembly function _fun, which will be linked in at a later stage. The actual definition also has the assembly name _fun, even though it takes an int instead of a double, and the linker will merrily use this when fun(double) is called.

    C++ on the other hand mangles the assembly names, so you’ll get something like _fun@int for fun(int) and _fun@double for fun(double), in order for overloading to work. The linker will see these have different names and spurt out an error that it can’t find the definition for fun(double).

    For your second question it is always a good idea to declare function prototypes, generally done in a header, especially if the function is used in multiple files. There should be a warning option for missing prototypes in your compiler, gcc uses -Wmissing-prototypes. Your code would be better if set up like

    // file1.hpp
    #ifndef FILE1_HPP
    #define FILE1_HPP
    void fun(int)
    #endif
    
    // file1.c
    #include "file1.hpp"
    ...
    
    // file2.c
    #include "file1.hpp"
    int main()
    {
        fun(5);
    }
    

    You’d then not have multiple conflicting prototypes in your program.

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

Sidebar

Related Questions

Static variable has file scope. Say I have two following files: file1.h file1.cpp file2.h
I have two source files created in unix folder say file1.dat and file2.dat. I
say I have two files: file1 and file2 is there a single command that
Say that I have two CSV files (file1 and file2) with contents as shown
I have two files say file1.php and file2.php. I have one php file named
Lets say I have files: Libs: one.cpp, one.h two.cpp, two.h three.cpp, three.h Program: program.cpp
Say I have two different cpp files. Both declare classes with the same name,
Say I have two .cpp files: oranges.cpp and basket.cpp . They have the classes
Lets say I have 3 files: add.h, add.cpp and main.cpp. This is a common
Say I have two .cpp files and in one of them I wrote extern

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.