error compiling on macbook terminal

I encountered the following error message when I try to compile using g++ on my macbook terminal


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

i was wondering if this issue has more to do with my compiler on my macbook terminal?

the same code seems to run alright elsewhere.

how may i resolve this on my macbook?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

bool comp(string s1, string s2) {
   return (s1 == s2);
}

int main(void) {
   vector<string> v = {"ONE", "Two", "Three"};
   bool result;

   result = binary_search(v.begin(), v.end(), "one", comp); 

   if (result == true)
      cout << "String \"one\" exist in vector." << endl;

   v[0] = "Ten";

   return 0;
}
Last edited on
Show your whole command line, as you typed it, and all the output that results from it.
L12 try:

 
int main() {

here's my whole command string, as i typed, with the output messages

1
2
3
4
5
6
7
8
9
(base) MacBook-Air% g++ -o compare comparestring.cpp

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

(base) MacBook-Air% 



i did try

int main (){

instead of

int main(void){

doesn't work either
See if this helps. I don't know what they are going on about, something about linking to allegro, which I know is an allergy medication. This looks like a different compiler, though, so maybe this is not relevant.

https://stackoverflow.com/questions/33304295/main-not-defined-on-mac-os-x-10-11-in-c

everything else I see implies your g++ isnt installed right, somehow.
Last edited on
Curious to see what's in the object file that it created.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ cat foo.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

bool comp(string s1, string s2) {
   return (s1 == s2);
}

int main(void) {
   vector<string> v = {"ONE", "Two", "Three"};
   bool result;

   result = binary_search(v.begin(), v.end(), "one", comp);

   if (result == true)
      cout << "String \"one\" exist in vector." << endl;

   v[0] = "Ten";

   return 0;
}
$ g++ -o foo_program foo.cpp
$ nm -C foo_program | egrep ' [T] '
0000000000002948 T _fini
0000000000002940 T __libc_csu_fini
00000000000028d0 T __libc_csu_init
0000000000001412 T main
0000000000001300 T _start
00000000000013e9 T comp(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, \
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

There's main and comp from your code, and some stuff from the runtime support libraries.

You can also try the -v as it suggests.
g++ -v -o foo_program foo.cpp
It prints a lot of information about what the compiler is up to.
It might indicate where it's going wrong.
Topic archived. No new replies allowed.