0

It is a C question as says the title.

I have a function named "net_send" defined in two different header files. Here is the example:

f1.h and f2.h both contain net_send(args) but with different arguments.

I get an error that the function is already defined in alternate file. I know function overloading is not allowed in C. Also, I cannot edit those files, as these are from external libraries. Also, there are functions in each of those files which are being used by my program itself.

So what should I do? Remember I cannot change the original files.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Khawar
  • 23
  • 4
  • So you have two libraries that contain an identically named function? And you don't have the source code to those libraries? –  Feb 27 '17 at 13:20
  • 1
    If the C compiler is able to detect this then you have at least one C source file which does (directly or indirectly) include both headers (`f1.h` and `f2.h`). Otherwise, this probably becomes a linker (instead of compiler) issue. – Scheff's Cat Feb 27 '17 at 13:23
  • 3
    Different argument *names* or different argument *types*? – Some programmer dude Feb 27 '17 at 13:23
  • @Evert I have source code of both libraries but I don't want to change it. – Khawar Feb 27 '17 at 13:25
  • @Scheff I am including those two headers as I have mentioned it that there are some functions in both of them that I need in the same source file of my custom program. – Khawar Feb 27 '17 at 13:26
  • Why you don't want to change it? How are you going to choose which function to call? – Iharob Al Asimi Feb 27 '17 at 13:26
  • @Someprogrammerdude exactly as well. – Khawar Feb 27 '17 at 13:26
  • 3
    If both libraries contain a `net_send` function without a proper name-spacing prefix they should both be deemed poorly written. And you have no choice but to write a wrapper for one of those functions with a different name. – StoryTeller - Unslander Monica Feb 27 '17 at 13:27
  • @Khawar This looks like an instance of the [XY Problem](http://xyproblem.info). Could you say what you really want? – Iharob Al Asimi Feb 27 '17 at 13:28
  • @IharobAlAsimi exactly that is my question. How I am able to choose without changing code in the dependencies, is actually what i am referring. – Khawar Feb 27 '17 at 13:28
  • Your requirement does not make sense, you can use a c++ compiler if the arguments differ and the c++ compiler will do the wise thing and use one name for each function. Because if you manage to solve the problem at the source code level, I think the linker will be very confused. – Iharob Al Asimi Feb 27 '17 at 13:29
  • 2
    @IharobAlAsimi - Huh? If the libraries are pre-built and compiled with a C compiler, that will still never link. – StoryTeller - Unslander Monica Feb 27 '17 at 13:30
  • Thanks John and Klas. The solution is certainly to wrap of of it up in a third library and include it. – Khawar Feb 27 '17 at 13:31
  • 1
    @StoryTeller True but the OP claimed that he owns the source code so he can recompile, but apparently refuses to do the right thing and change the names. – Iharob Al Asimi Feb 27 '17 at 13:31
  • Thanks @StoryTeller about the wrapping stuff. – Khawar Feb 27 '17 at 13:32
  • @IharobAlAsimi - Recompiling is one thing. But changing is another. He may not want to do it, so as to avoid breaking a software license. And even then, a perfectly good C program may not build with a C++ compiler. There is no silver bullet here. – StoryTeller - Unslander Monica Feb 27 '17 at 13:32
  • @StoryTeller You are right again and polymorphism is one of the things I hate about c++. I did not think about a license and you are right again. But, still it's worth trying, because even though your statement about a c program not always compiling as c++ is true, it's not normally the case unless the code was written in certain way. – Iharob Al Asimi Feb 27 '17 at 13:34

0 Answers0