[英]Error in compiling C++ code?
This is my test.cpp
: 这是我的
test.cpp
:
#include <iostream.h>
class C {
public:
C();
~C();
};
int main()
{
C obj;
return 0;
}
When I compile it using the command g++ test.cpp
, I get this error message: 当我使用命令
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
Compiling with gcc test.cpp
gives similar messages and even more: 使用
gcc test.cpp
进行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
Note that I haven't set LD_LIBRARY_PATH
: 请注意,我尚未设置
LD_LIBRARY_PATH
:
bash-3.2$ echo $LD_LIBRARY_PATH bash-3.2$
You have declared the existence of the C
constructor and destructor, but have not provided implementations. 您已经声明了
C
构造函数和析构函数的存在 ,但尚未提供实现。 Try: 尝试:
class C {
public:
C() {}
~C() {}
};
And, for C++ programs, use g++
to compile (as in your first attempt). 而且,对于C ++程序,请使用
g++
进行编译(如您的第一次尝试)。
Replace 更换
#include <iostream.h>
by 通过
#include <iostream>
and provide implementations, at least empty, of the constructor and destructor of class C. 并提供类C的构造函数和析构函数的实现(至少为空)。
As you don't provide an actual question, I'd have to guess at what you'd like to know. 由于您没有提供实际的问题,因此我不得不猜测您想知道的内容。 Anyway, my 2c are:
无论如何,我的2c是:
iostream.h
, that header is pre-standard and well out of date. iostream.h
,该标头是预标准的并且已经过时。 Use <iostream>
instead <iostream>
代替 C
, which is what the linker is complaining about. C
的构造函数和析构函数提供任何实现,而链接器在抱怨这些实现。 You need to define your C constructor and destuctor: 您需要定义C构造函数和析构函数:
C::C()
{
}
C::~C()
{
}
Also, stick with compiling with g++. 另外,坚持使用g ++进行编译。 If you look closely, the errors you get with compiling with gcc include everything you get with g++ plus extra errors.
如果仔细观察,使用gcc编译时遇到的错误包括使用g ++时得到的所有错误以及其他错误。
You're including iostream.h
instead of iostream
, that's why you get a warning about this include. 您要包含
iostream.h
而不是iostream
,这就是为什么您会收到有关此包含的警告的原因。 Also you have declared a constructor and a destructor for C
but you haven't actually implemented it anywhere. 另外,您已经为
C
声明了构造函数和析构函数,但实际上并没有在任何地方实现它。 Therefore the linker complains about undefined symbols. 因此,链接器抱怨未定义的符号。
You need to add implementations for the methods of C
, like: 您需要为
C
的方法添加实现,例如:
C::C() {
// ...
}
use #include <iostream>
instead of #include <iostream.h>
使用
#include <iostream>
而不是#include <iostream.h>
you should read the error properly. 您应该正确阅读错误。
One note about LD_LIBRARY_PATH - it doesn't concern you at compile or link time (then the linker will look in paths given with -L and some standard paths, like /usr/lib). 关于LD_LIBRARY_PATH的注释-在编译或链接时不关心您(然后,链接器将查找-L给定的路径以及一些标准路径,例如/ usr / lib)。
It's important When you run your application - the system will search for the shared libraries first in the paths, given in LD_LIBRARY_PATH. 重要的是运行应用程序时-系统将首先在LD_LIBRARY_PATH中指定的路径中搜索共享库。
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.