4

I'm working on a small open source project in C where I'm trying to use a test framework with C (the framework is min_unit).

I have a foo.h file with prototypes, and foo.c, with the implementation.

In my test file, tests.c, I have

#include "../test_framework/min_unit.h"
#include "foo.c"

... test cases ...

the problem is, because I have a main() function in foo.c (which I need to compile it), I can't compile tests.c because I get an error that states

note: previous definition of ‘main’ was here
int main() {

My question is, is there a way to make it so that the main() function in foo.c is conditional, so that it does not compile when I'm running tests.c? It's just annoying to have to remove and add main over and over.

CisBae
  • 165
  • 1
  • 13
  • 1
    Don't include the source file, instead declare the functions you need. – Some programmer dude Apr 19 '15 at 18:50
  • There are several frameworks called 'minunit' so would help to know which one. For the two I found, neither defined their own "main()" function. – Joe Apr 19 '15 at 18:50
  • Consider using `make` or `cmake`. –  Apr 19 '15 at 18:51
  • @JoachimPileborg do you mean put the function declarations that I need from `foo.c` near the top of `tests.c`? – CisBae Apr 21 '15 at 00:22
  • @Joe I was unaware of that, my apologies. The minunit framework I was using is this one: http://www.jera.com/techinfo/jtns/jtn002.html – CisBae Apr 21 '15 at 00:22
  • @ligoore are there advantages to using that setup (`make` or `cmake`) over the preprocessor declaration over main + compiler flag solution that was suggested? – CisBae Apr 21 '15 at 00:23
  • Choose what you find more convenient. If you are using preprocessor declaration, then imagine you come back to a project after a year, would you remember to define these macros? If not - you have to dig inside source files. With `make` you can specify which modules are compiled. One time for example `make test` the other `make program-name`. Seeing `Makefile` file in your project folder shows you use-cases with a simple `cat Makefile`. –  Apr 21 '15 at 06:39
  • Ohh I see, I must've been tired. I'm already using `makefile`s for all the compilation and test running. Thanks for the explanation! – CisBae Apr 21 '15 at 06:44

2 Answers2

6

The easiest way to use conditional compilation is to use #ifdef statements. E.g., in foo.c you have:

#ifdef NOT_TESTING //if a macro NOT_TESTING was defined
int main() {
    //main function here
}
#endif

While in test.c, you put:

#ifndef NOT_TESTING //if NOT_TESTING was NOT defined
int main() {
    //main function here
}
#endif

When you want to compile the main function in foo.c, you simply add the option -DNOT_TESTING to your compile command. If you want to compile the main function in test.c, don't add that option.

wolfPack88
  • 4,163
  • 4
  • 32
  • 47
  • I was so close! I was just about to edit my post saying that I have ```#ifndef TEST_IMPORT #if 0 int main() { return 0; } #endif #endif``` in `foo.c`, but I didn't think of the compile flag option, I'll try it now, thanks so much! – CisBae Apr 19 '15 at 18:51
  • That worked, nice solution. I'll definitely use that trick in the future, thanks a lot @wolfPack88 ! – CisBae Apr 19 '15 at 18:57
1

Haven't you try the use of pre-processor compiler conditions? May be you've tried but it doesn't work, hum?

Anyway, you probably should:

1- Define a token at top of "tests.c" class file like:

#defined foo_MIN_UNIT_TEST

2- Surround your "main() { ... } " method in "foo.c" class file with #ifndef / #endif like:

#ifndef foo_MIN_UNIT_TEST  //consider using #ifndef not #ifdef!!!
int main() 
{ ... }
#endif

3- This way, when you compile your unit test files, the main() method of foo.c will not be included in compile time and the only main() method of tests will be available to compiler.

For further reading: http://www.cprogramming.com/

Regards.

Arman Hatefi
  • 43
  • 1
  • 8
  • Hey thanks for the input, I actually did use preprocessor directives, but couldn't get it to work properly. I indicated that in my comment to @wolfPack88. Thanks for the help! I'll keep this in mind as well. – CisBae Apr 19 '15 at 19:33