0
/* Header.h */
// filename : Testcpp41.h

namespace ProcCom {
    void SimpleFunc(void);
}

namespace BestCom {
    void SimpleFunc(void);
    void PrettyFunc(void);
}



/* Source1.cpp */
// filename : cpp_41_2.cpp 

#include <iostream>
#include "Testcpp41.h"

using namespace std;

void BestCom::SimpleFunc(void) {
    cout << "BestCom is defined." <<"\n";
    PrettyFunc();
    ProcCom::SimpleFunc();
}

void BestCom::PrettyFunc()
{
    cout << "Pretty !! " << "\n";    
}
void ProcCom::SimpleFunc(void) {
    cout << "ProcCom is defined" <<"\n";
}


/* Source2.cpp */
// filename : cpp_41_3.cpp
#include "Testcpp41.h"

int main(){
    BestCom::SimpleFunc();
    return 0;
}

I thought that the first source file need to be compiled.

So I am trying to compile it.

And then, I got the error as below.

I searched the issues about "Undefined symbols for architecture arm64."

But I don't know & figure out what the solution is.

If you already knew, or experienced it before, Please let me know what you already knew.

Error :

jinhojeon@jinui-Macmini cpp % g++ cpp_41_2.cpp -o cpp_41_2
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)
YSC
  • 38,212
  • 9
  • 96
  • 149
Jeon JI No
  • 25
  • 6
  • 2
    how do you compile and link the code? – 463035818_is_not_an_ai Jul 16 '21 at 15:12
  • 1
    To perform compile only (produce object file, no linking to executable binary), add `-c` option to `g++`. – MikeCAT Jul 16 '21 at 15:13
  • 1
    Your program doesn't have a main function. "But," you say, "it's in Source2.cpp!" yeah but you're probably not compiling Source2.cpp into your program. – user253751 Jul 16 '21 at 15:15
  • 2
    Try `g++ -c cpp_41_2.cpp -o cpp_41_2.o` (mind the `-c`) – YSC Jul 16 '21 at 15:16
  • 1
    or try `g++ cpp_41_2.cpp cpp_41_3.cpp -o myExec`, but you will need `#include` guards in your header file ... but very odd file names IMO,, and what are Header.h, Source1.cpp and Source2.cpp? Stale comments? Other project files? – yano Jul 16 '21 at 15:30
  • Right as Stale comments said @yano – Jeon JI No Jul 16 '21 at 16:06
  • @YSC It worked. Since, whether there exists an error or not, just complied. That's why "source2.cpp" couldn't. – Jeon JI No Jul 16 '21 at 16:10
  • @user253751, I thought that the logic : make a header file(function), define them in the first source file, and then complie the first. And then, complie the second source file. As 463035818_is_not_a_number said, that the problem is "link" with header and source file. But I don't know why it makes an error. I just did what the book said. what is the matter with Arm64?(My question) – Jeon JI No Jul 16 '21 at 16:15
  • @463035818_is_not_a_number Please read above. And My question is the latter(how to link those files and solve the error messages.) – Jeon JI No Jul 16 '21 at 16:16
  • @MikeCAT can make "cpp_41_2.o" file. but #include, the object file? happend as below 72... – Jeon JI No Jul 16 '21 at 16:18
  • 1
    @JeonJINo No, you need to **compile without linking** each source file (using `-c` flag) and then link the two together. Or, you can compile and link all at once by telling the compiler both source file names. Otherwise, it tries to make a program using just one source file, and that doesn't work because there isn't a main function. – user253751 Jul 16 '21 at 16:23
  • 1
    g++ cpp_41_2.cpp cpp_41_3.cpp -o cpp_41_ I GOT That thank you all :D:D:D:D:D @yano You knew !! – Jeon JI No Jul 16 '21 at 16:24
  • Thank you all my teachers. – Jeon JI No Jul 16 '21 at 16:34

0 Answers0