-2

I am trying to use Makefile to compile my project for c++ (Huffmans compression) but I keep getting this compiler 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) make: *** [huff] Error 1

Since this is for my assignment I don't think I would be able to put all my code online, but I should be able to include my Makefile and I will include my main cpp file as well as it is barebones for now.

all:    huff

huff:   HuffmanCompression.o PriorityQueue.o Node.o TreeNode.o FrequencyCounter.o BitStream.o HuffmanTree.o
    g++ -Wall -std=c++11 HuffmanCompression.o PriorityQueue.o Node.o TreeNode.o FrequencyCounter.o BitStream.o HuffmanTree.o -o huff

HuffmanCompression.o:   HuffmanCompression.cpp PriorityQueue.o HuffmanTree.o
    g++ -c HuffmanCompression.cpp

PriorityQueue.o:    PriorityQueue.cpp PriorityQueue.h Node.o
    g++ -c PriorityQueue.cpp

Node.o: Node.cpp Node.h
    g++ -c Node.cpp

TreeNode.o: TreeNode.cpp TreeNode.h
    g++ -c TreeNode.cpp

FrequencyCounter.o: FrequencyCounter.cpp FrequencyCounter.h
    g++ -c FrequencyCounter.cpp

BitStream.o:    BitStream.cpp BitStream.h FrequencyCounter.o
    g++ -c BitStream.cpp

HuffmanTree.o: HuffmanTree.cpp HuffmanTree.h TreeNode.o
    g++ -c HuffmanTree.cpp

clean:  
    rm -f huff *.o

and here is the main cpp:

#include <list>

#include "HuffmanTree.h"
#include "PriorityQueue.h"

class HuffmanCompression {

    int main(int argc, char** argv) {
        //std::list<FrequencyCounter> mylist;

        return 0;
    }
};    
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Your makefile doesn't mention main.cpp. This should probably be closed as a typo rather than a duplicate, but eh. – HolyBlackCat Mar 18 '19 at 17:45
  • You forgot to compile `main.cpp` in your makefile – NathanOliver Mar 18 '19 at 17:45
  • @HolyBlackCat The main C++ I'm using is named HuffmanCompression which I think I included in the Makefile, I don't know much about Makefiles usually they are given to us but this time we had to form our own and I don't know which part I messed up. Thanks! – Dana Salman Mar 18 '19 at 17:52
  • @NathanOliver ^ Do I have to have my main file named main.cpp? – Dana Salman Mar 18 '19 at 17:53
  • @DanaSalman No. You just need a single `.cpp` file that has a `main` function in it. – NathanOliver Mar 18 '19 at 17:54
  • 1
    @DanaSalman _"Do I have to have my main file named main.cpp?"_ No, you can name it as you like. You just have to ensure that it contains a global function definition for `int main()` and also ensure to link it in the right order. What you have places a `main()` function in a namespace. – πάντα ῥεῖ Mar 18 '19 at 17:55
  • @NathanOliver gotcha! I do have that in my "HuffmanCompression.cpp" class so thats why I am a bit lost on this. – Dana Salman Mar 18 '19 at 17:55
  • See the answer below then. Unlike Java or C#, `main` should not be a class member function. It needs to be a free function in the global space of the file. – NathanOliver Mar 18 '19 at 17:56
  • That was right damn I didn't see that for the longest time. Thanks for your help and everyone elses! – Dana Salman Mar 18 '19 at 17:58

1 Answers1

1

There are two separate errors.

First, you forgot to add main.cpp to your makefile.

Second, your main.cpp doesn't contain a global function named main. A class member function named main doesn't qualify. You need a function named main in the global namespace with external linkage.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243