LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-15-2004, 08:24 PM   #1
ttucker
LQ Newbie
 
Registered: May 2004
Location: TX. boo TX
Distribution: RH 9
Posts: 12

Rep: Reputation: 0
basic object compiling c++


ok, i have no idea how to link my .h files and .c files
I have 3 files, they are: testerprog.c, outlinepiece.c, outlinepiece.h
and look like:

//testerprof.c
#include <iostream>
#include <string>
#include "outlinepiece.h"

using namespace std;

int main
{
string strnode = "asdf";
string strsubnodes = "sdfg lkjh oiuy";
int numnode = 3;
outlinepiece tester1(numnode, strnode, strsubnodes);
cout << tester1.givenode() << tester1.givesupernode() << tester1.givenumnode() << tester1.givesubnodes();
return 0;
}




//outlinepiece.c
#include <string>
#include "outlinepiece.h"

string outlinepiece::givenode()
{return node1;}

string outlinepiece::givesubnode()
{return nodes2;}

string outlinepiece::givesupernode()
{return node1.substring(1,-1);}

int outlinepiece::givenumnode()
{return nodeNUM;}



//outlinepiece.h
#ifndef OUTLINEPIECE_CLASS
#define OUTLINEPIECE_CLASS

class outlinepiece {
public:
outlinepiece( int nodeNUM, string node1, string nodes2);
string givenode();
string givesubnode();
string givesupernode();
int givenumnode();
};

#endif // OUTLINEPIECE_CLASS




I used:
gcc -o testerprog testerprog.c outlinepiece.c

I get so many errors like:
testerprog.c:2:20: iostream: No such file or directory
testerprog.c:3:18: string: No such file or directory
In file included from testerprog.c:4:
outlinepiece.h:4: parse error before "outlinepiece"
outlinepiece.h:5: syntax error before '{' token
outlinepiece.h:8: warning: data definition has no type or storage class
outlinepiece.h:9: parse error before "givesubnode"
outlinepiece.h:9: warning: data definition has no type or storage class
outlinepiece.h:10: parse error before "givesupernode"
outlinepiece.h:10: warning: data definition has no type or storage class
outlinepiece.h:12: parse error before '}' token
testerprog.c:6: parse error before "namespace"
testerprog.c:6: warning: data definition has no type or storage class
testerprog.c:9: syntax error before '{' token
testerprog.c:11: warning: initialization makes integer from pointer without a cast
testerprog.c:11: warning: data definition has no type or storage class
testerprog.c:13: parse error before "tester1"
testerprog.c:13: warning: parameter names (without types) in function declaration
testerprog.c:13: warning: data definition has no type or storage class
testerprog.c:14: parse error before '<<' token
outlinepiece.c:2:18: string: No such file or directory
In file included from outlinepiece.c:3:
outlinepiece.h:4: parse error before "outlinepiece"
outlinepiece.h:5: syntax error before '{' token
outlinepiece.h:8: warning: data definition has no type or storage class
outlinepiece.h:9: parse error before "givesubnode"
outlinepiece.h:9: warning: data definition has no type or storage class
outlinepiece.h:10: parse error before "givesupernode"
outlinepiece.h:10: warning: data definition has no type or storage class
outlinepiece.h:12: parse error before '}' token
outlinepiece.c:5: parse error before "outlinepiece"
outlinepiece.c:14: parse error before ':' token

what is going on here?
how do I compile this?

thanks,
ttucker
 
Old 09-15-2004, 08:29 PM   #2
Cake
LQ Newbie
 
Registered: Jan 2004
Location: UK
Distribution: Fedora Core 3
Posts: 27

Rep: Reputation: 15
Hey I just started with C++ the other day. I read the documentation for gcc, and it seems to suggest all along that it will detect files in C++ rather than in plain C and compile them accordingly. Unfortunately this didn't work for me. To cut a long story short, replace 'gcc' in your command with 'g++' to force it to compile for C++.

I hope this helps!
 
Old 09-15-2004, 08:39 PM   #3
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 30
An awful lot of the times, subsequent errors are caused by the very first error.

Examine the first error listed and tell me what happened.

This is a case of me not giving you the fish but teaching you how to fish. You'll get no production if you have to wait for someone else to debug your problems.
 
Old 09-16-2004, 01:18 AM   #4
rustynailz
Member
 
Registered: Jun 2004
Distribution: MDK 9.2/10.0, VectorLinux 4.0
Posts: 50

Rep: Reputation: 15
<iostream> is a C++ idea. If you want to use cout and the like, rename your .c files to .cc files and compile with g++.

g++ -o testerprog testerprog.cc outlinepiece.cc
 
Old 09-16-2004, 11:55 AM   #5
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 30
Re: basic object compiling c++

Quote:
Originally posted by ttucker
#include <iostream>
#include <string>
#include "outlinepiece.h"
Originaly posted by rustynailz
Quote:
<iostream> is a C++ idea.
I don't code in C++ and its been a few years since I wrote anything in pre-ansii C, so I guess standards change drasticlly.

How does the compiler know if it is compiling function code or header code?
 
Old 09-16-2004, 12:30 PM   #6
ttucker
LQ Newbie
 
Registered: May 2004
Location: TX. boo TX
Distribution: RH 9
Posts: 12

Original Poster
Rep: Reputation: 0
ok take 2

now the files look like this:

//testerprog.cpp
#include <iostream>
#include <string>
#include "outlinepiece.h"

using namespace std;

int main{
string strnode = "asdf";
string strsubnodes = "sdfg lkjh oiuy";
int numnode = 3;
outlinepiece tester1(numnode, strnode, strsubnodes);
cout << tester1.givenode() << tester1.givesupernode() << tester1.givenumnode() << tester1.givesubnodes();
return 0;
}



