In a C project, I have a main() function in several files. When I compile I thus have an error “multiple declarations of main”. Is it possible to choose in the Makefile which one of those main() functions should be used to compile ? (the other ones would then be ignored…)
Share
You could hide them using the pre-processor:
In file1.c:
This can be repeated as necessary in any number of C files.
Then have logic in the Makefile that passes the proper -D option to the compiler, i.e. -DFILE1_MAIN to include the
main()from file1, -DFILE2_MAIN to get file2.c’s, and so on.This technique can also be useful when implementing e.g. library modules, to include an optional
main()for testing in a single C file.