0

I have two header files:a.h and b.h. I want to use only one of them, based on some condition. How can I do this?

Here some code:

main.c:

#include<stdio.h>
#include"a.h"
#include"b.h"
main() {
    test();
    system("pause");
    return 0;
}

a.h:

#ifndef A_H
#define A_H
#include<stdio.h>
void test();
#endif

a.c:

#include "a.h"
void test() {
    printf("Got tested from a.\n");
}

b.h:

#ifndef B_H
#define B_H
#include<stdio.h>
void test();
#endif

b.c:

#include "b.h"
void test() {
    printf("Got tested from b.\n");
}

As I've said before,I only want to use one of them.How can I do this?

Neri-kun
  • 173
  • 3
  • 14
  • Depends on what you mean by "condition" and "use a header file". [This](https://en.wikipedia.org/wiki/Include_guard) might be enlightening, it shows how to include contents of the header file on the condition that they have not been already included. You should explain what condition you want to use. – hyde Oct 19 '19 at 14:04
  • @hyde Well,that's not kind of what I'm looking for.Maybe I wasn't clear enough,but first of all those two header files are independent.And regarding the condition,I don't have a specific condition in mind.It's just up to me to think about one,because that's what my assignment tells me to do. – Neri-kun Oct 19 '19 at 14:22
  • Indeed. You should show some example code. If I interpret what you say correctly, then no, you can't do that. Or, maybe, your question doesn't actually have anything to do with header files? Hard to say without code. – hyde Oct 19 '19 at 14:26
  • @hyde I'll edit my question as soon as possible in order to add some code which will most likely more clarity to you. – Neri-kun Oct 19 '19 at 14:28
  • 1
    The code you show does not compile, as I am sure you know. What are you trying to achieve? Here `test` is a global symbol and it can't have multiple different definitions. How to work around this depends on what actual problem you are trying to solve. – hyde Oct 19 '19 at 14:45
  • @hyde I know it doesn't compile."What are you trying to achieve?"Like I said,I want to be able to use the function ```test``` from just one header,despite being included two headers that contain this function. – Neri-kun Oct 19 '19 at 14:47
  • Also note that include files are just text, which is essentially copy-pasted to .c file when building. Since these .h files have same relevant content, you could just have one *ab.h* and include that. – hyde Oct 19 '19 at 14:47
  • @hyde One more thing,I edited ```test1()``` and ```test2()``` to ```test()``` to make it clear what I am trying to achieve. – Neri-kun Oct 19 '19 at 14:53
  • Well, you can't link with both a.c and b.c, as they have same symbol `test` with conflicting definition. So you need to make this choice at build time. And you really want just 1 header file. – hyde Oct 19 '19 at 15:14
  • Or, you could build a dynamic library and load it dynamically at runtime from the file you want (.so on unix, .dll on Windows). IOW use a plugin system. – hyde Oct 19 '19 at 15:15
  • And how can I make this choice at build time? – Neri-kun Oct 19 '19 at 15:16
  • What build system do you use? – hyde Oct 19 '19 at 15:32
  • By build system do you mean IDE or another specific tool? – Neri-kun Oct 19 '19 at 15:53
  • What ever you use to build this. I mean, this problem is not something which is covered by the C standard (other than, you can not do this). So solution must come from somewhere else. Oh, one more option, which would be pure C: use preprosessor macro to rename the function in a.c and b.c (say, to `test_a` and `test_b` before they are compiled, and then another function or macro with name `test` which selects which one to call. It all depends on *what is the purpose of all this*. – hyde Oct 19 '19 at 16:00
  • 'what is the purpose of all this?`.I don't really know to be honest with you.Like I said,it's an assignment that I got from my OOP course(yes I know,it seems kinda weird to get introduced first with C modular programming).I guess it's to test overloaded functions?Anyway,regarding what I use to build is Visual Studio 2019.That's all I can say. – Neri-kun Oct 19 '19 at 16:13

0 Answers0