这是我的test.cpp
:
#include <iostream.h>
class C {
public:
C();
~C();
};
int main()
{
C obj;
return 0;
}
当我使用命令g++ test.cpp
编译它时,我收到以下错误消息:
In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31, from test.cpp:1: /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated. /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x131): undefined reference to `C::C()' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/ccoYkiAS.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()' collect2: ld returned 1 exit status
使用gcc test.cpp
进行编译会发出类似的消息,甚至更多:
In file included from /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/iostream.h:31, from test.cpp:1: /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated. /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xd): undefined reference to `std::basic_string, std::allocator >::size() const' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x60): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x9f): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0xce): undefined reference to `std::basic_string, std::allocator >::operator[](unsigned int) const' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x131): undefined reference to `C::C()' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x13c): undefined reference to `C::~C()' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x165): undefined reference to `std::ios_base::Init::Init()' /cygdrive/c/Users/aswinik_sattaluri/AppData/Local/Temp/cc3ntGx0.o:test.cpp:(.text+0x180): undefined reference to `std::ios_base::Init::~Init()' collect2: ld returned 1 exit status
请注意,我尚未设置LD_LIBRARY_PATH
:
bash-3.2$ echo $LD_LIBRARY_PATH bash-3.2$
答案 0 :(得分:10)
您已声明C
构造函数和析构函数的存在,但尚未提供实现。尝试:
class C {
public:
C() {}
~C() {}
};
而且,对于C ++程序,使用g++
进行编译(如第一次尝试)。
答案 1 :(得分:9)
替换
#include <iostream.h>
通过
#include <iostream>
并提供类C的构造函数和析构函数的实现,至少为空。
答案 2 :(得分:5)
由于您没有提供实际问题,我不得不猜测您想知道什么。无论如何,我的2c是:
iostream.h
,该标头已预先标准且已过期。请改用<iostream>
C
的构造函数和析构函数提供任何实现,这是链接器所抱怨的。答案 3 :(得分:4)
您需要定义C构造函数和destuctor:
C::C()
{
}
C::~C()
{
}
另外,坚持用g ++编译。如果仔细观察,使用gcc编译时遇到的错误包括g ++加上额外错误所带来的一切。
答案 4 :(得分:4)
您包含iostream.h
而不是iostream
,这就是您收到此警告的原因。你还为C
声明了一个构造函数和析构函数,但你实际上并没有在任何地方实现它。因此链接器抱怨未定义的符号。
您需要为C
的方法添加实现,例如:
C::C() {
// ...
}
答案 5 :(得分:0)
使用#include <iostream>
代替#include <iostream.h>
你应该正确阅读错误。
答案 6 :(得分:0)
关于LD_LIBRARY_PATH的一个注意事项 - 它在编译或链接时并不关心你(然后链接器会查看用-L和一些标准路径给出的路径,比如/ usr / lib)。
这很重要当您运行应用程序时 - 系统将首先在LD_LIBRARY_PATH中给出的路径中搜索共享库。
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html