Search code examples
c++pragmaredefinition

ignore a main in a header file


I'm trying to use (Ligra) in a project. The framework works as long as the chief header "ligra.h" is included. Trouble is, that header has an implementation of parallel_main, which is a macro wrapper around main with OpenMP trickery. So if I wanted to write a simple program:

#include "ligra.h"
#include <iostream>

int main(){
std::cout<<"Hello World";
return 0;
} 

It would not compile. Redefinition of symbol main.

Also, I need a parallel_main, with the exact macro trickery done in the "parallel.h" header.

So I think I have two options:

1) modify the file, add a pair of #ifdef LIGRA_MAIN's and not define the macro at compile time. Thus I can have my own main and not have redefinition. Trouble is I need my project to be using the upstream version of ligra, and Julian Shun, the original developer has probably forgottten about his project (and github, since he ignored more than one pull request).

2) Use/Write a #pragma that would strip that function out at the include stage.

I don't know how to do that last part, and would be very much in your debt if someone who did, reached out.


Solution

  • A solution that does not involve modifying library files (but is somewhat brittle) could be to do the following:

    1. #include "ligra/parallel.h" (this does #define parallel_main main).

    2. #undef parallel_main to prevent this rewriting of function names.

    3. #include "ligra/ligra.h" as usual. Since parallel.h has an include guard, its repeated inclusion is prevented and parallel_main will not be redefined.

    4. Proceed as normal.

    You might also want to wrap this into a header so you only have to write it once.

    Alternatively, you could do what @user463035818 suggests and redefine main only for the inclusion of ligra.h for very similar effect. The difference is in the names that the parallel_main function(s) from ligra will get.