mkd
mkd

Reputation: 37

gc/c++ omit main from compilation, provide main in separate file

I'd like to do a compilation of two files which contain main methods by excluding or omitting the main method defined in one.

To give some background:

I'm trying to automate testing student-submitted code against a main method I provide.

My trouble is that the student code also included a main method. I'm wondering how I can compile (using g++) the file while excluding main. I'd just like to get the object code for the methods they define and link that with my new main method.

I'd rather find an automated way to do this rather than be forced to edit by hand 100s of submissions to remove / replace the main method.

Thanks

Upvotes: 2

Views: 235

Answers (1)

Jarod42
Jarod42

Reputation: 217990

You might add flag -Dmain=main2 for their files.

That mainly add #define main main2 which will substitute their main by a regular method.

Notice though that as main is a special method, especially, we can omit final return 0;, you should adapt warning configuration to not reject code with main2 without return (which fortunately would just be a warning and not an error).

Upvotes: 5

Related Questions