I have a program with multiple headers and cpp files, I want to call a function defined in the same file main is defined (from a function defined in another file). If I'm correct, the file with the main function never has a corresponding header, right? How would I do it?
Asked
Active
Viewed 2,941 times
0
-
1" If I'm correct the file with the main function never has a corresponding header, right?" Only if you don't write one. – Mar 05 '17 at 21:31
-
*"If I'm correct the file with the main function never has a corresponding header, right?"* How did you get that idea? Because your IDE did not auto-generate one? – Baum mit Augen Mar 05 '17 at 21:32
-
2You just need to *declare* the called function. Often that is (conventionally) done in some header file. You need to read a good book on C++ programming. – Basile Starynkevitch Mar 05 '17 at 21:34
-
You seem to be in need of [a good book on c++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?lq=1) – Rob K Apr 23 '18 at 20:03
1 Answers
0
Why wouldn't you be allowed to have a header for functions inside main.cpp
? Assuming main.cpp
defines main
and foo
, nothing prevents you from having a header containing the declaration of foo
.
In fact, some libraries and frameworks make their own main
. In cases like these, you just link against the library that contains the main
.
For example, if you look at the last example code of this page of Boost's Unit Test Framework, you will see there is no main
. The main
is already written for you inside the Boost Unit Test Framework library.

adentinger
- 1,367
- 1
- 14
- 30
-
1"You could even call main from within your program" - Not in C++. Also, I think the Standard forbids declaring main. In any case, the OP is not asking about calling main. – Mar 05 '17 at 21:53
-