make print
gcc -x c++ try.C node.o -o try
node.o:1: unterminated string or character constant
*** Error code 1
make: Fatal error: Command failed for target `print'
Compilation exited abnormally with code 1 at Sun Mar 31 17:57:34
Can anybody explain what's going on here? the program is
clearly correct: when I simply #include "node.C"
into try.C everything compiles fine.
//*************** Makefile ********************
print: node.o
gcc -x c++ try.C node.o -o try
node.o: node.h
gcc -x c++ -c node.C
//************** End of file *****************
// ******************* node.h ***************************
class Node {
public:
Node::Node ();
Node::~Node ();
void Node::print ();
private:
char* data;
};
//************** End of file *****************
// ******************* try.C ***************************
#include "node.h"
int
main ()
{
Node* current;
current = new Node ();
current->print ();
return 0;
}
//************** End of file *****************
// ******************* node.C ***************************
extern "C" int printf (...);
#include "node.h"
Node::Node ()
{
data = new char;
data = "kuku";
}
Node::~Node ()
{
delete data;
}
void Node::print ()
{
printf ("%s\n", data);
}
//************** End of file *****************
[ Articles to moderate: mailto:c++-s...@netlab.cs.rpi.edu ]
[ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
[ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
[ Comments? mailto:c++-r...@netlab.cs.rpi.edu ]
Well, I delete the example because the error message is pretty much
clear enough. node.o contains an unterminated string or character
constant, and this at the beginning of the file. Now let's just try to
switch on our brain: if the compiler complains about a *syntax* error
in an *object* file, and this right at the beginning, it pretty much
seems like the compiler was trying to interpret the object file as a
c++ source file. Why on earth? Probably because you explicitly told it
that it is to consider all files on the command line, irrespective of
the actual extension, as C++ source (that's what -x c++ means, look it
up in the manpage).
Now the compiler is as baffled as I am: of course it would have known
anyway that an extension of .C is to be compiled as C++, so why do you
feel forced to tell it so explicitly?
Just trust the compiler:
gcc try.C node.o -o try
should do the trick perfectly. Although you should rather say
g++ try.C node.o -o try
so that the right libraries get linked in.
--
David Kastrup, Goethestr. 20, D-52064 Aachen Tel: +49-241-72419
Email: d...@pool.informatik.rwth-aachen.de Fax: +49-241-79502
: make print
: gcc -x c++ try.C node.o -o try
: node.o:1: unterminated string or character constant
: *** Error code 1
: make: Fatal error: Command failed for target `print'
Well, the problem is that you try to compile the node.o file, which is
an object file.
If you replace you Makefile with this, everything would work:
print: node.o try.o
gcc -o try node.o try.o
# rules
.C.o:
gcc -x c++ -c $< -o $@
# dependencies
node.o: node.h
try.o: node.h
: Compilationoexited abnormally with code 1 at Sun Mar 31 17:57:34
:
: Can anybody explain what's going on here? the program is
: clearly correct: when I simply #include "node.C"
: into try.C everything compiles fine.
btw: gcc -x c++ -o target list-of-object-files starts the compiler and
not the linker. IMHO this is incorrect behaviour of gcc. This means that
if you try to link a program with gcc -x c++, gcc starts cc1plus which tries
to compile you object file(s) rather than link them. The best solution
would simply to use g++ instead of -x c++
bjornw>
-------------------------------------------------------------------------------
Bjoern....@colargol.idb.hist.no
Srinivas
Although the program is clearly INcorrect, it compiles and executes as
expected on g++2.7.2 RS6000/AIX.
[...]
: Node::Node ()
: {
: data = new char;
Get a single new char? Gives a pointer to a single char.
: data = "kuku";
Now replace that pointer with a pointer to another string of characters. It
is a memory leak since the allocated char can not be deleted.
: }
: Node::~Node ()
: {
: delete data;
Now the real fun begins. Delete the first character of a string which was
not allocated by new. Anything can happen here, including system("rm -r *").
: }
: void Node::print ()
: {
: printf ("%s\n", data);
: }
: //************** End of file *****************
You want
data = new char[5].
strcpy(data, "kuku")
delete [] data
This may not solve your make problem, but at least it will give you something
to work with.
John
[snip]
I think the problem is that when you use "-x c++", gcc ignores the
file extension and treats all files as c++ files. Thus you're telling
gcc that "node.o" is c++ source code, which it clearly is not. I'd
try removing the "-x c++" from the makefile (both places, it isn't
necessary).
You told gcc to compile an object file, node.o, as C++. Don't do
that.
The "-x c++" tells gcc that the files are C++ source, and disables its
automatic determination of language. You can turn off the effect by
putting "-x none" later on the command line, or in this case you can
omit the option completely, as .C is recognised by gcc.
-zefram