c++ubuntug++

no ./a.out produced in g++


I have the following program:

#include "Sptr.cpp"
#include <iostream>
#include <stdio.h>
#include <assert.h>

namespace my {
    template <class T>
    class Sptr {
    private:
        //some kind of pointer
            //one to current obj
        T obj;
        size_t reference_count;
            //one to original obj
    public:
        Sptr();

        template <typename U> 
        Sptr(U *);

        Sptr(const Sptr &);

        template <typename U> 
        Sptr(const Sptr<U> &);

        template <typename U> 
        Sptr<T> &operator=(const Sptr<U> &);

        void reset();

        T* operator->() const
        {return &obj;};

        T& operator*() const
        {return obj;};

        T* get() const
        {return &obj;};

        //operator unspecified_bool_type() const;

        //overload *,->,=,copy-constructor

        // const-ness should be preserved.
        // Test for null using safe-bool idiom
        // Static casting, returns a smart pointer
    };

    template <typename T>
    Sptr<T>::Sptr() {
        //do something
    }

    template <typename T>
    template <typename U> 
    Sptr<T>::Sptr(U* u) {
        //do something
    }

    template <typename T> 
    Sptr<T>::Sptr(const Sptr<T> &copyObj) {
        //do copy constructor stuff
    }

    template <typename T> 
    template <typename U> 
    Sptr<T>& Sptr<T>::operator=(const Sptr<U> &t) {
        return *this;
    }

    template <typename T>
    void Sptr<T>::reset() {
        //do something
    }

    template <typename T1, typename T2>
    bool operator==(const Sptr<T1> &, const Sptr<T2> &) {
        //do something
        return true;
    }

    template <typename T, typename U>
    Sptr<T> static_pointer_cast(const Sptr<U> &sp) {
        //do something
        return true;
    }

    template <typename T, typename U>
    Sptr<T> dynamic_pointer_cast(const Sptr<U> &sp) {
        //Do something
        return true;
    }
}


using namespace std;
using namespace my;
/* Basic Tests 1 ================================================================================ */

class Base1 {
    protected:
        Base1() : derived_destructor_called(false) {
            printf("Base1::Base1()\n");
        }
    private:
        Base1(const Base1 &); // Disallow.
        Base1 &operator=(const Base1 &); // Disallow.
    protected:
        ~Base1() {
            printf("Base1::~Base1()\n");
            assert(derived_destructor_called);
        }
    protected:
        bool derived_destructor_called;
};

class Derived : public Base1 {
        friend void basic_tests_1();
    private:
        Derived() {}
        Derived(const Derived &); // Disallow.
        Derived &operator=(const Derived &); // Disallow.
    public:
        ~Derived() {
            printf("Derived::~Derived()\n");
            derived_destructor_called = true;
        }
        int value;
};

void basic_tests_1() {

}

int main(int argc, char *argv[]) {

    cout << "Hello world";
    basic_tests_1();

    return 0;
}

When I compile it compiles without a problem, but the ./a.out file is not produced. So I used the -o command but that also failed. Here is my console output

$:~/oops$ g++ Sptr.hpp
$:~/oops$ ./a.out
bash: ./a.out: No such file or directory
$:~/oops$ g++ Sptr.hpp -o sp
$:~/oops$ ./sp
bash: ./sp: Permission denied
$:~/oops$ sudo ./sp
sudo: ./sp: command not found
$:~/oops$ ls
1  sp  Sptr.cpp Sptr.hpp
$:~/oops$ ls -la sp
-rw-rw-r-- 1 adarshakb adarshakb 10763696 Mar 29 19:33 sp
$:~/oops$ chmod 0777 ./sp
$:~/oops$ ls -la sp
-rwxrwxrwx 1 adarshakb adarshakb 10763696 Mar 29 19:33 sp
$:~/oops$ ./sp
bash: ./sp: cannot execute binary file

What is the problem that the binary is not being created? My g++ version is 4.7 and am running a Ubuntu 12.10.

PS: A simple hello world program works


If I rename the file from .hpp to .cpp it compiles correctly.

Note: All the codes are in single file. The other file is just empty.

Why is this?

EDIT:

@teppic mentions it

It'll generate a precompiled header, not an executable


Solution

  • From the gcc manual:

       file.hh
       file.H
       file.hp
       file.hxx
       file.hpp
       file.HPP
       file.h++
       file.tcc
       C++ header file to be turned into a precompiled header or Ada spec.
    

    i.e. the extension is wrong.

    If you check the type of file, it's confirmed:

    $ g++ -o foo foo.hpp
    $ file foo
    foo: GCC precompiled header (version 013) for C++