Thread View: gnu.gcc.help
5 messages
5 total messages
Started by UNLISTED
Mon, 04 Sep 1995 19:43
how can i tell if my new c++ compiler is working?
Author: UNLISTED
Date: Mon, 04 Sep 1995 19:43
Date: Mon, 04 Sep 1995 19:43
43 lines
1411 bytes
1411 bytes
i just finished installing the libg++ library for c++ and i want to test the
compiler to see if it is working. when i try to compile the following little
test file using gcc,
i get these errors:
collect2: ld returned 2 exit status
ld: Undefined symbol
String::length(void)
String::set_to(char *)
String::read(void)
what do these errors mean i did or didn't do?
(i'm new at c++)
below are the files...
/* this is the beginning of file String.h */
const int max_string_length = 128;
class String{
public:
void set_to(char*);
int length();
int read();
void print();
private:
char text[max_string_length+1];
};
/* this is the end of file String.h */
/* and the beginning of file test.cc */
#include "String.h"
#include<stdio.h>
main(int, char *[])
{
String s, t;
s.set_to("hello world");
printf("Length of s is %d\n", s.length());
printf("Enter string t: ");
t.read();
printf("Length of t is %d\n",t.length());
return 0;
}
/* this is the end of file test.cc */
Re: how can i tell if my new c++ compiler is working?
Author: Jonathan Chen
Date: Mon, 04 Sep 1995 23:56
Date: Mon, 04 Sep 1995 23:56
21 lines
1313 bytes
1313 bytes
In <42fktm$g2p@news.acns.nwu.edu> bcc063@casbah.acns.nwu.edu (UNLISTED) writes: >i just finished installing the libg++ library for c++ and i want to test the >compiler to see if it is working. when i try to compile the following little >test file using gcc, >i get these errors: > collect2: ld returned 2 exit status > ld: Undefined symbol > String::length(void) > String::set_to(char *) > String::read(void) > >what do these errors mean i did or didn't do? You didn't write the routines for these functions in the files you supplied. If you're planning on using libg++ String routines, don't use your own home-grown stuff. -- Jonathan Chen e-mail : jonc@pinnacle.co.nz Pinnacle Software Ltd Voice : +64.9.489.7020 Auckland, New Zealand Fax : +64.9.489.7040
Re: how can i tell if my new c++ compiler is working?
Author: Andy J Robb
Date: Tue, 05 Sep 1995 04:32
Date: Tue, 05 Sep 1995 04:32
64 lines
2317 bytes
2317 bytes
bcc063@casbah.acns.nwu.edu (UNLISTED) wrote:
>i just finished installing the libg++ library for c++ and i want to test the
>compiler to see if it is working. when i try to compile the following little
>test file using gcc,
>i get these errors:
> collect2: ld returned 2 exit status
> ld: Undefined symbol
> String::length(void)
> String::set_to(char *)
> String::read(void)
>what do these errors mean i did or didn't do?
>(i'm new at c++)
>below are the files...
>/* this is the beginning of file String.h */
>const int max_string_length = 128;
>class String{
>public:
> void set_to(char*);
> int length();
> int read();
> void print();
>private:
> char text[max_string_length+1];
>};
>/* this is the end of file String.h */
You have only "declared" the member functions, set_to, length, read
and printf. You must "define" them as well. If they are simple (one
liners) then this can be done within the class instead of the
declarations. . .
#include <stdio.h>
#include <string.h>
class String
{ char text[max_string_length+1);
public:
void set_to(char *str) { strncpy(text, str, sizeof text); }
int length() { return strlen(text); }
void read() { gets(text); }
void printf() { puts(text); }
};
Simple member functions like these are implicitly "inline" so they
incur no overhead above directly accessing a structure member in C.
Regards,
Andy Robb.
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: 2.6.2i
mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
=/wVD
-----END PGP PUBLIC KEY BLOCK-----
Re: how can i tell if my new c++ compiler is working?
Author: Michael Bresnahan
Date: Tue, 05 Sep 1995 06:07
Date: Tue, 05 Sep 1995 06:07
39 lines
1314 bytes
1314 bytes
The errors you got do just pretain to C++. They pertain to any language
that uses a linker (I think). The problem is that you made references to
functions you didn't define. You _declared_ them in your header file,
but you didn't link in a module where they are defined.
You another file something like this:
String.cc:
#include "String.h"
String::set_to( char *s) {
strcpy( text, s);
}
String::length() {
return strlen( text);
}
String::read() {
return text;
}
String::print() {
cout << text;
}
You need to compile this file and link it with your program. An
easy way to do this is:
g++ my_prog.c String.c -o my_prog
Hope this helps.
MikeB
Re: how can i tell if my new c++ compiler is working?
Author: Steve E. Chapel
Date: Tue, 05 Sep 1995 08:14
Date: Tue, 05 Sep 1995 08:14
51 lines
2169 bytes
2169 bytes
bcc063@casbah.acns.nwu.edu (UNLISTED) writes:
>what do these errors mean i did or didn't do?
>/* this is the beginning of file String.h */
>const int max_string_length = 128;
>class String{
>public:
> void set_to(char*);
> int length();
> int read();
> void print();
>private:
> char text[max_string_length+1];
>};
>/* this is the end of file String.h */
>/* and the beginning of file test.cc */
>#include "String.h"
>#include<stdio.h>
>main(int, char *[])
>{
> String s, t;
> s.set_to("hello world");
> printf("Length of s is %d\n", s.length());
> printf("Enter string t: ");
> t.read();
> printf("Length of t is %d\n",t.length());
> return 0;
>}
>/* this is the end of file test.cc */
You never defined the member functions. You gave the declarations, but
never the definitions. This is what the linker is complaining about.
The compiler sees just the declarations and write the function calls,
assuming you will link in the called functions. But since you never gave
the definitions of the functions, the linker was not able to write
the machine code for the function calls.
You need a String.cc file that contains the definitions. It should #include
"String.h". You'll compile it separately from test.cc. Then you'll link
test.o and String.o (or whatever your object files are called).
By the way, the main(int, char *[]) is a new twist on an old theme.
I'd recommend int main(). Ignoring the argc and agrv arguments is okay.
--
Steve Chapel schapel@cs.ucsb.edu | http://www.cs.ucsb.edu/~schapel
Senior in Computer Science at the | finger -l schapel@eci1.ucsb.edu
University of California, Santa Barbara | Finger me for my PGP public key
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads