LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 06-07-2015, 04:18 AM   #1
jovin555
Member
 
Registered: Jul 2013
Location: Trivandrum,India
Distribution: Debian Wheezy
Posts: 31

Rep: Reputation: Disabled
g++ -o command not working


I am trying to do the first example of advanced linux programming.pdf.In page 30,there is an example.I have compiled the code.But when i try to execute the command

g++ -o reciprocal main.o reciprocal.o

I am getting the following errors

main.o: In function `main':
main.c.text+0x25): undefined reference to `reciprocal'
collect2: error: ld returned 1 exit status

these are my files.
main.c
Code:
#include <stdio.h>
#include "reciprocal.hpp"

int main(int argc,char **argv)
{
	int i;
	i = atoi(argv[1]);
	printf("The reciprocal of %d is %g\n",i,reciprocal(i));
	return 0;
}
reciprocal.cpp
Code:
#include <cassert>
#include "reciprocal.hpp"

double reciprocal (int i){
//i should be non-zero
assert(i!=0);
return 1.0/i;
}
reciprocal.hpp
Code:
#ifdef _cplusplus
extern 'C'{
#endif

extern double reciprocal(int i);

#ifdef _cplusplus
}
#endif

Please let me know what is wrong with this command execution
 
Old 06-07-2015, 05:50 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try this:
Code:
g++ -o reciprocal main.c reciprocal.c
 
Old 06-07-2015, 06:46 AM   #3
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by jovin555 View Post

g++ -o reciprocal main.o reciprocal.o

I am getting the following errors

main.o: In function `main':
main.c.text+0x25): undefined reference to `reciprocal'
collect2: error: ld returned 1 exit status
main needs stuff from reciprocal so the linker must see reciprocal first

change the order to g++ -o reciprocal reciprocal.o main.o
 
Old 06-07-2015, 11:53 AM   #4
jovin555
Member
 
Registered: Jul 2013
Location: Trivandrum,India
Distribution: Debian Wheezy
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
main needs stuff from reciprocal so the linker must see reciprocal first

change the order to g++ -o reciprocal reciprocal.o main.o
I tried this command and got the same error.

When i tried the other suggestion,

Quote:
g++ -o reciprocal main.c reciprocal.c
I got the following error.

Code:
main.c: In function ‘int main(int, char**)’:
main.c:7:18: error: ‘atoi’ was not declared in this scope
 
Old 06-07-2015, 12:08 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
my bad: you don't have main.c and recefice.c only main.cpp and recefice.cpp:

Code:
g++ -o reciprocal main.cpp reciprocal.cpp
if the problem persists, add the missing include into main.cpp:

Code:
#include <cstdlib>
PS: if you do have 'main.c' then rename it to 'main.cpp'

Last edited by NevemTeve; 06-07-2015 at 12:09 PM.
 
Old 06-07-2015, 12:14 PM   #6
jovin555
Member
 
Registered: Jul 2013
Location: Trivandrum,India
Distribution: Debian Wheezy
Posts: 31

Original Poster
Rep: Reputation: Disabled
I have main.c,reciprocal.cpp and reciprocal.hpp as i have mentioned these with the code at start of the thread.

I have included the "#include <cstdlib>" file in my main.c.
but when compiling that gives me error.

Code:
main.c:3:21: fatal error: cstdlib.h: No such file or directory
compilation terminated.
The name of the pdf that i am following is
Advanced Linux Programming - Richard Esplin

Last edited by jovin555; 06-07-2015 at 12:17 PM.
 
Old 06-07-2015, 01:13 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please read the last sentence of my previous post.
 
Old 06-07-2015, 01:22 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please don't think C and C++ are freely interchangeable synonyms; they are different programming languages.
From C++ you can call C, from C you are not to call C++ (it might or mightn't work).
 
Old 06-08-2015, 09:39 AM   #9
jovin555
Member
 
Registered: Jul 2013
Location: Trivandrum,India
Distribution: Debian Wheezy
Posts: 31

Original Poster
Rep: Reputation: Disabled
I have tried changing main.c to main.cpp.When i compile using command

g++ -c main.cpp

i am getting the following errors

main.cpp: In function ‘int main(int, char**)’:
main.cpp:7:18: error: ‘atoi’ was not declared in this scope
 
Old 06-08-2015, 09:47 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I've already suggested adding this line into main.cpp:

Code:
#include <cstdlib>
 
Old 06-08-2015, 11:10 AM   #11
jovin555
Member
 
Registered: Jul 2013
Location: Trivandrum,India
Distribution: Debian Wheezy
Posts: 31

Original Poster
Rep: Reputation: Disabled
After adding #include <cstdlib> to the main.cpp file,it worked well.
Thanks NevemTeve for your help.
 
Old 06-08-2015, 12:24 PM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You are welcome.
 
  


Reply



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
Synaptic package not working from xcfe yet working from the command line onerous Debian 8 11-15-2012 03:40 PM
[SOLVED] command line cd command not working right bjrn64 Linux - Software 6 05-10-2010 05:46 AM
./configure command isn't working in command line jumpmansbro Linux - Newbie 20 01-22-2009 08:29 PM
'at' command not working minike Slackware 9 03-04-2006 07:59 PM
su command not working dahmad Linux - Newbie 6 02-28-2003 12:08 AM

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

All times are GMT -5. The time now is 06:34 PM.

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