nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Dumb Newbie problem -- can't compile

This is a discussion on Dumb Newbie problem -- can't compile within the Coding in General forums, part of the Development/Scripting category; Hi, I am trying to teach myself C++ and to start I plan to learn C. I have recently set ...


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 01-06-2006, 07:14 AM
Henry
Guest
 
Posts: n/a
Default Dumb Newbie problem -- can't compile

Hi,
I am trying to teach myself C++ and to start I plan to learn C. I have recently set up a Red Hat FC4 box from a commercial DVD without any problems. I have bought a couple of C and C++ books and wrote a very simple program to test the compiling process. The C code is as follows:

#include <stdio.h>
main()
{
printf("\nHello World\n");
}

Using the command: "gcc testprog.c -o testprog.out" it compiles without error messages and produces the output file (which shows as an executable file in the file browser). My problem is that when I type "testprog.out" at the command prompt I get the following response:
bash: testprog.out : command not found
What am I doing wrong? In frustration after much experimentation I changed the code to be C++ compatible:
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
}

This does not compile successfuly and gives me several errors relating to undefined references to std:cout,std::basic_ostream<<char,std::char_traits <<char and many other references to what I assume are I/O functions The error messages end with "ld returned 1 exit status" I assume this is all telling me that I have a linking problem to <iostream>. none of the books I have or the man pages give me any suggestions how to get things working. HELP!!!!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-06-2006, 01:19 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

Quote:
Using the command: "gcc testprog.c -o testprog.out" it compiles without error messages and produces the output file (which shows as an executable file in the file browser). My problem is that when I type "testprog.out" at the command prompt I get the following response:
bash: testprog.out : command not found
Make sure you are in same directory where testprog.out is created second you need to type command as follows:
./testprog.out
OR
/path/to/testprog.out

Here is C++ example (remember file extension use cpp or capital C):

Code:
#include <iostream> // Required for std::cout
int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}
Compile:
Code:
g++ demo.C -o demo
Execute:
Code:
./demo
If you have any further problem feel free to reply
Reply With Quote
  #3 (permalink)  
Old 01-07-2006, 03:54 AM
Henry
Guest
 
Posts: n/a
Default still can't compile

Well I have tried running the C variation with "/testprog" (and the working directory contains the executable file) but it still does not recognize the file. Adding the starting "/" changes the error message from "Command not found" to "No such file or directory"

The C++ varient still does not compile. It still seems to be a problem with using <iostream> It seems to have a problem with "std::cout" if I leave off the "std::" on cout, it just doesnt recognize it and yhe error message is much simpler (see second listing below)

When I type the code as suggested:
************************************************** ****
#include <iostream>

int main()
{
std::cout<<"Hello World" <<std::endl;
return 0;
}
************************************************** ***
I get the following when I try to compile it:

[hgerar@localhost cdev]$ gcc testprog2.C -o testprog2

/tmp/ccQqAE2Y.o(.text+0x25): In function `main':
testprog2.C: undefined reference to `std::cout'
/tmp/ccQqAE2Y.o(.text+0x2a):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccQqAE2Y.o(.text+0x35):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/tmp/ccQqAE2Y.o(.text+0x3b):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >:perator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccQqAE2Y.o(.text+0x59): In function `__tcf_0':
testprog2.C: undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccQqAE2Y.o(.text+0x86): In function `__static_initialization_and_destruction_0(int, int)':
testprog2.C: undefined reference to `std::ios_base::Init::Init()'
/tmp/ccQqAE2Y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

If I leave out the "srd::" infront of cout I get the following:
**********************************************
#include <iostream>

int main()
{
cout<<"Hello World" <<std::endl;
return 0;
}
***********************************************
I get the following:

[hgerar@localhost cdev]$ gcc testprog2.C -o testprog2

testprog2.C: In function ‘int main()’:
testprog2.C:5: error: ‘cout’ was not declared in this scope

If I put in the "using namespace std;" I get the same rror message as with "std::cout"
I am confused!!!?? Why can't it find "std::cout"?
Reply With Quote
  #4 (permalink)  
Old 01-07-2006, 10:41 PM
Guest
 
Posts: n/a
Default

Quote:
Well I have tried running the C variation with "/testprog" (and the working directory contains the executable file) but it still does not recognize the file. Adding the starting "/" changes the error message from "Command not found" to "No such file or directory"
It is not just / it is ./ (dot slash) i.e.

./myprog.out
Reply With Quote
  #5 (permalink)  
Old 01-08-2006, 12:50 AM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Henry,

You are using which Linux distro? You need to have full development tools installed as well as all libraries. For c prog
Code:
gcc myprog.c
To execut:
Code:
./a.out
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #6 (permalink)  
Old 01-09-2006, 03:34 PM
Henry
Guest
 
Posts: n/a
Default Hey "./" works!!! but C++ "std::cout" i

OK.

I can get my c programs to run using the "./" in front of the file name!

As far as the distro I am running, it is a recent Redhat FC4 from the commercial DVD that came with the "Red Hat Fedora and Enterprise Linux 4 Bible" book.

I chose the install all packages option, so all development tools and libraries should be installed. The version of GCC is 4.0.1
Reply With Quote
  #7 (permalink)  
Old 01-10-2006, 07:46 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

Try to upgrade gcc and other packages via yum. Look like your gcc is not working right so just update it
Reply With Quote
  #8 (permalink)  
Old 01-12-2006, 05:24 AM
Henry
Guest
 
Posts: n/a
Default Solution Found!

:P Well I figured out how to make my X++ code compile!!

It was obviously some sort of library linking issue. I had used the command
gcc testprog2.cpp -o testprog2
without success. I think that library linking options on this command would make it work, but I found a reference in the GCC info pages that mentioned that using the G++ command automatically included the C++ libraries in the compile process. So I tried it and it works perfectly!

g++ testprog2.cpp -o testprog2

Ok so now I can at least compile the code, now I just have to learn the language.....
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Newbie here...looking to install Newlnx Getting started tutorials 4 03-25-2008 12:26 AM
sa-compile diptanjan Linux software 1 06-07-2007 08:07 PM
squid for newbie raghuram Linux software 5 02-17-2007 05:47 PM
Kernel Compile mannrj45 Linux software 1 12-02-2006 09:32 PM
problems starting squid for a rank newbie scarletlancer Linux software 1 11-02-2006 05:20 PM


All times are GMT +5.5. The time now is 03:22 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36