//outlinepiece.h
#ifndef OUTLINEPIECE_CLASS
#define OUTLINEPIECE_CLASS

#include <string>

using namespace std;

class outlinepiece {
public:
outlinepiece( int nodeNUM, string node1, string nodes2);
string givenode();
string givesubnode();
string givesupernode();
int givenumnode();
};

#endif // OUTLINEPIECE_CLASS



//outlinepiece.cpp
#include <string>
#include "outlinepiece.h"
using namespace std;

string outlinepiece::givenode()
{return node1;}

string outlinepiece::givesubnode()
{return nodes2;}

string outlinepiece::givesupernode()
{return node1.substring(1,-1);}

int outlinepiece::givenumnode()
{return nodeNUM;}



I compiled it with: g++ -o testerprog testerprog.cpp

I get the following result:
[root@localhost bin]# g++ -o testerprog testerprog.cpp
testerprog.cpp:8: syntax error before `{' token
testerprog.cpp:12: `strnode' was not declared in this scope
testerprog.cpp:13: syntax error before `<<' token

ok, on the first two, I have no clue what is going on. on the third, I think it might be a namespace issue?
any help is appreciated, it has come a long way fast, thanks to all (except Dave with that fish remark), I've read a lot about C++, just squat about compilers

ttucker
(and now Diane being a bitch, Diane)
 
Old 09-16-2004, 01:02 PM   #7
WhiteChedda
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Mandrake 9.1 for now
Posts: 205

Rep: Reputation: 30
Re: ok take 2

Originally posted by ttucker
now the files look like this:

//testerprog.cpp
#include <iostream>
#include <string>
#include "outlinepiece.h"

using namespace std;

int main{ try changing to int main()
 
Old 09-16-2004, 05:54 PM   #8
ttucker
LQ Newbie
 
Registered: May 2004
Location: TX. boo TX
Distribution: RH 9
Posts: 12

Original Poster
Rep: Reputation: 0
ok, it got weird

so i fixed that () and the strnodes error; the << error went away on its own
now the files look like:
//testerprog.cpp
#include <iostream>
#include <string>
#include "outlinepiece.h"

using namespace std;

int main()
{
string strnode = "asdf";
string strsubnodes = "sdfg lkjh oiuy";
int numnode = 3;
outlinepiece tester1(numnode, strnode, strsubnodes);
cout << tester1.givenode() << tester1.givesupernode() << tester1.givenumnode() << tester1.givesubnode();
return 0;
}

//outlinepiece.cpp
#include <string>
#include "outlinepiece.h"
using namespace std;

string outlinepiece::givenode()
{return node1;}

string outlinepiece::givesubnode()
{return node2;}

string outlinepiece::givesupernode()
{return node1.substring(1,-1);}

int outlinepiece::givenumnode()
{return nodeNUM;}

//outlinepiece.h
#ifndef OUTLINEPIECE_CLASS
#define OUTLINEPIECE_CLASS

#include <string>

using namespace std;

class outlinepiece {
public:
outlinepiece( int nodeNUM, string node1, string node2);
string givenode();
string givesubnode();
string givesupernode();
int givenumnode();
};

#endif // OUTLINEPIECE_CLASS


[root@localhost bin]# g++ -o testerprog testerprog.cpp
/usr/bin/ld: cannot open crt1.o: No such file or directory
collect2: ld returned 1 exit status

No idea what is going on now, none.

ttucker
 
Old 09-16-2004, 07:15 PM   #9
Cake
LQ Newbie
 
Registered: Jan 2004
Location: UK
Distribution: Fedora Core 3
Posts: 27

Rep: Reputation: 15
Re: ok take 2

Nice Family Guy quote ttucker!
 
Old 09-17-2004, 08:06 AM   #10
WhiteChedda
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Mandrake 9.1 for now
Posts: 205

Rep: Reputation: 30
Re: ok, it got weird

Quote:
Originally posted by ttucker

[root@localhost bin]# g++ -o testerprog testerprog.cpp
/usr/bin/ld: cannot open crt1.o: No such file or directory
collect2: ld returned 1 exit status

No idea what is going on now, none.

ttucker
I'm assuming the program is having trouble locating what it needs to link which makes sense, since you have 3 source files and are only compiling one, in which case you need to tell it to compile and NOT link sinc eyou have not compiled the other two source files to link with yet. Try adding -c to the command line, this tell the compiler to simply compile and not link.

You should compile the two source files that do not define the funciton main() using -c then try compiling the one with main not using the -c option. If your paths are set up right, the compiler then should compile the code, in the initial fiel and link it to the code in the other 2 files correctly.

You might be better off looking into an IDE, I'm guessing you are used to using an IDE with project files to compile your apps, and not used to manually compiling each source file and linking them yourself.

I have been out of programming for far too long to reccomend an IDE to you however.
hopefully someone else here can steer you to the direction of a half way decent Integrated Development Environemnt for G++.

Also it might be strings and not string you need to include, it has been a while but I seem to recall it having an s.

Last edited by WhiteChedda; 09-17-2004 at 08:11 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
basic linux programming/compiling question (permissions error) Godsmacker777 Programming 11 03-17-2005 11:35 AM
Gatos: help please with basic compiling of Components Metal Martian Linux - Newbie 2 04-01-2004 12:49 AM
Compiling a basic KDE program cbcallaw Linux - Software 1 03-09-2004 11:34 AM
Attn VB lovers:Phoenix Object Basic nbjayme Linux - Software 3 02-10-2004 10:45 PM
Event driven object-to-object: C++ template class mecanism ( NOT STL or STDC++) bretzeltux Programming 2 12-23-2003 02:45 PM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration