Reputation: 31
I am stuck with the apparently common yet very cryptic compilation 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)
I narrowed it down to a tiny implementation (from a tutorial) which just won't compile.
Header file: Num.h
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
Implementation: Num.cpp
#include "Num.h"
Num::Num(int n): num(n) {}
int Num::getNum()
{
return num;
}
Compilation command:
g++ Num.cpp
I don't see any obvious hints in the invocation stack (via g++ Num.cpp -v
).
Both Num.h and Num.cpp are located in the same directory, so I don't get why the linker wouldn't be able to link them. I suspect that I might need to set some environment variable, but which one?
Any help would be greatly appreciated! Thanks for your patience - I know this error has been posted here a thousand times before, but I haven't found an answer that works for me.
Upvotes: 2
Views: 96
Reputation: 31
For reference, solution was to add a dummy main
to Num.cpp in order to compile with g++.
#include "Num.h"
int main(){}
Num::Num(int n): num(n) {}
int Num::getNum() { return num; }
Upvotes: -1
Reputation: 75062
By default you need main
function, from which program execution starts from there, to build executable binary file.
You can use -c
option like
g++ -c Num.cpp
to do compilation only (no linking) and get an object file Num.o
.
Upvotes: 2