-5
#include <iostream>
 void dummyfunction(void)
 {
     std::cout<< "this is";
 }

error:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

.How can i fix it??

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • Well, dunno, is there? What you show is fine -- though spacing could be improved. You will need to call `dummyfunction();` before it does anything from `main()`, etc.. Add at end `int main() { dummyfunction(); }` ( for reference, the `main()` function is the *entry-point* for program start-up) – David C. Rankin Jun 29 '21 at 04:48
  • Your question title is not specific. – Tyler Durden Jun 29 '21 at 05:01

1 Answers1

6

In a C/C++ program the main() function provides the entry point for program startup. (there are some specialized .ctor functions called before main() that you will rarely, if ever, encounter)

So what you have above declares the function dummyfunction(); but there is no main() function that the linker ld can use as the entry point (the address where program control is passed from the shell to your program to start running)

Your error messages is quite explicit about the lack of main() being the problem. To correct the error you need to define the main() function, where the correct invocations are either int main (void) or int main (int argc, char *argv[]) (you will see char *argv[] written equivalently as char **argv). There is also an env variable, but that isn't important here. If your program takes no arguments on the command line, then int main (void) is proper. With C++ the void can be omitted as it only makes a difference for C.

Including main(), your program will compile with:

#include <iostream>

void dummyfunction(void)
{
    std::cout << "this is\n";
}

int main ()
{
    dummyfunction();
}

(note: the addition of the '\n' at the end of "this is\n" so the output of your program terminates with a newline as specified by POSIX)

Example Use/Output

I just named to program executable dummyfunction, but you are free to name it anything you like.

$ ./dummyfunction
this is
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Shouldn't `main()` return a value, in order to avoid the dreaded Undefined Behavior curse? – Jeremy Friesner Jun 29 '21 at 05:15
  • 1
    `main()` returns `0` by default unless otherwise specified (at least since the C99 standard) It works the same for C++. That is a good point and you can easily add `return 0;` at the end to make the default behavior explicit. [Rationale behind return 0 as default value in C/C++](https://stackoverflow.com/q/329950/3422102) – David C. Rankin Jun 29 '21 at 05:21