linker command failed with exit code 1 in g++ with -o3 flag
<p>I am using the code <a href="https://stackoverflow.com/questions/37269825/memory-leak-of-pointer-to-pointer/37270192#37270192">in this page</a>. Recently I would like to compile with -o3 flag on my mac terminal. It turns out showing the error message, but the code can be successfully complied with -o flag without any errors.</p>

<p>Here are my code</p>

<pre><code>// C / C++ program for Dijkstra's shortest path algorithm for adjacency
// list representation of graph


// C / C++ program for Dijkstra's shortest path algorithm for adjacency
// list representation of graph

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;limits.h&gt;


// A structure to represent a node in adjacency list
struct AdjListNode
{
    int dest;
    int weight;
    struct AdjListNode* next;
};

// A structure to represent an adjacency liat
struct AdjList
{
    struct AdjListNode *head;  // pointer to head node of list
};

// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
    int V;
    struct AdjList* array;
};

// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest, int weight)
{
    struct AdjListNode* newNode =
    (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
    newNode-&gt;dest = dest;
    newNode-&gt;weight = weight;
    newNode-&gt;next = NULL;
    return newNode;
}

// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
    struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
    graph-&gt;V = V;

    // Create an array of adjacency lists.  Size of array will be V
    graph-&gt;array = (struct AdjList*) malloc(V * sizeof(struct AdjList));

    // Initialize each adjacency list as empty by making head as NULL
    for (int i = 0; i &lt; V; ++i)
        graph-&gt;array[i].head = NULL;

    return graph;
}

// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest, int weight)
{
    // Add an edge from src to dest.  A new node is added to the adjacency
    // list of src.  The node is added at the begining
    struct AdjListNode* newNode = newAdjListNode(dest, weight);
    newNode-&gt;next = graph-&gt;array[src].head;
    graph-&gt;array[src].head = newNode;

    // Since graph is undirected, add an edge from dest to src also
    newNode = newAdjListNode(src, weight);
    newNode-&gt;next = graph-&gt;array[dest].head;
    graph-&gt;array[dest].head = newNode;
}

// Structure to represent a min heap node
struct MinHeapNode
{
    int  v;
    int dist;
};

// Structure to represent a min heap
struct MinHeap
{
    int size;      // Number of heap nodes present currently
    int capacity;  // Capacity of min heap
    int *pos;     // This is needed for decreaseKey()
    struct MinHeapNode **array;
};

// A utility function to create a new Min Heap Node
struct MinHeapNode* newMinHeapNode(int v, int dist)
{
    struct MinHeapNode* minHeapNode =
    (struct MinHeapNode*) malloc(sizeof(struct MinHeapNode));
    minHeapNode-&gt;v = v;
    minHeapNode-&gt;dist = dist;
    return minHeapNode;
}

// A utility function to create a Min Heap
struct MinHeap* createMinHeap(int capacity)
{
    struct MinHeap* minHeap =
    (struct MinHeap*) malloc(sizeof(struct MinHeap));
    minHeap-&gt;pos = (int *)malloc(capacity * sizeof(int));
    minHeap-&gt;size = 0;
    minHeap-&gt;capacity = capacity;
    minHeap-&gt;array =
    (struct MinHeapNode**) malloc(capacity * sizeof(struct MinHeapNode*));
    return minHeap;
}

// A utility function to swap two nodes of min heap. Needed for min heapify
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
    struct MinHeapNode* t = *a;
    *a = *b;
    *b = t;
}

// A standard function to heapify at given idx
// This function also updates position of nodes when they are swapped.
// Position is needed for decreaseKey()
void minHeapify(struct MinHeap* minHeap, int idx)
{
    int smallest, left, right;
    smallest = idx;
    left = 2 * idx + 1;
    right = 2 * idx + 2;

    if (left &lt; minHeap-&gt;size &amp;&amp;
        minHeap-&gt;array[left]-&gt;dist &lt; minHeap-&gt;array[smallest]-&gt;dist )
        smallest = left;

    if (right &lt; minHeap-&gt;size &amp;&amp;
        minHeap-&gt;array[right]-&gt;dist &lt; minHeap-&gt;array[smallest]-&gt;dist )
        smallest = right;

    if (smallest != idx)
    {
        // The nodes to be swapped in min heap
        MinHeapNode *smallestNode = minHeap-&gt;array[smallest];
        MinHeapNode *idxNode = minHeap-&gt;array[idx];

        // Swap positions
        minHeap-&gt;pos[smallestNode-&gt;v] = idx;
        minHeap-&gt;pos[idxNode-&gt;v] = smallest;

        // Swap nodes
        swapMinHeapNode(&amp;minHeap-&gt;array[smallest], &amp;minHeap-&gt;array[idx]);

        minHeapify(minHeap, smallest);
    }
}

// A utility function to check if the given minHeap is ampty or not
int isEmpty(struct MinHeap* minHeap)
{
    return minHeap-&gt;size == 0;
}

// Standard function to extract minimum node from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
    if (isEmpty(minHeap))
        return NULL;

    // Store the root node
    struct MinHeapNode* root = minHeap-&gt;array[0];

    // Replace root node with last node
    struct MinHeapNode* lastNode = minHeap-&gt;array[minHeap-&gt;size - 1];
    minHeap-&gt;array[0] = lastNode;

    // Update position of last node
    minHeap-&gt;pos[root-&gt;v] = minHeap-&gt;size-1;
    minHeap-&gt;pos[lastNode-&gt;v] = 0;

    // Reduce heap size and heapify root
    --minHeap-&gt;size;
    minHeapify(minHeap, 0);

    return root;
}

// Function to decreasy dist value of a given vertex v. This function
// uses pos[] of min heap to get the current index of node in min heap
void decreaseKey(struct MinHeap* minHeap, int v, int dist)
{
    // Get the index of v in  heap array
    int i = minHeap-&gt;pos[v];

    // Get the node and update its dist value
    minHeap-&gt;array[i]-&gt;dist = dist;

    // Travel up while the complete tree is not hepified.
    // This is a O(Logn) loop
    while (i &amp;&amp; minHeap-&gt;array[i]-&gt;dist &lt; minHeap-&gt;array[(i - 1) / 2]-&gt;dist)
    {
        // Swap this node with its parent
        minHeap-&gt;pos[minHeap-&gt;array[i]-&gt;v] = (i-1)/2;
        minHeap-&gt;pos[minHeap-&gt;array[(i-1)/2]-&gt;v] = i;
        swapMinHeapNode(&amp;minHeap-&gt;array[i],  &amp;minHeap-&gt;array[(i - 1) / 2]);

        // move to parent index
        i = (i - 1) / 2;
    }
}

// A utility function to check if a given vertex
// 'v' is in min heap or not
bool isInMinHeap(struct MinHeap *minHeap, int v)
{
    if (minHeap-&gt;pos[v] &lt; minHeap-&gt;size)
        return true;
    return false;
}

// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void dijkstra(struct Graph* graph, int src)
{
    int V = graph-&gt;V;// Get the number of vertices in graph
    int dist[V];      // dist values used to pick minimum weight edge in cut

    // minHeap represents set E
    struct MinHeap* minHeap = createMinHeap(V);

    // Initialize min heap with all vertices. dist value of all vertices
    for (int v = 0; v &lt; V; ++v)
    {
        dist[v] = INT_MAX;
        minHeap-&gt;array[v] = newMinHeapNode(v, dist[v]);
        minHeap-&gt;pos[v] = v;
    }

    // Make dist value of src vertex as 0 so that it is extracted first
    minHeap-&gt;array[src] = newMinHeapNode(src, dist[src]);
    minHeap-&gt;pos[src]   = src;
    dist[src] = 0;
    decreaseKey(minHeap, src, dist[src]);

    // Initially size of min heap is equal to V
    minHeap-&gt;size = V;

    // In the followin loop, min heap contains all nodes
    // whose shortest distance is not yet finalized.
    while (!isEmpty(minHeap))
    {
        // Extract the vertex with minimum distance value
        struct MinHeapNode* minHeapNode = extractMin(minHeap);
        int u = minHeapNode-&gt;v; // Store the extracted vertex number

        // Traverse through all adjacent vertices of u (the extracted
        // vertex) and update their distance values
        struct AdjListNode* pCrawl = graph-&gt;array[u].head;
        while (pCrawl != NULL)
        {
            int v = pCrawl-&gt;dest;

            // If shortest distance to v is not finalized yet, and distance to v
            // through u is less than its previously calculated distance
            if (isInMinHeap(minHeap, v) &amp;&amp; dist[u] != INT_MAX &amp;&amp;
                pCrawl-&gt;weight + dist[u] &lt; dist[v])
            {
                dist[v] = dist[u] + pCrawl-&gt;weight;

                // update distance value in min heap also
                decreaseKey(minHeap, v, dist[v]);
            }
            pCrawl = pCrawl-&gt;next;
        }
    }

    free(minHeap-&gt;pos);

    for (int i=0;i&lt;minHeap-&gt;size;i++) {
        free(minHeap-&gt;array[i]);
    }

    free(minHeap-&gt;array);
    free(minHeap);
}


// Driver program to test above functions
int main()
{
    // create the graph given in above fugure
    int V = 20000,t=0;
    while (t!=10) {
        struct Graph* graph = createGraph(V);
        for (int i=0; i&lt;10000; i++) {
            for(int j=10000;j&lt;20000;j++){
                addEdge(graph, 0, i, i);
                addEdge(graph, i, j, i+j);
            }
        }
        dijkstra(graph, 0);

        for(int d=0; d&lt;graph-&gt;V; d++)
        {
            AdjListNode *p1=graph-&gt;array[d].head, *p2;
            while(p1)
            {
                p2=p1;
                p1=p1-&gt;next;
                free(p2);
            }
        }
        free(graph-&gt;array);
        free(graph);
        t++;
    }

    return 0;
}
</code></pre>

<p>Here is the error message</p>

<pre><code>seshunsakaitekiMacBook-Pro:test Daniel$ g++ -o3 TEST main.cpp
ld: can't link with a main executable file 'TEST' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
</code></pre>