Matt Young
Matt Young

Reputation: 73

Issues compiling C++ 11 code from a remote host

I'm trying to compile C++ 11 code on a server running Ubuntu from my home computer using Putty. I'm using a makefile to compile and include shared pointers in the code, but it gives me this error:

/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.

However, when I try to compile the code from the server hosting the files (when I'm at university), it compiles perfectly. Here is my makefile:

all: huffmandriver.o huffmannode.o huffmantree.o
    g++ -o huffencode huffmandriver.o huffmannode.o huffmantree.o -std=c++11

huffmannode.o: huffmannode.cpp huffmannode.h
    g++ -c huffmannode.cpp -std=c++11

huffmantree.o: huffmantree.cpp huffmantree.h
    g++ -c huffmantree.cpp -std=c++11

clean:
    @rm -f *.o
    @rm -f huffencode

I have also tried adding the flags -stdlib=libc++ -std=gnu++, but that does not work either. Here is a snippet of the code where the error is being thrown:

// Huffman Node class header

#ifndef HUFFMANNODE_H
#define HUFFMANNODE_H

#include <memory>
#include <string>

namespace YNGMAT005 {

class HuffmanNode {
    private:
        std::shared_ptr<HuffmanNode> left;
        std::shared_ptr<HuffmanNode> right;
        std::shared_ptr<HuffmanNode> parent;
        std::string letter;
        int frequency;

    public:
        HuffmanNode(std::string l, int freq);
        ~HuffmanNode();
        std::shared_ptr<HuffmanNode> & get_left();
        std::shared_ptr<HuffmanNode> & get_right();
        std::shared_ptr<HuffmanNode> & get_parent();
        void set_left(std::shared_ptr<HuffmanNode> & l);
        void set_right(std::shared_ptr<HuffmanNode> & r);
        bool has_left();
        bool has_right();
        void set_parent(std::shared_ptr<HuffmanNode> & p);
        bool has_parent();
        std::string get_letter();
        int get_frequency();
    };

}

#endif

Many thanks!

Upvotes: 1

Views: 313

Answers (2)

Daniel Trebbien
Daniel Trebbien

Reputation: 39218

You might need to compile huffmandriver.cpp with -std=c++11 as well. Currently, you have rules for compiling huffmannode.cpp and huffmantree.cpp with the -std=c++11 compiler option, but not huffmandriver.cpp.

You can create a custom pattern rule for making .o files from .cpp files and specifying the header dependencies like so:

huffmannode.o: huffmannode.h

huffmantree.o: huffmantree.h

%.o: %.cpp
    g++ -std=c++11 -c -march=native -o $@ $<

An alternative approach is to define the CXXFLAGS implicit variable to -std=c++11. This way, the built-in rule$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’ for compiling C++ source files will use the -std=c++11 compiler option.

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185988

I don't know why you're seeing different behaviour from a different login context (check which g++; g++ --version on both and see if your .profile, .bash_profile or .bashrc does anything weird based on ssh-vs-local login).

Nonetheless, you should be able to getting it working in both settings by providing -std=c++11 to the default rule for .cpp to .o thus:

CXXFLAGS = -std=c++11

Additionally, (shamelessly stealing from Daniel's answer) you can remove the explicit rules for the other .cpp files, just leaving the .h dependencies:

huffmannode.o: huffmannode.h

huffmantree.o: huffmantree.h

These will then automatically pick up the CXXFLAGS setting.

Upvotes: 2

Related Questions