We help IT Professionals succeed at work.

specifying which function to call...

dartslca
dartslca asked
on
I have a function in my code which has the same name as a function in another header file I am including.  I want to call the other function within my function.  Anyone know how to do this?  I believe in C++ you would do this with the :: operator, but this needs to be programmed in C.

Thanks,

Darts
Comment
Watch Question

Sorry to say, but you can not have 2 functions with the same name in a project unless one of the functions is static (limited to the source file it is in).

I suggest renaming your function.

Tam
CERTIFIED EXPERT

Commented:
you will have to rename as C does not support method/function overloading.

Paul

Author

Commented:
is there a way to write/compile a function in c++ or another language and then include it in a C program??  The original program has to be compiled in C, but my function can be written in anything, as long as it can be read by the C program and it can include a header file that has the function i am trying to add code to.
you can compile the .C program using your c compiler

then in the header file (you do use one dont you? ;) you simply use:

extern "C" void myfunction(void);

with the return type and arguments the way you want it. The "C" part tells the C++ compiler that it should NOT mangle those names (C doesnt mangle). Now the C++ compiler can find the functions.

Tam
erm, the object file generated from the c compiler needs of course to be linked with the C++ program :)

Author

Commented:
so are you saying that I can supposedly, write my function in c++, which supports function overloading, and specify which function to use when I call it in my code(my function or a function found in a header file) and then in the original C program, include my function in a header file, compile my function in a c++ compiler, and compile the original program with a C compiler with the .o file from the c++ compiler linked in.  or does it have to be the other way around, compile it in a c++ compiler and link in the C main program(?).  Not sure if this will work cause the main program is written has to be compiled in C, can it?

Thanks,
Darts
You will need to compile the .c and .cpp files with their own compiler, then you need to link it with the linker that came with the c++ compiler. If you use a compiler that differentiates between .c and .cpp files and supports porjects just stuff them all in the project and the environment will do the rest.

You can then still not call the c++ functions with the same name as a function in your .C file, but you can call the function from the .C file from your c++ source (and it can overload it internally).

This is for backwards compatibility to 'old' C programs. It is not to interface new .C programs with .c++ programs.

In fact, due to the name mangling your .C source would not even KNOW the c++ functions.

Tam

Author

Commented:
Tam,
So that doesn't seem like it would work with what I have.  How about this...
I add a static function to the main C program and have that function call a function with a different name that is called thru a header and then that function can have the extra code needed and then call the original function(which then there will only be one, because the other one is static, so it won't be seen).  Really messy, but it seems like it would work.  do you see any flaws in this?

thanks,
Darts

Author

Commented:
i.e.
include "header.h"  //my function that calls the real func
include <realheader.h> //real function

static samenamefunc();

main()
{
     samenamefunc();
/*it would call the samenamefunc() in this file instead of the samenamefunc in realheader.h because of the compiling rules, right? */
}

static samenamefunc(){
     newsamenamefunc();
/* this func calls another function that is in header.h and inside this function, samenamefunc() is called and this would call the the samenamefunc() in realheader.h and not the one in the main program file. right? */
}

This would work right?
Darts
consider this:

CPP_header:

extern "C" cpp_myfunction();
extern myfunction();


CPP_file:

myfunction()
{
/* do stuff */
}

cpp_myfunction()
{
return myfunction();
}


C_file:
#include "CPP_header"

int main()
{
cpp_myfunction(); /* this will succeed */
myfunction(); /* this will get a link error, myfunction not found */
}


You must remember, that the header files are used in the language the .C or .CPP file is compiled in. Thus a name in the header is name-mangled in C++, but not in C. Thus you have the same problem again with the names not being right.

I think you should just rename one of the functions, it rally is easiest and the only way to get it to work properly.

Tam

Author

Commented:
I got it to work...
this is what I did:
MyStuff.h:
int myFunction();

MyStuff.c:
#include <RealStuff.h>
int myFunction(){
/* do stuff I need it to do */
  realFunction();//calls the real function from RealStuff.h
}

MainProg.c:
#include "MyStuff.h"
#include <RealStuff.h>

static int realFunction();

main(){
/* the program stuff I can't touch with a
bunch of realFunction() calls */
}
static int realFunction(){
  myFunction();
}

This seems to work if you can follow it.  Thanks for all the help Tam.  I will accept one of your comments as the answer.  
thanks again,

Darts

Author

Commented:
Thanks again for all the help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.