Home c++ 2 functions int Main ()

2 functions int Main ()




I have a file foo.cpp , which contains definitions of some functions and the function int Main () . This file is a separate target and can be running (Add_executable (Foo Foo.cpp) ).

I also have a bar.cpp file, which includes the foo.hpp file (containing some functions of some functions from Foo.cpp ), but also contains another int Main () .

The task is to be able to make target for bar.cpp , but for this to ads from foo.hpp you need to stick to the definitions from the Foo file .CPP . However, when linking, an error occurs due to two INT MAIN () functions. The same, if you make from Foo.cpp Library and sticking to Target Bar .

How can I solve this problem? Is it possible to somehow stick foo to bar without the int main () function>from foo.cpp ? Or choose from two functions the only thing so that there is no error?






add_executable (foo foo.cpp)
Add_executable (Bar Bar.cpp)
add_library (foo_lib foo.cpp foo.hpp)
target_link_libruries (Bar Foo_Lib) # Linking error: 2 functions int main ()

IMPORTANT: File Foo.cpp You can not change, everything else can be.

Answer 1

well, for example, file foo_wrapper.cpp:

# include & lt; foo.hpp & gt;
#Define Main This_is_Not_Main
#include "foo.cpp"

Answer 2

Each of the Main functions wrap in #if , for example:

# if foo_is_main

And when compiling, specify the corresponding constant.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions