This is my 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:
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:
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, s开发者_开发百科td::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
:
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. Try:
class C {
public:
C() {}
~C() {}
};
And, for C++ programs, use g++
to compile (as in your first attempt).
Replace
#include <iostream.h>
by
#include <iostream>
and provide implementations, at least empty, of the constructor and destructor of class 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:
- Don't use
iostream.h
, that header is pre-standard and well out of date. Use<iostream>
instead - You don't provide any implementation for the constructor and destructor of
C
, which is what the linker is complaining about.
You need to define your C constructor and destuctor:
C::C()
{
}
C::~C()
{
}
Also, stick with compiling with g++. If you look closely, the errors you get with compiling with gcc include everything you get with g++ plus extra errors.
You're including iostream.h
instead of iostream
, that's why you get a warning about this include. Also you have declared a constructor and a destructor for C
but you haven't actually implemented it anywhere. Therefore the linker complains about undefined symbols.
You need to add implementations for the methods of C
, like:
C::C() {
// ...
}
use #include <iostream>
instead of #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).
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.
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html
精彩评论