我有以下程序:
#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> ©Obj) {
//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;
}
编译时编译没有问题,但不生成./a.out
文件。所以我使用了-o
命令,但也失败了。这是我的控制台输出
$:~/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
没有创建二进制文件的问题是什么?我的g ++版本是4.7,我正在运行Ubuntu 12.10。
PS:一个简单的hello world程序可以运行
如果我将文件从.hpp重命名为.cpp,则会正确编译。
注意:所有代码都在单个文件中。另一个文件只是空的。
为什么会这样?
修改
@teppic提到它
它将生成预编译头,而不是可执行文件
答案 0 :(得分:4)
来自gcc手册:
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.
即。扩展是错误的。
如果您检查文件类型,则会确认:
$ g++ -o foo foo.hpp
$ file foo
foo: GCC precompiled header (version 013) for C++