LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
  Search this Thread
Old 07-06-2005, 03:53 AM   #1
nelnel
Member
 
Registered: May 2005
Posts: 47

Rep: Reputation: 15
Unhappy C++ Compilation problem with G++


hi,
i am writing a simple c++ program to test the linking problem. i know that is it the beginner problem , but i really can't find the solution. i have written total 3 files - main.cpp, abc.cpp and abc.h.
abc.h:


class ABC
{
public:
void test(int i);
};

abc.cpp:

#include "abc.h"

#include <stdio.h>
#include <string.h>

void ABC::test(int i)
{
printf("testing success");
}

main.cpp:


#include <stdio.h>
#include <string>
#include "abc.h"

int main ()
{
ABC tabc;

tabc.test(3);
return 0;
}


I have tried the code in window VC6, there is no problem in exe. and running. But when i compile it under Linux with G++, the error occur in compiling to execute file.

the compilation statement:
g++ -o test.o -c main.cpp
g++ -o test test.o

error message is :

: undefined reference to `ABC::test(int)'
collect2: ld returned 1 exit status

i found that it may be the linking problem, but i can find the solution of that.

Thx for ur help!!
 
Old 07-06-2005, 04:22 AM   #2
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
you are not compiling abc.cpp only main.cpp, so the compiler knows from abc.h that test(int) exist but the linker cannot find it in an object file ( or the system libraries) to link with. try this:

Code:
g++ -c main.cpp
g++ -c abc.cpp
g++ -o test1 abc.o main.o
the reason for the 1 at the end of test is most distro's come with a built in command called "test", so if you current working directory is not part of your path you may inadvertently call it instead of your program.
 
Old 07-06-2005, 08:30 PM   #3
nelnel
Member
 
Registered: May 2005
Posts: 47

Original Poster
Rep: Reputation: 15
thx for ur help!!

but i still have the same error in the compilation......

error :

[root@H733-00011 test]# g++ -o test abc.o main.o
strncat.o(.text+0xf3): In function `main':
: undefined reference to `ABC::test(int)'
collect2: ld returned 1 exit status
[root@H733-00011 test]#

what's wrong with it?
 
Old 07-06-2005, 08:47 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
g++ -c -o abc.o abc.cpp 
g++ -o test abc.o main.cpp
[edit]
Sorry, that's just a shortcut-version of the other statements :)

What your problem is is that the compiler doesn't seem
to see some SYSTEM libraries...
[/edit]


Cheers,
Tink

Last edited by Tinkster; 07-06-2005 at 08:53 PM.
 
Old 07-06-2005, 09:39 PM   #5
nelnel
Member
 
Registered: May 2005
Posts: 47

Original Poster
Rep: Reputation: 15
Really thx for ur kindly help!!!

but i still get the same error....

[root@H733-00011 test]# g++ -o test abc.o main.cpp
/tmp/ccOwk4N6.o(.text+0xf3): In function `main':
: undefined reference to `ABC::test(int)'
collect2: ld returned 1 exit status

Sorry, i haven't clicked the THANKS button~~, ihave clicked it now~~

thxs
 
Old 07-06-2005, 09:53 PM   #6
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 139Reputation: 139
The program compiles and works absolutely fine for me.

Cannot see what your problem could be. Maybe there is a problem with the compiler/linker configuration.
 
Old 07-06-2005, 09:58 PM   #7
nelnel
Member
 
Registered: May 2005
Posts: 47

Original Poster
Rep: Reputation: 15
thx for ur help

i have solved the problem because of my typing mistake~~

 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel compilation problem JJX Linux - Software 4 08-31-2005 05:50 AM
Compilation Problem jonty_11 Programming 4 03-24-2005 09:45 PM
c compilation problem edreddy Programming 5 06-02-2004 12:37 PM
Problem with Apache compilation gr8geezer Linux - Software 0 03-13-2003 11:25 PM
Compilation problem... need help Korshun Programming 2 05-07-2002 12:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration