-3

I am going through a source code and I found these snippets

extern int main(); main();

and one more is

extern void create_network_device(int N, const char* route, const char* ip); create_network_device(0, "10.0.0.0/24", "10.0.0.1");

What is happening in the above code ?

edit : the above snippets are in .cpp file. They are being called as shown above without any code in between.

karra
  • 45
  • 1
  • 10
  • 1
    Regarding the first snippet with `main`, in C++ it's actually invalid to call `main` yourself. It should *never* be called by your own code. – Some programmer dude May 07 '18 at 06:31
  • @GauravSehgal The link which you provided explains about the global variables and declared in header file. The above code is in .cpp file and I want to learn why is it they are calling in that manner. – karra May 07 '18 at 06:43
  • Can you provide the reproduced source code sections, in which it is used? – AmeyaVS May 07 '18 at 06:47
  • @karra it's same thing. .h file is just part of .cpp files text when compiler is concerned. See 5.2 phases of translation – Swift - Friday Pie May 07 '18 at 06:52
  • @AmeyaVS You can find in this link https://github.com/hioa-cs/IncludeOS/blob/master/examples/TCP_perf/service.cpp at line 99 and 100 – karra May 07 '18 at 06:52
  • @Swift Ok. But why are they calling it twice ? and just now I have seen in the source code has been updated from `main()` to `stream_main()` – karra May 07 '18 at 06:55
  • 2
    The function is not "called" twice, only once. The first is a *declaration* of the function, also called a *function prototype*. The declaration is needed so the compiler knows that the function exists *somewhere* in the program, and so it can be called. – Some programmer dude May 07 '18 at 06:58
  • exactly what Someprogrammerdude said. Note, the said line have noting in common with function call statement, which should consist of expression that returns function object (in simplest case, that would be function's name), followed by comma-separated list of expressions surrounded by parenthesis (can be empty list), intended to be passed arguments. Your `extern` string got obvious features of function's prototype declaration. – Swift - Friday Pie May 07 '18 at 08:20

1 Answers1

0

First snippet is technically UB because if seem that program defines function with extern C++ linkage void main() somewhere.

extern void create_network_device(int N, const char* route, const char* ip);
 create_network_device(0, "10.0.0.0/24", "10.0.0.1");

The line that starts with extern declares function create_network_device with external linkage, then a call to that function follow. That function may be in any compilation module of program.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42